2012-03-22 68 views
54

我想顯示在我的MKMapView而不是搖桿的圖像。有人可以在這裏提供一些有用的代碼,或告訴方式如何做?MKMapView:而不是註釋銷,自定義視圖

謝謝!

編輯

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: 
(id <MKAnnotation>)annotation { 
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"com.invasivecode.pin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) pinView = [[MKPinAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

    pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.canShowCallout = YES; 
    pinView.animatesDrop = YES; 
    pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
} 
else { 
    [mapView.userLocation setTitle:@"I am here"]; 
} 
return pinView; 
} 

我期待我的形象pinks.jpg是在地圖上,釘扎的位置,而不是默認引腳視圖(巖腳形)。但是我仍然得到了引腳的默認圖像。

+0

聽起來就像你想要一個自定義註釋。查看[本教程](http://shawnsbits.com/blog/2011/04/12/custom-map-pins-for-mapkit/)。使用Google自定義註釋教程也會讓你去,如果這不適合你。 – Squatch 2012-03-22 01:34:18

+0

我猜Annotation只是用於說明有關_pinned_位置信息的_note_。但是我想定製針腳,將其圖像更改爲公司徽標。 – turtle 2012-03-22 01:39:30

+0

我也曾試過你_tutorial_的方式,看來它不適合我。教程將'MKAnnotationView'對象的'image'屬性設置爲某個'UIImage'。我已經測試過,不會將_pinView_更改爲我分配的圖像。 – turtle 2012-03-22 01:49:22

回答

112

如果要將自己的圖像用於註釋視圖,應該創建一個MKAnnotationView而不是MKPinAnnotationView

MKPinAnnotationViewMKAnnotationView子類,所以它有一個image屬性,但它通常會覆蓋並繪製一個圖釘圖像(這就是它是)。

所以更改代碼:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

     //pinView.pinColor = MKPinAnnotationColorGreen; 
     pinView.canShowCallout = YES; 
     //pinView.animatesDrop = YES; 
     pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
    } 
    else { 
     [mapView.userLocation setTitle:@"I am here"]; 
    } 
    return pinView; 
} 


注意animatesDrop也註釋掉,因爲該屬性只有在MKPinAnnotationView存在。

如果您仍然希望圖像註釋掉落,您必須自己動畫。你可以在Stack Overflow中搜索「animatesdrop mkannotationview」,你會發現幾個答案。下面是前兩個條件:

+0

嗨安娜,我只想添加一個事件點擊這個圖像。我怎樣才能做到這一點? – turtle 2012-05-25 05:23:17

+0

嗨安娜,你有任何想法在你的心中爲以下問題http://stackoverflow.com/questions/27489898/image-annotation-on-ios – casillas 2014-12-15 18:05:38

+0

@安娜請幫助我在這個http://stackoverflow.com/questions/35647839/show-dynamic-annotation-pin-in-mapview – 2016-02-26 09:54:19

17

這裏有斯威夫特3答案。它會轉移如果可能的註解視圖,或創建如果不是一個新問題:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !(annotation is MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 

雨燕2.2:我與安娜的答案達成一致

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !annotation.isKindOfClass(MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
     annotationView = av 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 
+0

謝謝。你爲什麼要調用'annotationView?.annotation = annotation'?我認爲註釋和視圖是自動鏈接的? – Andrej 2017-04-29 10:41:58

+1

他們沒有自動鏈接。您將出現可重用的註釋視圖。如果它在使用之前可能有另一個「註釋」鏈接。 – 2017-05-02 08:03:57

0

,我喜歡展示如何看的是在swift3。這個答案有很多其他選項。像調整圖像大小,從數組和圖像中獲取圖像列表。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if let annotation = annotation as? PetrolStation { 
      let identifier = "pinAnnotation" 
      var view: MKAnnotationView 
      if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
       as? MKPinAnnotationView { // 2 
       dequeuedView.annotation = annotation 
       view = dequeuedView 
      } else { 
       // 3 
       view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       view.canShowCallout = true 

       //here We put a coordinates where we like to show bubble with text information up on the pin image 
       view.calloutOffset = CGPoint(x: -7, y: 7) 


       //Here this is a array of images 
       let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace] 

       //Here we set the resize of the image 
       let size = CGSize(width: 30, height: 30) 
       UIGraphicsBeginImageContext(size) 
       pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
       let resizeImage = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       view.image = resizeImage 

       //Here we like to put into bubble window a singe for detail Informations 
       view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView 
       //Here we make change of standard pin image with our image 
       view.image = resizeImage 
      } 

      return view 
     } 
     return nil 
    }