2009-11-26 11 views
6

मैं अपना नक्शा एनोटेट कर रहा हूं और एक छवि बस ठीक कर रहा हूं, लेकिन जब मैपव्यू पर एनोटेशन टैप करता हूं, तो छवि मेरी कस्टम छवि से लाल पिन पर जाती है। ऐसा क्यों है?एक कस्टम MKMapView एनोटेशन छवि स्पर्श पर गायब क्यों होती है?

- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id)newAnnotation { 
    MKPinAnnotationView *annotation = [[MKPinAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"]; 
    if (annotation == nil) { 
     annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"]; 
    } 

    annotation.image = [UIImage imageNamed:@"anno.png"]; 
    annotation.canShowCallout = YES; 
    annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]]; 
    annotation.leftCalloutAccessoryView = imgView; 

    return annotation; 
}

मेरा कोड कुछ नमूना कोड के समान दिखता है जो इस समस्या का उत्पादन नहीं करता है।

उत्तर

17

यहां मेरे अपने प्रश्न का उत्तर दें, बस अगर अन्य लोगों के पास एक ही समस्या है। ध्यान दें कि मैं "एमकेपीएनएनोटेशन व्यू" का उपयोग कर रहा हूं - इसे "एमकेएनोटेशन व्यू" में बदला जाना चाहिए और सबकुछ काम करता है।

फिक्स्ड कोड:

- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id)newAnnotation { 
    MKAnnotationView *annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"]; 

    if (annotation == nil) { 
     annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"]; 
    } 

    annotation.image = [UIImage imageNamed:@"anno.png"]; 
    annotation.canShowCallout = YES; 
    annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]]; 
    annotation.leftCalloutAccessoryView = imgView; 

    return annotation; 
} 
+0

धन्यवाद धन्यवाद .. –

0

मुझे लगता है कि यह सबसे अच्छा उपाय है:

(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    NSLog(@"welcome into the map view annotation"); 
    MyAnnotation* myAnnotation1=annotation; 

    // if it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

    if([mapView isEqual:mapMainView]== NO) 
    { 
     return nil; 
    } 
    // try to dequeue an existing pin view first 
    // if ([annotation isKindOfClass:[myAnnotation1 class]]) 
    // { 

    // try to dequeue an existing pin view first 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 

    MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 

    if (nil == pinView) 
    { 
     MKAnnotationView* annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 

     reuseIdentifier:AnnotationIdentifier] autorelease]; 
     // annotationView.canShowCallout = YES; 
     // pinView.animatesDrop=YES; 
     //pinView.pinColor=MKPinAnnotationColorPurple; 

     //image 
     UIImage* flagImage = [UIImage imageNamed:@"map_marker_over2.png"]; 

     CGRect resizeRect; 

     resizeRect.size = flagImage.size; 
     CGSize maxSize = CGRectInset(self.view.bounds, 
     [MapPropertyViewController annotationPadding], 
     [MapPropertyViewController annotationPadding]).size; 
     maxSize.height -= self.navigationController.navigationBar.frame.size.height + [MapPropertyViewController calloutHeight]; 
     if (resizeRect.size.width > maxSize.width) 
     resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height/resizeRect.size.width * maxSize.width); 
     if (resizeRect.size.height > maxSize.height) 
     resizeRect.size = CGSizeMake(resizeRect.size.width/resizeRect.size.height * maxSize.height, maxSize.height); 

     resizeRect.origin = (CGPoint){0.0f, 0.0f}; 
     UIGraphicsBeginImageContext(resizeRect.size); 
     [flagImage drawInRect:resizeRect]; 
     UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     annotationView.image = resizedImage; 
     annotationView.opaque = NO; 

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

     profileIconView = [[UIImageView alloc]initWithFrame:CGRectMake(18.5f, 10.0f, 37.0f, 30.0f)]; 
     annotationView.leftCalloutAccessoryView = profileIconView; 
     //image 
     img = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:[arrImages objectAtIndex:myAnnotation1.tagAnnotation]]]]; 
     NSLog(@"Annotation Click %d",[arrImages count]); 
     NSLog(@"image %@",img); 
     profileIconView.image=img; 

     [profileIconView release]; 

     [annotationView setEnabled:YES]; 
     [annotationView setCanShowCallout:YES]; 
     return annotationView; 
    } 
    return pinView; 
    // } 
    // return nil; 
} 
+2

क्या आप कुछ स्पष्टीकरण प्रदान कर सकते हैं? यह भविष्य के पाठकों की मदद करेगा। –

0
Try this if you want to set both Source to Destination pin images different. 

    MKPointAnnotation * sourceAnno= [[MKPointAnnotation alloc] init]; 
    sourceAnno.coordinate=CLLocationCoordinate2DMake(YourSourcelatitude,YourSourcelongitude); 
    [sourceAnno setAccessibilityLabel:@“Source”]; 
    sourceAnno.title = @“You”; 
    [mapview addAnnotation: sourceAnno]; 

    DestinationAnno = [[MKPointAnnotation alloc] init]; 
    DestinationAnno.coordinate = CLLocationCoordinate2DMake(YourDestinationlatitude,YourDestinationlongitude); 
    [DestinationAnno setAccessibilityLabel:@“Destination”]; 
    DestinationAnno.title [email protected]“Destination”; 
    [mapview addAnnotation: DestinationAnno]; 

    In Delegate Method 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPointAnnotation *PointAnno=(MKPointAnnotation *)annotation; 
    NSString * kPinAnnotationIdentifier = PointAnno.accessibilityLabel; 
    MKAnnotationView *AnnotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier]; 

    if ([kPinAnnotationIdentifier isEqualToString:@"Source"]) 
    { 
     AnnotationView.centerOffset = CGPointMake(8, -14); 
     AnnotationView.calloutOffset = CGPointMake(-8, 0); 
     AnnotationView.image = [UIImage imageNamed:@"pin_blue.png"]; 
     AnnotationView.canShowCallout = YES; 
    } 
    else if([kPinAnnotationIdentifier isEqualToString:@"Destination"]) 
    { 
     AnnotationView.centerOffset = CGPointMake(8, -14); 
     AnnotationView.calloutOffset = CGPointMake(-8, 0); 
     AnnotationView.image = [UIImage imageNamed:@"pin_red.png"]; 
     AnnotationView.canShowCallout = YES; 
    } 
    return AnnotationView; 
} 

के लिए इसे मेरा समय solution..saved आप

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