如何在MKMapView中居我当前的位置?

vip*_*pul 18 iphone mkmapview ios

我通过使用启用来显示current locationin .我还希望将用户当前位置以及位置的缩放地图居中.请帮我解决这个问题,因为我在stackoverflow上没有得到任何其他类似问题的帮助.MKMapViewshowUserLocationmapview

我的代码如下:

- (void)viewDidLoad {
  [super viewDidLoad];
  [mapView setMapType:MKMapTypeStandard];
  [mapView setZoomEnabled:YES];
  [mapView setScrollEnabled:YES];
  [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
  [mapView setDelegate:self];
}
Run Code Online (Sandbox Code Playgroud)

Den*_*nis 51

要以用户位置为中心,您可以使用以下代码:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
Run Code Online (Sandbox Code Playgroud)

要放大特殊位置,您应该研究区域(MKCoordinateRegion)的计数和工作方式,计算区域的值并使用调用显示它:

[mapView setRegion:myRegion animated:YES];
Run Code Online (Sandbox Code Playgroud)

示例WorldCities显示了区域显示的基础知识.

  • 如果你试图从viewDidLoad设置它:它将失败,因为地图视图还不知道用户位置(它需要一些时间来接收来自GPS的响应); 所以从UIMapViewDelegate方法调用[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]是正确的 - (void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation (11认同)