8

I am developing a custom pins in a MapView in my iPhone app. Here is the code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
 {
     static NSString *AnnotationViewID = @"annotationViewID";

     MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

     if (annotationView == nil)
     {
         annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
     }

    if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {

        MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
        //pin.pinColor = MKPinAnnotationColorRed;
        annotationView = pin;
    }
    else
    {
        NSString *pin = [NSString stringWithFormat:@"pin%i.png",[[annotation imgNumber] intValue]];
        annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [annotationView setRightCalloutAccessoryView:button];
        annotationView.annotation = annotation;
        annotationView.canShowCallout = YES;

    /********* Imagen a la izquierda en la burbuja *********/
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    // Set up the Left callout
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // Set the image for the button
    //[myDetailButton setImage:[UIImage imageNamed:@"botocorreu.png"] forState:UIControlStateNormal];
    NSString *detailButton = [NSString stringWithFormat:@"%i",[[annotation imgNumber] intValue]];
    [myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal];  
    // Set the button as the callout view
    annotationView.leftCalloutAccessoryView = myDetailButton;
    annotationView.canShowCallout = YES;
    }
     return annotationView;
  }

This works good but show me the users location with a red pin. I check the "show user location" in the IB. i would like to use the default blue point which will move it automatically when the user is moving, isn't correct? How can i do that?

Thanks a lot.

ValentiGoClimb
  • 750
  • 3
  • 13
  • 23
  • See this as well https://stackoverflow.com/a/68444109/9497800 . The code in the top of the viewForAnnotation method is simply `if annotation is MKUserLocation { return nil }` for Swift 5 – multitudes Jul 19 '21 at 16:40

1 Answers1

22

Add this to the top of the viewForAnnotation method:

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

The special user location annotation is of type MKUserLocation and returning nil in that case tells the map view to draw the default view for it which is the blue dot.

You can also remove these lines since they will no longer be needed:

if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")])  {
    MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    //pin.pinColor = MKPinAnnotationColorRed;
    annotationView = pin;
}
else
  • it works good, thnks. The ony thing is thaht the blue point which show the user location, makes a circle very big around him, is it costumizable? – ValentiGoClimb May 21 '11 at 15:22
  • The circle size indicates how accurate the location is (smaller means more accurate). Not sure if this can be controlled in the MKMapView using CLLocationManager. –  May 21 '11 at 16:15
  • ok thnks. And you know how to introduce manually by code the userlocation. I need to put the user in one place. thnks – ValentiGoClimb May 22 '11 at 18:10
  • 2
    Unfortunately, the animated blue dot view is not available for your own annotation view and you can't override the map view's user location coordinates. You can create your own annotation at a certain location but you can't make it use the blue dot animation. You'll have to draw it yourself or take a screenshot of the dot and use that as an image in your annotation view. –  May 22 '11 at 19:49