MKPinAnnotationView pinColor

时间:2012-11-19 20:51:58

标签: ios mapkit mkpinannotationview

我无法弄清楚为什么与MKPointAnnotation关联(理论上)的MKPinAnnotationView没有出现在地图上。事实上,引脚出现但它不应该是紫色的......

以下是代码:

MKPointAnnotation *myPersonalAnnotation= [[MKPointAnnotation alloc]init];
myPersonalAnnotation.title= [appDelegate.theDictionary objectForKey:@"theKey"];
myPersonalAnnotation.coordinate=CLLocationCoordinate2DMake(6.14, 10.7);
MKPinAnnotationView *myPersonalView=[[MKPinAnnotationView alloc] initWithAnnotation:myPersonalAnnotation reuseIdentifier:@"hello"];
myPersonalView.pinColor=MKPinAnnotationColorPurple;
[myMap addAnnotation:myPersonalAnnotation];

1 个答案:

答案 0 :(得分:2)

如果要创建与默认红色引脚不同的注释视图,则必须在地图视图的viewForAnnotation委托方法中创建并返回它。

只要需要显示一些注释(内置用户位置或您添加的注释),地图就会自动调用viewForAnnotation委托方法。

在调用myPersonalView之前删除addAnnotation的本地创建,然后实施viewForAnnotation方法。

例如:

//in your current method...
MKPointAnnotation *myPersonalAnnotation= [[MKPointAnnotation alloc]init];
myPersonalAnnotation.title= [appDelegate.theDictionary objectForKey:@"theKey"];
myPersonalAnnotation.coordinate=CLLocationCoordinate2DMake(6.14, 10.7);
[myMap addAnnotation:myPersonalAnnotation];

//...

//add the viewForAnnotation delegate method...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //if annotation is the user location, return nil to get default blue-dot...
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //create purple pin view for all other annotations...
    static NSString *reuseId = @"hello";

    MKPinAnnotationView *myPersonalView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (myPersonalView == nil)
    {
        myPersonalView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        myPersonalView.pinColor = MKPinAnnotationColorPurple;
        myPersonalView.canShowCallout = YES;
    }
    else
    {
        //if re-using view from another annotation, point view to current annotation...
        myPersonalView.annotation = annotation;
    }

    return myPersonalView;
}


确保设置了地图视图的delegate属性,否则将不会调用委托方法 在代码中,如果myMap.delegate = self;viewDidLoad,请使用myMap(例如在IBOutlet中)或在Interface Builder中建立连接。