3

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.

4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

16

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.

yonel
  • 7,855
  • 2
  • 44
  • 51
  • That gets rid of the exception but the pulsing blue dot isn't showing up. I'm guessing that is because I'm returning nil? Shouldn't some type of view be returned there? – 4thSpace Mar 22 '10 at 04:19
  • 1
    are you 100% sure that when you invoke [self.mapView setShowsUserLocation:YES]; your mapView is not nil ? I'm returning nil in my code and I get the blue dot :/ – yonel Mar 22 '10 at 09:13
  • I agree with yonel—this is nearly identical to the code I'm using (I check the annotation's class rather than whether it's equal to the location annotation), and the blue dot does appear on my map. – Noah Witherspoon May 12 '10 at 01:06
8

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.

Jakob Borg
  • 23,685
  • 6
  • 47
  • 47
  • Thanks. I'm now doing that in viewDidLoad but get the same exception. I've updated the question to reflect a few more details. If I don't set the location, it works fine. – 4thSpace Mar 14 '10 at 20:31
  • 1
    I suggest you strip it down to a bare MKMapView and set the relevant options on it. Skip the annotation and CLLocationManager stuff, event handling, etc. When you have that working, start adding the rest back, and when the problem reappears you have a better idea about where it is. – Jakob Borg Mar 14 '10 at 20:47
0

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
{
}
Van Du Tran
  • 6,736
  • 11
  • 45
  • 55