首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPinAnnotationView pinColor

MKPinAnnotationView pinColor
EN

Stack Overflow用户
提问于 2012-11-20 04:51:58
回答 1查看 3.2K关注 0票数 3

我不明白为什么(理论上)与MKPointAnnotation相关联的MKPinAnnotationView不会出现在地图上。事实上,大头针出现了,但它不是应该是紫色的……

代码如下:

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2012-11-20 07:01:27

如果您想要创建一个不同于默认红色图钉的注释视图,则必须在地图视图的viewForAnnotation委托方法中创建并返回它。

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

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

例如:

代码语言:javascript
复制
//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,则在接口生成器中建立连接。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13462007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档