MKMapView userLocation在模拟器中显示0,0?

say*_*guh 11 mkmapview ios

我的视图(MKMapViewDelegate)上有一个mapView插座,启用了"显示用户位置".

在我的控制器的viewdidload中

CLLocation *userLoc = mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude); 
mapView.delegate=self;
Run Code Online (Sandbox Code Playgroud)

模拟器在地图上显示正确的用户位置(我在IOS模拟器中使用自定义位置)

但NSLog显示纬度和经度为0和0.

我不应该在模拟器中获得自定义经度和纬度吗?

更新与答案:

需要实施

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    self.mapView.centerCoordinate = userLocation.location.coordinate;
} 
Run Code Online (Sandbox Code Playgroud)

Jim*_*Jim 13

定位服务不是即时的.你的代码在viewDidLoad,所以它还没有机会修复你的位置.您应该将delegate地图视图的属性设置为实现MKMapViewDelegate协议的对象.

  • 你需要实现`didUpdateUserLocation`.本网站以及Apple网站上应该有很多例子. (2认同)
  • iOS开发中心主页上有一个"示例代码"链接.如果单击它,它会显示Apple所有示例代码的列表.有一个搜索框,如果您在其中放置"地图",它会为您提供八个使用MapKit的示例项目. (2认同)