2013-02-21 43 views
5

में विभिन्न पिन छवियों को दिखाता है मेरे एमके मैप दृश्य में मैंने एक छवि के साथ एनोटेशन पिन को अनुकूलित किया है। लेकिन अभी भी कुछ पिन स्थिर हैं और दी गई छवि नहीं दिखा रहे हैं।MKMapview

मैं उपयोग कर रहा हूँ - (MKAnnotationView *) MapView: (MKMapView *) MapView viewForAnnotation: (आईडी) एनोटेशन पिन छवि स्थापित करने के लिए।

यहाँ मेरी कोड और स्क्रीन जोड़ना:

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


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

static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] 
           initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 
pinView.animatesDrop=YES; 
pinView.canShowCallout=YES; 
    pinView.pinColor= MKPinAnnotationColorGreen; 

    pinView.enabled = YES; 
    pinView.canShowCallout = YES; 
    pinView.image=[UIImage imageNamed:@"bublerest.png"]; //here I am giving the image 





UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
[rightButton addTarget:self 
       action:@selector(showDetails:) 
     forControlEvents:UIControlEventTouchUpInside]; 

    pinView.rightCalloutAccessoryView = rightButton; 

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rest_image2.png"]]; 
pinView.leftCalloutAccessoryView = profileIconView; 


return pinView; 
} 

enter image description here

कोई भी विचार?

+0

क्या आप किसी भी पिन के लिए अपने कस्टम मानचित्र आइकन देख सकते हैं या बस कुछ पिन उन्हें नहीं दिखाते हैं? – Zhang

उत्तर

11

यदि आप सेब "पिन" की तुलना में MKAnnotationView पर एक अलग छवि चाहते हैं, तो आपको MKPinAnnotationView के बजाय MKAnnotationView का उपयोग करना होगा।

एमकेपीएनएनोटेशन व्यू इस तरह से अनुकूलित करने के लिए नहीं है और समय-समय पर पिन दिखाएगा। Class Reference दिखाता है कि केवल pinColor और animatesDrop को बदला जाना चाहिए।

हालांकि, आप PinAnnotationView की एनीमेशन और छाया खो देंगे।

+1

जबकि अन्य उत्तर यहां सभी छवि को अनुकूलित करने के तरीके प्रदान करते हैं, केवल यह वास्तव में बताता है कि समस्या क्या है और इसे कैसे ठीक किया जाए। – Craig

+0

आईओएस 9 में मुझे यह समस्या मिली (मेरी पिन छवि के बजाय ऐप्पल पिन छवि)। आपके सुझाव के बाद अब ठीक काम करता है! धन्यवाद – Fulkron

+0

यदि आप एक ही मानचित्र पर ऐप्पल पिन (आईपिन?) और अपनी खुद की कस्टम छवि शो दोनों चाहते हैं - एक कस्टम एनोटेशन का उपयोग करें और एनोटेशन दृश्य (नियमित या पिन) के "प्रकार" के रूप में एक संकेतक जोड़ें। फिर प्रकार के आधार पर एक एनोटेशन व्यू या पिन एन्नोटेशन व्यू को हटाएं, और दो मामलों को अलग से संभाल लें। – Larry

1

एक नमूना स्निपेट पोस्ट करना जो मैंने हाल ही में प्रोजेक्ट में उपयोग किया था। उम्मीद है की यह मदद करेगा।

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

Annotation *annotationInst = (Annotation*)annotation; 

MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"pinId"; 
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 

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


    pinView.canShowCallout = YES; 
    pinView.image = [UIImage imageNamed:LOCATION_BUBBLE]; 

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    pinView.rightCalloutAccessoryView = rightButton; 
}  
return pinView;  
} 
1

viewForAnnotation

if ([annotation isKindOfClass:[MyAnnotation class]]) { 
    // try to dequeue an existing pin view first 
    static NSString* myAnnotationIdentifier = @"MyAnnotationIdentifier"; 

    // If an existing pin view was not available, create one 
    MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier]; 

    if ([(MyAnnotation *)annotation ann] == AnnotationTypeStart) { 
     NSLog(@"In Start"); 
     customPinView.enabled = NO; 
     customPinView.image = [UIImage imageNamed:@"begin.png"]; 
    } 
} 

में इस का प्रयोग करें जहां MyAnnotation के रूप में नीचे

typedef enum AnnotationType { 
    AnnotationTypeStart, 
    AnnotationTypeEnd, 
    AnnotationTypeWayPoint, 
} AnnotationType; 

@interface MyAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    AnnotationType ann; 
} 
2

MKAnnotation प्रोटोकॉल जो पिन जैसे

स्थापित करने के लिए उपयोग किया जाएगा में एक प्रॉपर्टी बनाएं कार्यान्वित किया जाता है
@interface AddressAnnotation1 : NSObject<MKAnnotation> { 
} 
@property (nonatomic, retain) NSString *mPinColor; 
,210

मीटर फ़ाइल कार्यान्वयन फ़ाइल

- (NSString *)pincolor{ 
    return mPinColor; 
} 

- (void) setpincolor:(NSString*) String1{ 
    mPinColor = String1; 
} 

जब आप annotation जोड़ने में, pin रंग भी निर्धारित किया है।

#pragma mark annotation delegate 
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation1 *) annotation 
{ 
    UIImage *anImage = nil; 

    MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"]; 
    if(annView==nil){ 
     annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease]; 
    } 
    if([annotation.mPinColor isEqualToString:@"green"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"red"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 01.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"blue"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]]; 
    } 
    annView.image = anImage; 
return annView; 
} 

आप पिन के लिए कस्टम छवियों

annView.pinColor = MKPinAnnotationColorGreen; 

या

साथ सशर्त बयान के शरीर की जगह नहीं करना चाहते हैं
annView.pinColor = MKPinAnnotationColorRed; 

या

annView.pinColor = MKPinAnnotationColorPurple; 

मैं इस को लागू किया है पहले। तो इसके साथ जाओ।

+0

धन्यवाद, यह अच्छा विचार है :) –

+0

यह कोड ठीक काम करना चाहिए, लेकिन मेरी परियोजना में यह काम नहीं कर रहा है। मैं मानचित्र में केएमएल भी लोड कर रहा हूं। –