2012-03-22 12 views
54

मैं रॉक पिन के बजाय अपने MKMapView में एक छवि प्रदर्शित करना चाहता हूं। क्या कोई यहां कुछ सहायक कोड डाल सकता है, या इसे कैसे करना है, इस बारे में बताएं?एमकेमैप व्यू: एनोटेशन पिन के बजाय, एक कस्टम व्यू

धन्यवाद!

संपादित

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: 
(id <MKAnnotation>)annotation { 
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"com.invasivecode.pin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) pinView = [[MKPinAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

    pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.canShowCallout = YES; 
    pinView.animatesDrop = YES; 
    pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
} 
else { 
    [mapView.userLocation setTitle:@"I am here"]; 
} 
return pinView; 
} 

मैं मेरी छवि को pinks.jpg उम्मीद कर रहा हूँ के नक्शे पर होना करने के लिए, डिफ़ॉल्ट पिन दृश्य (रॉक पिन के आकार का) के बजाय स्थान pinning। लेकिन फिर भी मुझे पिन की डिफ़ॉल्ट छवि मिल रही है।

+0

लगता है जैसे आप एक कस्टम एनोटेशन चाहते हैं। [यह ट्यूटोरियल] जांचें (http://shawnsbits.com/blog/2011/04/12/custom-map-pins-for-mapkit/) बाहर। एक कस्टम एनोटेशन ट्यूटोरियल के लिए गुगलिंग आपको भी मिल जाएगी यदि वह आपके अनुरूप नहीं है। – Squatch

+0

मुझे लगता है कि एनोटेशन सिर्फ _note_ है जिसे _pinned_ स्थान के बारे में जानकारी बताने के लिए रखा गया है। लेकिन मैं पिन को कस्टमाइज़ करना चाहता हूं, जो अपनी छवि को कंपनी लोगो में बदल देता है। – turtle

+0

मैंने आपको _tutorial_ तरीके से भी आजमाया है, ऐसा लगता है कि यह मेरे लिए काम नहीं कर रहा है। ट्यूटोरियल कुछ 'UIImage'' के लिए 'MKAnotationView' ऑब्जेक्ट' की 'छवि' प्रॉपर्टी सेट कर रहा है। मैंने परीक्षण किया है, _pinView_ को मेरी असाइन की गई छवि में नहीं बदलता है। – turtle

उत्तर

112

जब आप एनोटेशन व्यू के लिए अपनी छवि का उपयोग करना चाहते हैं, तो आपको MKPinAnnotationView के बजाय MKAnnotationView बनाना चाहिए।

MKPinAnnotationView (कि क्या इसके लिए है) MKAnnotationView का एक उपवर्ग इसलिए यह एक image संपत्ति है, लेकिन यह आम तौर पर उस ओवरराइड करता है और एक पिन छवि खींचता है।

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

     //pinView.pinColor = MKPinAnnotationColorGreen; 
     pinView.canShowCallout = YES; 
     //pinView.animatesDrop = YES; 
     pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
    } 
    else { 
     [mapView.userLocation setTitle:@"I am here"]; 
    } 
    return pinView; 
} 


सूचना है कि animatesDrop भी टिप्पणी की है के बाद से है कि संपत्ति केवल MKPinAnnotationView में मौजूद है:

तो कोड को बदल जाते हैं।

यदि आप अभी भी अपनी छवि टिप्पणियां छोड़ना चाहते हैं, तो आपको एनीमेशन स्वयं करना होगा। आप "animatesdrop mkannotationview" के लिए स्टैक ओवरफ़्लो खोज सकते हैं और आपको कई उत्तरों मिलेंगे। यहाँ पहले दो हैं:

+0

हाय अन्ना, मैं सिर्फ इस पिन छवि के क्लिक पर एक ईवेंट जोड़ना चाहता हूं। मैं उसे कैसे कर सकता हूँ? – turtle

+0

हाय अन्ना, क्या आपको अपने दिमाग में निम्नलिखित प्रश्न के लिए कोई विचार है http://stackoverflow.com/questions/27489898/image-annotation-on-ios – casillas

+0

@Anna कृपया इस http://stackoverflow.com पर मेरी सहायता करें/प्रश्न/35647839/शो-डायनामिक-एनोटेशन-पिन-इन-मैप्यूव्यू –

17

यहाँ स्विफ्ट 3 पर एक जवाब है। यह एनोटेशन दृश्य यदि संभव हो तो dequeues या एक नया एक नहीं तो बनाता है:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !(annotation is MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 

स्विफ्ट 2.2:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !annotation.isKindOfClass(MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
     annotationView = av 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 
+0

धन्यवाद। आप 'एनोटेशन व्यू क्यों कहते हैं? .annotation = annotation'? मैंने सोचा कि एनोटेशन और दृश्य स्वचालित रूप से जुड़े हुए हैं? – Andrej

+1

वे स्वचालित रूप से जुड़े नहीं हैं। आप एक पुन: प्रयोज्य एनोटेशन दृश्य को हटा रहे हैं। यदि इसका इस्तेमाल पहले किया गया था तो संभवतः एक और 'एनोटेशन' जुड़ा हुआ है। –

0

मैं अन्ना के जवाब के साथ साथ सहमत हैं और मुझे लगता है कि कैसे दिखेगा दिखाना चाहते स्विफ्ट 3 में यह उत्तर कई अन्य विकल्पों के साथ है। छवि का आकार बदलने के समान, सरणी और ect से छवियों की एक सूची प्राप्त करें।

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if let annotation = annotation as? PetrolStation { 
      let identifier = "pinAnnotation" 
      var view: MKAnnotationView 
      if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
       as? MKPinAnnotationView { // 2 
       dequeuedView.annotation = annotation 
       view = dequeuedView 
      } else { 
       // 3 
       view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       view.canShowCallout = true 

       //here We put a coordinates where we like to show bubble with text information up on the pin image 
       view.calloutOffset = CGPoint(x: -7, y: 7) 


       //Here this is a array of images 
       let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace] 

       //Here we set the resize of the image 
       let size = CGSize(width: 30, height: 30) 
       UIGraphicsBeginImageContext(size) 
       pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
       let resizeImage = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       view.image = resizeImage 

       //Here we like to put into bubble window a singe for detail Informations 
       view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView 
       //Here we make change of standard pin image with our image 
       view.image = resizeImage 
      } 

      return view 
     } 
     return nil 
    } 
संबंधित मुद्दे