iOS: User location turns into pin
Asked Answered
G

3

6

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;
}
Goddord answered 3/7, 2013 at 8:56 Comment(1)
Sometimes the mapView gets reload , in that time Your pinView.annotation already initialised , Return nil for Current Location.Hypochondriasis
A
11

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.

Atrophy answered 3/7, 2013 at 9:0 Comment(0)
T
6

Swift 3 Solution :

if annotation is MKUserLocation{
    return nil
}
Tael answered 28/4, 2017 at 14:30 Comment(0)
G
0

You need to return nil when annotation == mapView.userLocation to show the blue dot for user location and the circle around it

- (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];
        }
        return pinView;
    }
    else
    {
        pinView.annotation = annotation;
        return Nil;
    }
}
Guacharo answered 5/3, 2014 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.