2

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

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

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

4

5 回答 5

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;


}
于 2012-12-12T05:31:05.403 回答
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;
}
于 2012-12-10T04:23:31.283 回答
1

试试这个人...

kingsolomonslodge.pinColor = AnnotationColorRed;

让我知道它是否有效

编码快乐!!!!

于 2012-12-10T04:15:46.477 回答
0

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

 MKAnnotationView *pinView = nil; 
pinView.image = [UIImage imageNamed:@"pinks.jpg"];
于 2012-12-10T11:41:44.310 回答
0

从 iOS 9 开始:

kingsolomonslodge.pinTintColor = UIColor.greenColor;
于 2019-08-29T06:15:59.713 回答