MapKit不显示当前位置的蓝点

时间:2010-10-29 21:16:42

标签: iphone mapkit core-location

我是MapKit for iPhone的新手,并试图实现这一点,由于某些原因我看不到当前位置蓝点,其他人都有这个问题????

#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

2 个答案:

答案 0 :(得分:8)

当前编写viewForAnnotation的方式,在尝试显示当前位置时实际应该崩溃,因为“蓝点”注释视图没有pinColor或animatesDrop属性。

尝试将其更改为以下内容:

- (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;
}

在模拟器上,用户的位置将是美国加利福尼亚州库比蒂诺(位于旧金山南部)。如果您自己的注释不在5000米范围内,您将看不到蓝点。你必须缩小才能看到它。

答案 1 :(得分:2)

self.mapView.showsUserLocation = YES;
相关问题