2012-12-10 27 views
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]; 

我已经GOOGLE了这个问题几次,但我不确定如何实现这个到我有当前的代码。

回答

1

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

kingsolomonslodge.pinColor = AnnotationColorRed; 

让我知道它是否是workihg与否

编码快乐!!!!

+0

您好Nirav,感谢您的时间。 kingsolomonslodge.pinColor = AnnotationColorRed;它给了我一个错误,并问我是否要将其更改为kingsolomonslodge.pinColor = MKPinAnnotationColorRed ;.但是,当我改变它..它说它的属性别针颜色没有找到。 – Apps

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; 
} 
+0

谢谢Midhun MP!我也会查看自定义图像引脚。 – Apps

+0

@Apps:带着快乐的家伙:) –

+0

kingsolomonslodge.pinColor = AnnotationColorGreen;给我一个错误。它要求我用MKPinAnnotationColorGreen替换它;然而这也行不通。我只是玩弄它..刚下班回家。 – Apps

0

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

MKAnnotationView *pinView = nil; 
pinView.image = [UIImage imageNamed:@"pinks.jpg"]; 
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 *[email protected]"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; 


}