地图视图总是在我的位置中心地图

Tas*_*ris 2 mkmapview ios

当用户按下按钮时,我使用以下代码获取我的位置

 [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)

我的问题是如何防止地图始终以我的位置为中心?我想允许用户平移地图.

sco*_*ord 6

仅在您第一次显示地图时才在中心位置.这是一些伪代码......

    - (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的状态.

  • 请记住,您获得的第一个修复可能不是很准确,您可能希望在确定之前检查水平精度是否足够好. (3认同)

Cra*_*aig 5

我认为它来自iOS5,您现在可以删除委托内容并只设置MKMapView的userTrackingMode.将它设置为MKUserTrackingModeFollow以使make与用户一起移动,然后当它们开始平移地图时,它将自动关闭跟踪模式,然后您只需要提供一个按钮将其重新打开.