Iphone 如何在地图套件视图中更改注释的pin颜色

Iphone 如何在地图套件视图中更改注释的pin颜色,iphone,objective-c,ipad,mapkit,Iphone,Objective C,Ipad,Mapkit,我想知道如何更改地图套件中注释的pin颜色。目前我正在使用以下代码 //King Solomon's Lodge No. 194 CLLocationCoordinate2D kingsolomons; kingsolomons.latitude = 38.052041; kingsolomons.longitude = -78.683218; Annotation *kingsolomonslodge = [[Annotation alloc] init]; kingsolomonsl

我想知道如何更改地图套件中注释的pin颜色。目前我正在使用以下代码

    //King Solomon's Lodge No. 194
CLLocationCoordinate2D kingsolomons;
kingsolomons.latitude = 38.052041;
kingsolomons.longitude = -78.683218;
Annotation *kingsolomonslodge = [[Annotation alloc] init];
kingsolomonslodge.coordinate = kingsolomons;
kingsolomonslodge.title = @"King Solomon's Lodge No. 194";
kingsolomonslodge.subtitle = @"Charlottesville, VA";
[self.myMapView addAnnotation:kingsolomonslodge];
我在谷歌上搜索了好几次这个问题,但我不确定如何在我现有的代码中实现它

试试这个男人

kingsolomonslodge.pinColor = AnnotationColorRed;
让我知道它是否有效


编码快乐

如果
MKAnnotation
class,则可以使用pinColor属性

kingsolomonslodge.pinColor = MKPinAnnotationColorGreen;
另一种方法是,您可以使用图像而不是默认pin,方法是使用
viewForAnnotation:
delegate:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[yourAnnotationLocation class]])
    {

        MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"yourImage.png"];//here we use a nice image instead of the default pins
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        else
        {
            annotationView.annotation = annotation;
        }
        return annotationView;
    }

return nil;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
静态NSString*标识符=@“MyLocation”;
if([annotation IsKindof类:[yourAnnotationLocation类]])
{
MKAnnotationView*annotationView=(MKAnnotationView*)[\u地图视图出列可重用annotationView with identifier:identifier];
如果(注释视图==nil)
{
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
annotationView.enabled=是;
annotationView.canShowCallout=是;
annotationView.image=[UIImage ImageName:@“yourImage.png”];//这里我们使用一个漂亮的图像,而不是默认的管脚
annotationView.rightCalloutAccessoryView=[UIButton Button,类型:UIButtonTypedTailDisclosure];
}
其他的
{
annotationView.annotation=注释;
}
返回注释视图;
}
返回零;
}

如果要应用自定义颜色,还可以添加图像

 MKAnnotationView *pinView = nil; 
pinView.image = [UIImage imageNamed:@"pinks.jpg"];
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if(注释==mapview.userLocation)
返回零;
MKPinAnnotationView*引脚=(MKPinAnnotationView*)[mapview出列可重用注释视图,标识符:@“asdf”];
如果(引脚==零)
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“asdf”]自动释放];
其他的
pin.annotation=注释;
//NSLog(@“%@”,注释.标题);
NSString*标题名=@“xyz”;
if([annotation.title isEqualToString:titlename]){
pin.pinColor=MKPinAnnotationColorGreen;
//pin.image=[UIImage ImageName:@“restart.png”];
}
否则{
pin.pinColor=MKPinAnnotationColorPurple;
}
pin.userInteractionEnabled=是;
UIButton*披露按钮=[UIButton Button类型:UIButtonTypeCustom];
////pin.image=[UIImage ImageName:@“restrip.png”];
pin.rightCalloutAccessoryView=披露按钮;
//pin.pinColor=MKPinAnnotationColorRed;
pin.animatesDrop=是;
[引脚设置已启用:是];
[pin设置CanShowCallout:是];
回位销;
}
自iOS 9以来:

kingsolomonslodge.pinTintColor = UIColor.greenColor;

谢谢你Midhun议员!我还将查看自定义图像pin。@Apps:with-journey dude:)Kingsolomonsloge.pinColor=AnnotationColorGreen;给我一个错误。它要求我用MKPinAnnotationColorGreen替换它;然而,这也不起作用。。我来玩玩它。。刚下班回家。请注意,在iOS 9中,
annotationView.pinColor=MKPinAnnotationColorGreen
已被弃用,取而代之的是
annotationView.pinTintColor=[MKPinAnnotationView greenPinColor]
您好,谢谢您的时间。KingsSolomonsLodge.pinColor=注释彩色;它给了我一个错误,并询问我是否要将其更改为KingsSolomonsLodge.pinColor=MKPinAnnotationColorRed;。但是当我改变它的时候。。它说没有找到它的属性pin颜色。
kingsolomonslodge.pinTintColor = UIColor.greenColor;