当用户缩小时自动缩放重置
问题描述:
我制作了一个地图,以自动放大显示用户位置,但是当用户尝试缩小时,它会自动缩放,这不切实际,您能帮我吗禁用用户缩小时的自动放大功能吗?。这里是我的代码部分:当用户缩小时自动缩放重置
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
// store new user location
location = newLocation.coordinate;
//move the map to the new user location
MKCoordinateRegion region;
region.center = location;
// zoom level
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
// apply new coordinates
[mapView setRegion:region animated:TRUE];
}
编辑: 我在viewDidLoad中添加以下语句:
mapView.zoomEnabled=FALSE;
应该禁用它在当用户缩小自动变焦?
- (void)viewDidLoad {
[super viewDidLoad];
//
// On veut afficher la position courante de l'utilisateur
[mapView setShowsUserLocation:TRUE];
// MKMapTypeStandard est un mode d'affichage parmis 3 disponibles
// (les deux autres sont MKMapTypeSatelitte et MKMapTypeHybrid et MKMapTypeStandard)
[mapView setMapType:MKMapTypeHybrid];
// Le fait de setter le Delegate permet d'appeler méthodes implémentées dans cette classe
[mapView setDelegate:self];
// On ajoute la View du mapView a la View du controlleur courant afin de faire afficher la carte
[self.view insertSubview:mapView atIndex:0];
// CLLocationManager permet la gestion de la position géographique de l'utilisateur
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
// Le fait de setter le Delegate permet d'appeler méthodes implémentées dans cette classe
[locationManager setDelegate:self];
// Définit l'échelle de distance à prendre en compte pour le raffraichissement de la position courante
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
mapView.zoomEnabled=FALSE;
}
编辑: 我仍然工作在这一点,所以在继续之前,我想告诉你我的逻辑,我等待你的建议:) 所以,在我看来这是充电为了向我显示地图上的用户位置,添加了一个布尔变量来测试用户是否调整了缩放比例。 .H
BOOL shouldAdjustZoom;
和方法:
-(void)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels;
-(void)setShouldAdjustZoom:(BOOL)shouldAdjZoom;
-(BOOL)shouldAdjustZoom;
.M
我添加了吸气-(BOOL)shouldAdjustZoom
的实施,使此getter将调用zoomLevelForMapRect
知道缩放级别是否改变。
-(BOOL)shouldAdjustZoom
{
[self zoomLevelForMapRect];
return shouldAdjustZoom;
}
-(void)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels {
NSUInteger zoomLevel=20;
MKZoomScale zoomScale=mRect.size.width/viewSizeInPixels.width;
double zoomExponent=log2(zoomScale);
zoomLevel=(NSUInteger)(20-ceil(zoomExponent));
if(zoomLevel<20)
{
[self setShouldAdjustZoom:NO];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
MKCoordinateRegion region;
region.center = location;
if ([self shouldAdjustZoom]==YES) {
// Use the manually defined span
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
} else {
// Keep the current span
MKCoordinateRegion mapRegion = mapView.region; // To get the current span
mapRegion.center = newLocation.coordinate;
mapView.region = mapRegion;
}
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
我需要知道的是,我应该怎么称呼的zoomLevelForMapRect
方法,它与参数,在吸气我需要调用它:
-(BOOL)shouldAdjustZoom
{
[self zoomLevelForMapRect];//how should I fix the call??
return shouldAdjustZoom;
}
答
有没有这样的东西自动缩放。您可以通过定义跨度来手动设置缩放级别:
span.latitudeDelta = .005;
span.longitudeDelta = .005;
手动设置跨度将始终显示固定缩放级别。
如果你想保持当前的缩放级别,这样做:
MKCoordinateRegion mapRegion = mapView.region; // To get the current span
mapRegion.center = newLocation.coordinate;
mapView.region = mapRegion;
考虑使用一个标志您的期望的行为之间交替。
I.e.
在viewDidLoad中设置_shouldAdjustZoom = YES;
然后,当用户调节缩放设置_shouldAdjustZoom = NO;
(有地图视图的委托方法,当用户改变变焦的称呼)
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (_shouldAdjustZoom) {
// Use the manually defined span
} else {
// Keep the current span
}
}
嗨,THX很多关于你的解释,问题,你的意思是“考虑使用一个标志来交替你想要的行为。” ?thx – Malloc 2011-04-10 10:44:50
经纬度和经度变量假设包含从哪里? – Malloc 2011-04-10 12:18:54
纬度和经度是用户的位置,不适合更新我的答案。 – Robert 2011-04-10 18:24:43