如何在mapkit中显示用户位置?

时间:2010-03-14 19:02:45

标签: iphone cocoa-touch 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,一切正常,但当然,我没有看到用户位置。

3 个答案:

答案 0 :(得分:15)

当请求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.

答案 1 :(得分:8)

不确定实际问题是什么,但您不需要在每次更新时执行此操作。

}创建后设置一次,您应该开展业务。

mapView

您可能需要将地图缩放到用户实际所在的区域,以使点可见。

答案 2 :(得分:0)

你使用了错误的委托方法。对于mapview,您应该使用此委托方法来捕获用户位置:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}