Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: User location turns into pin

Tags:

ios

mkmapview

I have a mapView with a strange behavior: When I open it, everything works fine. The user (blue with circles) is going to be located and the 3 pins are in position. But (I don't know why) after a some time, the blue point turns into a pin - but only when I have slow connection speed.

Here's what I got:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

    if (!pinView && ![annotation isKindOfClass:[MKUserLocation class]])
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == self.locationZH1)
        {
            [pinView setTag:1];
        }
        else if (annotation == self.locationZH2)
        {
            [pinView setTag:2];
        }
        else if (annotation == self.locationZH3)
        {
            [pinView setTag:3];
        }
        else if (annotation == self.locationLU1)
        {
            [pinView setTag:4];
        }
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}
like image 505
filou Avatar asked Jul 03 '13 08:07

filou


2 Answers

Be sure to avoid providing an annotation view for the built in user location marker.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //check annotation is not user location
    if([annotation isEqual:[mapView userLocation]])
    {
        //bail
        return nil;
    }

    static NSString *annotationViewReuseIdentifer = @"map_view_annotation";

    //dequeue annotation view
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifer];

    if(!annotationView)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifer];
    }

    //set annotation view properties
    [annotationView setAnnotation:annotation];

    return annotationView;
}

By checking for the user location annotation earlier, you can provide an early out for returning nil rather than allocating a new MKPinAnnotationView and returning that instead.

The documentation for - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation states:

If the object in the annotation parameter is an instance of the MKUserLocation class, you can provide a custom view to denote the user’s location. To display the user’s location using the default system view, return nil.

like image 174
Zack Brown Avatar answered Oct 05 '22 08:10

Zack Brown


Swift 3 Solution :

if annotation is MKUserLocation{
    return nil
}
like image 31
Maor Avatar answered Oct 05 '22 06:10

Maor