当用户按下按钮时,我使用以下代码获取我的位置
[mapview setShowsUserLocation:YES];
Run Code Online (Sandbox Code Playgroud)
接下来将地图居中到te用户的位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何防止地图始终以我的位置为中心?我想允许用户平移地图.
仅在您第一次显示地图时才在中心位置.这是一些伪代码......
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if(shouldCenterLocation){
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
shouldCenterLocation = FALSE;
}
//do all your other stuff here
}
Run Code Online (Sandbox Code Playgroud)
shouldCenterLocation是一个布尔标志,您可以在第一次显示地图时将其设置为TRUE,然后将其设置为FALSE,直到您退出视图(或显示中心位置的任何其他条件).
编辑:您可以按照按下按钮的相同方法切换shouldCenterLocation的状态.
我认为它来自iOS5,您现在可以删除委托内容并只设置MKMapView的userTrackingMode.将它设置为MKUserTrackingModeFollow以使make与用户一起移动,然后当它们开始平移地图时,它将自动关闭跟踪模式,然后您只需要提供一个按钮将其重新打开.
| 归档时间: |
|
| 查看次数: |
6843 次 |
| 最近记录: |