4thSpace
4thSpace

Reputation: 44352

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. When establishment has a value, the exception occurs. If I don't set ShowsUserLocation, everything works fine but of course, I don't see the user location.

Upvotes: 3

Views: 14673

Answers (3)

yonel
yonel

Reputation: 7865

When the viewForAnnotation is requested, you need to check wether or not the given annotation corresponds to the current user location. If yes, return nil, otherwise return your own normal 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.

Upvotes: 16

Van Du Tran
Van Du Tran

Reputation: 6892

You're using the wrong delegate method. For mapview, you should use this delegate method to catch the user location:

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

Upvotes: 0

Jakob Borg
Jakob Borg

Reputation: 24495

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.showsUserLocation = YES;

You might need to zoom the map to the region where the user actually is, for the dot to be visible.

Upvotes: 8

Related Questions