show users location blue point in iPhone MKMapView
Asked Answered
R

1

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.

Redingote answered 20/5, 2011 at 22:55 Comment(1)
See this as well https://mcmap.net/q/1322804/-why-does-mkmapview-show-user-39-s-location-just-as-a-pin . The code in the top of the viewForAnnotation method is simply if annotation is MKUserLocation { return nil } for Swift 5Gaby
S
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
Symphonious answered 21/5, 2011 at 3:21 Comment(4)
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?Redingote
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.Symphonious
ok thnks. And you know how to introduce manually by code the userlocation. I need to put the user in one place. thnksRedingote
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.Symphonious

© 2022 - 2024 — McMap. All rights reserved.