2012-03-09 14 views
5

पर छवि जोड़ना मैं मानचित्र में अपनी टिप्पणियों में एक कस्टम छवि जोड़ना चाहता हूं। और मैं निम्नलिखित कस्टम MapAnnotationView बना दिया है:आईओएस: कस्टम एमकेएनोटेशनव्यू

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
@class POI; 

@interface MapAnnotation : MKAnnotationView <MKAnnotation > 

@property (nonatomic) CGFloat lat; 
@property (nonatomic) CGFloat lon; 
@property (nonatomic) CGFloat altitude; 
@property (nonatomic, copy) NSString * title; 
@property (nonatomic, copy) NSString * subtitle; 
@property (nonatomic,retain) NSString *source; 
@property (nonatomic,retain) UIImage *image; 

@end 

@implementation MapAnnotation 
@synthesize coordinate; 
@synthesize lat=_lat,lon=_lon,altitude= _altitude; 
@synthesize subtitle= _subtitle, title= _title, source=_source, image =_img; 


- (CLLocationCoordinate2D)coordinate;{ 
    CLLocationCoordinate2D position; 
    if (_lat != 0.0 && _lon != 0.0) { 
     position.latitude = _lat; 
     position.longitude = _lon; 

    }else { 
     position.latitude=0.0; 
     position.longitude=0.0; 
    } 

    return position; 
} 

@end 

-(void) mapDataToMapAnnotations{ 

    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10]; 
    for (id annotation in _map.annotations) 
     if (annotation != _map.userLocation) 
      [toRemove addObject:annotation]; 
    [_map removeAnnotations:toRemove]; 

    [_data removeAllObjects]; 

    [_data addObjectsFromArray:[UDdelegate naturArray]]; 


    if(_data != nil){ 
     MapAnnotation * tmpPlace; 
     //for(NSDictionary * poi in _data){ 


     for(POI* poi in _data){ 

      tmpPlace = [[MapAnnotation alloc]init]; 

      tmpPlace.title = [poi title]; 
      tmpPlace.lat = [poi lat]; 
      tmpPlace.lon = [poi lon]; 
      tmpPlace.subtitle = [poi dist]; 
      tmpPlace.image = [poi poiIcon]; 

      [self.map addAnnotation:tmpPlace]; 
      [_map setNeedsLayout]; 
     } 
    } 
} 

समस्या यह है कि पिन मानक redPin है .... मुझे यकीन है कि माउस रिक्त नहीं है हूँ, उस के लिए जाँच की है।

धन्यवाद

उत्तर

11

आप एक कस्टम दृश्य के साथ MapKit प्रतिनिधि विधि mapView:viewForAnnotation: की सेवा के लिए है।

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease]; 
    } 

    annotationView.image = [UIImage imageNamed:@"pin_image.png"]; 
    annotationView.annotation = annotation; 

    return annotationView; 
} 

संपुटित तरह तुमने किया था और अधिक आप एक कस्टम एनोटेशन दृश्य बनाने चाहिए और अपने वर्ग के साथ ऊपर प्रतिनिधि विधि की सेवा करने के लिए।

मैं आपको MapAnnotation कक्षा का नाम बदलने की सलाह देता हूं क्योंकि यह भ्रमित है। आईओएस में एनोटेशन भी हैं जो उन एनोटेशन विचारों के लिए डेटा धारक हैं। इसे हल करने के लिए मैं विरासत वर्ग के प्रकार को लिखना पसंद करूंगा, इस मामले में MKAnnotationView आपकी कस्टम कक्षा के अंत में। उदाहरण के लिए CustomPinAnnotationView

+0

मेरे मामले में उपर्युक्त प्रतिनिधि विधि को कॉल नहीं किया जाता है, यहां तक ​​कि मैंने <एमकेएएनोटेशन> प्रतिनिधि भी जोड़ा है। (और इसे mapView.delegate = self के रूप में भी असाइन किया गया है) – stack

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