2012-11-02 46 views
1

我正在製作一個使用MKMapView的應用程序。我添加自定義引腳(與圖像)。現在當我放大然後縮小時,引腳變回默認值(紅色)。當我放大和縮小MKMapView自定義引腳更改

這裏是我的代碼:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
    { 
     static NSString* SFAnnotationIdentifier = @"Kamera"; 
     MKPinAnnotationView* pinView = 
     (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier]; 
     if (!pinView) 
     { 
      MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:SFAnnotationIdentifier]; 
      annotationView.canShowCallout = NO; 

      UIImage *flagImage = [UIImage imageNamed:@"pinModer.png"]; 

      CGRect resizeRect; 

      resizeRect.size = flagImage.size; 
      resizeRect.size = CGSizeMake(40, 60); 
      resizeRect.origin = (CGPoint){0.0f, 0.0f}; 
      UIGraphicsBeginImageContext(resizeRect.size); 
      [flagImage drawInRect:resizeRect]; 
      UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
      annotationView.image = resizedImage; 
      annotationView.opaque = NO; 

      UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kameraNaprejModra.png"]]; 
      annotationView.leftCalloutAccessoryView = sfIconView; 

      return annotationView; 

    }  
    return nil; 
} 

回答

1

的代碼不處理,其中dequeue返回一個非空pinView(它重新使用先前的註解視圖之意)的情況。

如果pinView不是nil,則方法結束於最後一行,該行返回註釋視圖的nil

當您返回nil時,地圖視圖繪製默認的註釋視圖,它是一個紅色的針。


調整這樣的代碼:

if (!pinView) 
{ 
    //no changes to code inside this if 
    //... 
    return annotationView; 
} 
//add an else part and return pinView instead of nil... 
else 
{ 
    pinView.annotation = annotation; 
} 

return pinView; 
+0

謝謝,它的工作完美... – Gorazd