我的视图(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;
模拟器在地图上显示正确的用户位置(我在IOS模拟器中使用自定义位置)
但NSLog显示纬度和经度为0和0。
我不应该在模拟器中获得自定义经度和纬度吗?
更新与答案:
需要实施
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
self.mapView.centerCoordinate = userLocation.location.coordinate;
}
定位服务不是即时的。你的代码在 viewDidLoad
,所以它还没有机会修复你的位置。你应该设置 delegate
用于实现地图的对象的地图视图的属性 MKMapViewDelegate
协议。
定位服务不是即时的。你的代码在 viewDidLoad
,所以它还没有机会修复你的位置。你应该设置 delegate
用于实现地图的对象的地图视图的属性 MKMapViewDelegate
协议。
我必须通过Debug> Location在模拟器中设置一个位置。它被设置为无,它将我倾倒在(0°,0°)并且从未更新过;您可以设置自定义位置,也可以选择随时间自动移动用户位置的其他模拟位置脚本。
MKMapView *mapview=[[MKMapView alloc]initWithFrame:CGRectMake(0,0,300, 364)];
mapview.delegate=self;
mapview.showsUserLocation=YES;
然后将调用mapkit委托,在mak工具包委托中尝试打印坐标。
NSLog(@“user latitude =%f”,userLocation.location.coordinate.latitude);
NSLog(@“user longitude =%f”,userLocation.location.coordinate.longitude);