change image pin maps

前端 未结 1 1584
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-25 03:42

What should I write in order to put a personal picture instead of the traditional red pin?

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnot         


        
1条回答
  •  情话喂你
    2021-01-25 04:19

    Use MKAnnotationView instead of MKPinAnnotationView, and then set its image property. I'd also suggest implementing the dequeue logic so that annotations can be reused:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
    
        let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin
    
        var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)
    
        if annotationView != nil {
            annotationView.annotation = annotation
        } else {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    
            annotationView.image = UIImage(named: "annotation.png")
    
            annotationView.canShowCallout = true
            annotationView.calloutOffset = CGPointMake(-8, 0)
    
            annotationView.autoresizesSubviews = true
            annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView
        }
    
        return annotationView
    }
    

    0 讨论(0)
提交回复
热议问题