Iphone 当用户缩小时,自动放大将重置
我制作了一张地图,通过自动放大显示用户位置,但是当用户尝试缩小时,它是自动放大的,这是不实际的,您能帮我禁用用户缩小时的自动放大吗?。以下是我的部分代码:Iphone 当用户缩小时,自动放大将重置,iphone,Iphone,我制作了一张地图,通过自动放大显示用户位置,但是当用户尝试缩小时,它是自动放大的,这是不实际的,您能帮我禁用用户缩小时的自动放大吗?。以下是我的部分代码: - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ // store new user loc
- (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
我添加了getter-(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];
}
没有自动缩放这样的东西。您可以通过如下方式定义范围来手动设置缩放级别:
span.latitudeDelta = .005;
span.longitudeDelta = .005;
手动设置范围将始终显示固定的缩放级别
如果要保持当前缩放级别,请执行以下操作:
MKCoordinateRegion mapRegion = mapView.region; // To get the current span
mapRegion.center = newLocation.coordinate;
mapView.region = mapRegion;
考虑使用标志在所需行为之间切换
即
在viewDidLoad set\u应调整缩放=是代码>然后当用户调整缩放设置时\u应调整缩放=否代码>(当用户更改缩放时,会调用地图视图的委托方法)
您好,谢谢您的解释,问题,您所说的“考虑使用一个标志在您想要的行为之间切换”是什么意思?假设lat和lon变量包含其中的值??您好,罗伯特,谢谢您的解释,我有一个问题,你说当用户改变缩放时,地图视图有一个委托方法,我需要知道的是,它是自动调用的吗??它叫什么名字??谢谢
MKCoordinateRegion mapRegion = mapView.region; // To get the current span
mapRegion.center = newLocation.coordinate;
mapView.region = mapRegion;
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (_shouldAdjustZoom) {
// Use the manually defined span
} else {
// Keep the current span
}
}