ios - 为什么我的用户位置总是在中心?

标签 ios objective-c mapkit

我的应用程序中有一张 map ,我在 map 打开时设置中心用户位置。 我用过这段代码:

 map.delegate=Self; 
 ......
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

MKCoordinateRegion mapRegion;
mapRegion.center = self.mapView.userLocation.coordinate;
mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance
[self.mapView setRegion:mapRegion animated: YES];
}

但是当我移动 map 时它又回到了用户位置。 如何在不关心用户位置的情况下自由移动到我的 map ?

最佳答案

这是因为每次用户位置更改时您都在重置位置。您应该只执行一次,例如

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    if (self.centerToUserLocation)
    {
        MKCoordinateRegion mapRegion;
        mapRegion.center = self.mapView.userLocation.coordinate;
        mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance
        [self.mapView setRegion:mapRegion animated: YES];
        self.centerToUserLocation = NO;
    }
}

centerToUserLocation 类似于 @property (nonatomic) BOOL centerToUserLocation

关于ios - 为什么我的用户位置总是在中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28497973/

相关文章:

swift - 如何快速修复此 'Invalid Region <center:-180.00000000, -180.00000000 span:nan, nan>' 错误

ios - 内存管理 - 加载音效 - Cocoa Xcode

iphone - 如何在 ios 中切换隐藏和查看密码?

ios - plist 或文本文件

iphone - 复制数组 - 糟糕的设计?

ios - 如何更改 UISearchBar 中 UITextField 的属性

ios - 在 map 上显示用户的位置

ios - 为什么不同设备的单个应用程序中的日期不同?

objective-c - 将 NSNumber 对象分配给 NSString 对象不会导致任何错误或警告

swift - MKMapView 上的模糊