简体   繁体   中英

MapKit doesn't show Blue Dot for Current Location

I am a newbie in MapKit for iPhone and tried to implement this, for some reason I can't see the current location blue dot, anyone else had this issue????

#import "DetailMapViewController.h"
#import "mapAnnotations.h"

@implementation DetailMapViewController

@synthesize inStock;

-(void)getlocation:(CLLocationCoordinate2D)loc
{
    location = loc;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.navigationItem.title = @"Street View";
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate=self;      
    //MKCoordinateRegion region;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 5000, 5000);

    mapAnnotations *ann = [[mapAnnotations alloc] init];
    ann.title = @"";
    ann.subtitle = @"";
    ann.coordinate = region.center;

    mapView.showsUserLocation = YES;
    [mapView addAnnotation:ann];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    [self.view insertSubview:mapView atIndex:0];
}


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    if (annotation == mapView.userLocation)
    {

        annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        if (annView != nil)
        {
            annView.annotation = annotation;
        }
        else
        {
            annView = [[[NSClassFromString(@"MKUserLocationView") alloc] initWithAnnotation:annotation reuseIdentifier:@"blueDot"] autorelease];


        }
    }

    if([inStock isEqual:@"yes"]){
        annView.pinColor = MKPinAnnotationColorGreen;
    } 
    if([inStock isEqual:@"no"]){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    if([inStock isEqual:@"unknown"]){

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
        [annView addSubview:imageView];




    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

- (void)dealloc {
    [super dealloc];
}


@end

The way the viewForAnnotation is currently written, it should actually crash when trying to show the current location because the "blue dot" annotation view doesn't have the pinColor or animatesDrop properties.

Try changing it to the following:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if ([annotation isKindOfClass:MKUserLocation.class]) {
        //user location view is being requested,
        //return nil so it uses the default which is a blue dot...
        return nil;
    }

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    if([inStock isEqual:@"yes"]){
        annView.pinColor = MKPinAnnotationColorGreen;
    } 
    if([inStock isEqual:@"no"]){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    if([inStock isEqual:@"unknown"]){
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
        [annView addSubview:imageView];
    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

On the simulator, the user's location will be Cupertino, CA, USA (a little south of San Francisco). If your own annotation is not within 5000 meters of that, you won't see the blue dot. You'll have to zoom out to see it.

self.mapView.showsUserLocation = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM