2011-09-18 12 views
10

के लिए कस्टम छवि मैंने एक एनोटेशन बनाया है जिसे मैं एक एमकेमैपव्यू में जोड़ रहा हूं। मैं मानक लाल पिन की बजाय कस्टम छवि के बारे में कैसे जाउंगा?एमकेएनोटेशन

@interface AddressAnnotation : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    MKPinAnnotationColor pinColor; 
} 
@property (nonatomic,retain) NSString *title; 
@property (nonatomic,retain) NSString *subtitle; 
@property (nonatomic, assign) MKPinAnnotationColor pinColor; 
@end 

उत्तर

1

mapView:viewForAnnotation: प्रतिनिधि विधि को ओवरराइड करें। यदि annotation आपकी कस्टम एनोटेशन में से एक को पैरा पॉइंट्स देता है, तो अपनी पसंद के अनुसार एक कस्टम व्यू वापस करें।

+0

क्या आप मुझे ऐसा करने के उदाहरण के बारे में बता सकते हैं? मैं उद्देश्य सी के लिए नया हूँ। धन्यवाद – 3sl

18

MKMapView अपने प्रतिनिधि विधि mapView:viewForAnnotation: से अपने पिन बार देखा गया हो जाता है तो क्या आप के लिए:

  1. मानचित्र के प्रतिनिधि के रूप में आपके विचार नियंत्रक सेट करें।
  2. नक्शा लागू करें दृश्य: देखेंफोरअनोटेशन: आपके नियंत्रक में।

प्रतिनिधि

@interface MapViewController : UIViewController <MKMapViewDelegate> 

मार्क प्रतिनिधि प्रोटोकॉल के साथ इंटरफेस के रूप में अपने नियंत्रक सेट करें। यह आप इंटरफ़ेस बिल्डर (आईबी) में नियंत्रक को MKMapView के प्रतिनिधि के रूप में सेट करते हैं। अपने मानचित्र वाले .xib फ़ाइल को खोलें, MKMapView पर राइट क्लिक करें, और अपने नियंत्रक को delegate आउटलेट खींचें।
यदि आप आईबी के बजाय कोड का उपयोग करना पसंद करते हैं, तो नियंत्रक के दृश्यडिडलोड विधि में self.yourMapView.delegate=self; जोड़ें। नतीजा वही होगा।

MapView लागू: viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // this part is boilerplate code used to create or reuse a pin annotation 
    static NSString *viewId = @"MKPinAnnotationView"; 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*) 
     [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId]; 
    if (annotationView == nil) { 
     annotationView = [[[MKPinAnnotationView alloc] 
      initWithAnnotation:annotation reuseIdentifier:viewId] autorelease]; 
    } 
    // set your custom image 
    annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"]; 
    return annotationView; 
} 
0

standart MKPinAnnotationView कि समारोह - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation साथ MKAnnotationView उपयोग करने के लिए है करने के लिए एक ही रास्ता के कस्टम छवि सेट करने के लिए बजाय।

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

    if ([annotation isKindOfClass:[MKUserLocation class]]) { 
       return nil; 
    } 

    static NSString *identifier = @"Annotation"; 

    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (!aView) { 
      aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
      aView.image = [UIImage imageNamed:@"Untitled1.png"]; 
      aView.canShowCallout = YES; 
      aView.draggable = YES; 
    } else { 
      aView.annotation = annotation; 
    } 

    return pin; 
} 

aView.image मूल्य आप अपने स्वयं की छवि सेट कर सकते हैं के लिए: यहाँ उदाहरण है। और इसके साथ बेहतर संभाल करने के लिए MKAnnotationView कक्षा संदर्भ में भी देखें।

संबंधित मुद्दे