[英]How to display user location in mapkit?
我想为用户的位置显示蓝色脉冲点。 我这样做:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//some other stuff here
[self.mapView setShowsUserLocation:YES];
}
但我最终得到了
-[MKUserLocation establishment]: unrecognized selector sent to instance 0x125e90
我应该以其他方式这样做吗?
- 编辑 -
我也这样做,这是我最终获得上述异常的地方:
- (MKAnnotationView *) mapView:(MKMapView *)_mapView viewForAnnotation:(AddressNote *) annotation{
if(annotation.establishment != nil){
//do something}
establishment是我在AddressNote上的一个自定义类。 当establishment具有值时,会发生异常。 如果我没有设置ShowsUserLocation,一切正常,但当然,我没有看到用户位置。
当请求viewForAnnotation
,您需要检查给定的注释是否与当前用户位置相对应。 如果是,则返回nil,否则返回您自己的正常annoationView。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
if (annotation==self.mapView.userLocation)
return nil;
//then the rest of your code for all your 'classic' annotations.
[编辑]另一种方法是检查注释类的类型:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
//then the rest of your code for all your 'classic' annotations.
不确定实际问题是什么,但您不需要在每次更新时都这样做。 创建mapView
后设置一次,您应该开展业务。
mapView.showsUserLocation = YES;
您可能需要将地图缩放到用户实际所在的区域,以使点可见。
您正在使用错误的委托方法。 对于mapview,您应该使用此委托方法来捕获用户位置:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.