我正在使用 am map View ,有时当我打开 map 时,它会缩放到我的用户位置,但有时它会缩放到海洋中部。我不知道是什么原因造成的,这是我用于缩放的代码。我不希望 map 跟踪用户,而只是在用户打开 map 后缩放到他们的位置
-(void) viewDidAppear:(BOOL)animated{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 600.0f, 600.0f);
[self.mapView setRegion:region animated:YES];
}
-(void) viewDidLoad{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
}
最佳答案
我以前也遇到过这个问题。有时,mapView 加载和检测用户位置的速度似乎很慢,导致 viewDidAppear
中的代码在 map View 可以检查用户位置之前执行。因此,海洋中的那个点。
如果准备好,最好使用mapView的委托(delegate)来显示用户位置:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(isShowUserLocation)
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600.0, 600.0);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
isShowUserLocation = NO;
}
}
在viewDidLoad
中设置isShowUserLocation = YES
。这可确保用户位置在输入时显示一次,并在您需要时有选择地显示。
编辑1:
@implementation MapViewController
{
BOOL isShowUserLocation;
}
-(void) viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
isShowUserLocation = YES;
}
编辑2:
或者,使用 CLLocationManager - 请参阅 this post 。它允许您停止更新。请包含 CoreLocation.framework。不过,在 CLLocationManager 与 MapView 交互时,您可能需要处理一些具体问题。
关于ios - MKMapView 有时会缩放到用户位置有时会缩放到不知名的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739533/