MKPinAnnotationView 锚点颜色

3
我不明白为什么与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个回答

2
如果您想创建一个不同于默认红色图钉的注释视图,您需要在地图视图的viewForAnnotation委托方法中创建并返回它。
每当地图需要显示一些注释(无论是内置用户位置还是您添加的注释),地图都会自动调用viewForAnnotation委托方法。
从调用addAnnotation之前的本地创建myPersonalView,改为实现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中),或者如果myMapIBOutlet,则在Interface Builder中进行连接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接