2012-10-12 11 views
6

मैं इस तरह से मेरी नक्शे में व्याख्या जोड़ने:बदलने पिन रंग MKMapView

MyAnnotation *annotationPoint2 = [[MyAnnotation alloc] init]; 
annotationPoint2.coordinate = anyLocation; 
annotationPoint2.title = [NSString stringWithFormat:@"%@", obj]; 
annotationPoint2.subtitle = @""; //or set to nil 
annotationPoint2.keyValue = [NSString stringWithFormat:@"%@", key]; 
[mapPins addAnnotation:annotationPoint2]; 

पिंस सभी लाल हैं, और मैं उन सब को हरा करना चाहते हैं। मैं रंग कैसे बदल सकता हूं? मैं निम्नलिखित की कोशिश की है, लेकिन यह अभी भी एक लाल निशान देता है:

annotationPoint2.pinColor = MKPinAnnotationColorGreen; 

उत्तर

18
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
    { 
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"]; 
    annView.pinColor = MKPinAnnotationColorGreen; 
    return annView; 
    } 
+0

इस कोड काम करता है, लेकिन फिर भी वर्तमान उपयोगकर्ता स्थान हरे हो जाता है, भले ही मैं इसे हलकों यह दौर के साथ नीले रंग चाहते हैं। मैं उसे कैसे कर सकता हूँ? – Alessandro

+4

अगर ([[एनोटेशन शीर्षक] isqualToString है: @ "वर्तमान स्थान"]) { annView.pinColor = MKPinAnotationColorGreen; } अन्य {annView.pinColor = MKPinAnnotationColorRed;} – casillas

+0

@Alessandro आपको एनोटेशन == mapView.user स्थान को स्थानांतरित करने की आवश्यकता है जब उपयोगकर्ता स्थान के लिए नीली बिंदु और उसके आसपास के सर्कल को दिखाने के लिए स्थान – amitshinik

5

pinColor संपत्ति MKPinAnnotationView वर्ग (नहीं MKAnnotation प्रोटोकॉल) में परिभाषित किया गया है।

viewForAnnotation प्रतिनिधि विधि में आप MKPinAnnotationView बनाते हैं। यदि आपने उस प्रतिनिधि को लागू नहीं किया है, तो आपको डिफ़ॉल्ट रूप से मानक लाल पिन मिलते हैं।

उस प्रतिनिधि विधि में, आप MKPinAnnotationView का एक उदाहरण बनाते हैं और आप इसे pinColor हरे रंग में सेट कर सकते हैं।

1

Swift3 इस तरह से है:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") 

    if annotation.title! == "My Place" { 

     annotationView.pinTintColor = UIColor.green 

    } else { 

     annotationView.pinTintColor = UIColor.red 
    } 


    return annotationView 
}