简体   繁体   English

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

[英]How to display user location in mapkit?

I'd like to display the blue pulsing dot for a user's location. 我想为用户的位置显示蓝色脉冲点。 I'm doing this: 我这样做:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    *)newLocation fromLocation:(CLLocation *)oldLocation{
//some other stuff here
[self.mapView setShowsUserLocation:YES];
}

But I eventually get 但我最终得到了

-[MKUserLocation establishment]: unrecognized selector sent to instance 0x125e90

Should I be doing this some other way? 我应该以其他方式这样做吗?

-- EDIT -- - 编辑 -

I'm also doing this, which is where I eventually get the above exception: 我也这样做,这是我最终获得上述异常的地方:

- (MKAnnotationView *) mapView:(MKMapView *)_mapView viewForAnnotation:(AddressNote *) annotation{

if(annotation.establishment != nil){
//do something}

establishment is a custom class I have on AddressNote. establishment是我在AddressNote上的一个自定义类。 When establishment has a value, the exception occurs. 当establishment具有值时,会发生异常。 If I don't set ShowsUserLocation, everything works fine but of course, I don't see the user location. 如果我没有设置ShowsUserLocation,一切正常,但当然,我没有看到用户位置。

When the viewForAnnotation is requested, you need to check wether or not the given annotation corresponds to the current user location. 当请求viewForAnnotation ,您需要检查给定的注释是否与当前用户位置相对应。 If yes, return nil, otherwise return your own normal annoationView. 如果是,则返回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.

[EDIT] an alternative way is to check the kind of the class of the annotation : [编辑]另一种方法是检查注释类的类型:


- (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.

Not sure what the actual problem is, but you don't need to do this on every update. 不确定实际问题是什么,但您不需要在每次更新时都这样做。 Set it once after creating the mapView , and you should be in business. 创建mapView后设置一次,您应该开展业务。

mapView.showsUserLocation = YES;

You might need to zoom the map to the region where the user actually is, for the dot to be visible. 您可能需要将地图缩放到用户实际所在的区域,以使点可见。

You're using the wrong delegate method. 您正在使用错误的委托方法。 For mapview, you should use this delegate method to catch the user location: 对于mapview,您应该使用此委托方法来捕获用户位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM