如何在地图工具包视图中更改注释的引脚颜色

时间:2012-12-10 04:13:16

标签: iphone objective-c ipad mapkit

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

    //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];

我已经搜索了几次这个问题,但我不确定如何将其实现到我当前的代码中。

5 个答案:

答案 0 :(得分:3)

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{


   if (annotation == mapview.userLocation)
    return nil;

    MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapview dequeueReusableAnnotationViewWithIdentifier: @"asdf"];

   if (pin == nil)
      pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
   else
      pin.annotation = annotation;

 //  NSLog(@"%@",annotation.title);

    NSString *titlename=@"xyz";
   if ([annotation.title isEqualToString:titlename]) {
       pin.pinColor = MKPinAnnotationColorGreen;
     // pin.image=[UIImage imageNamed:@"arrest.png"] ;
}
else{
    pin.pinColor= MKPinAnnotationColorPurple;
}

    pin.userInteractionEnabled = YES;
    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom];
//  //pin.image=[UIImage imageNamed:@"arrest.png"] ;



pin.rightCalloutAccessoryView = disclosureButton;
//pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;


}

答案 1 :(得分:2)

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

kingsolomonslodge.pinColor = MKPinAnnotationColorGreen;

另一种方法是使用viewForAnnotation:委托:

来使用图像而不是默认图钉
- (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;
}

答案 2 :(得分:1)

试试这个男人......

kingsolomonslodge.pinColor = AnnotationColorRed;

让我知道它是否有效

快乐的编码!!!!

答案 3 :(得分:0)

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

 MKAnnotationView *pinView = nil; 
pinView.image = [UIImage imageNamed:@"pinks.jpg"];

答案 4 :(得分:0)

从iOS 9开始:

kingsolomonslodge.pinTintColor = UIColor.greenColor;