Swift - Add MKAnnotationView To MKMapView

痞子三分冷 提交于 2019-11-26 10:58:34

问题


I\'m trying to add MKAnnotationView to MKMapView but I can\'t do it… Can anyone help me?

Here is my code:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    var latitude:CLLocationDegrees = locationManager.location.coordinate.latitude
    var longitude:CLLocationDegrees = locationManager.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati, homeLong)
    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, theSpan)
    self.theMapView.setRegion(theRegion, animated: true)
    self.theMapView.mapType = MKMapType.Hybrid
    self.theMapView.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = \"Home\"
    myHomePin.subtitle = \"Bogdan\'s home\"
    self.theMapView.addAnnotation(myHomePin)

    var anView:MKAnnotationView = MKAnnotationView()
    anView.annotation = myHomePin
    anView.image = UIImage(named:\"xaxas\")
    anView.canShowCallout = true
    anView.enabled = true
}

回答1:


You need to implement the viewForAnnotation delegate method and return an MKAnnotationView from there.

Here's an example:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"xaxas")
        anView.canShowCallout = true
    }
    else {
        //we are re-using a view, update its annotation reference...
        anView.annotation = annotation
    }

    return anView
}

Remember you need to create a plain MKAnnotationView when you want to use your own custom image. The MKPinAnnotationView should only be used for the standard pin annotations.

Also note you should not try to access locationManager.location immediately after calling startUpdatingLocation. It may not be ready yet.

Use the didUpdateLocations delegate method.

You also need to call requestAlwaysAuthorization/requestWhenInUseAuthorization (see Location Services not working in iOS 8).




回答2:


1- your map view should delegate itself to your view controller 2- you should implement

func mapView(aMapView: MKMapView!, viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView!

this function is called from every annotation added to your Map

3- subclass your annotations to identify them in the viewForAnnotation method

4- in viewForAnnotation, add

if annotation.isKindOfClass(CustomMapPinAnnotation)
{
  // change the image here
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? YourSubclassedAnnotation
  pinView!.image = UIImage(contentsOfFile: "xyz")
}



回答3:


You have to show annotation, then you have to add the show annotation method:

let annotation = MKPointAnnotation()
mapVew.showAnnotations([annotation], animated: true)

annotation is an instance of MKPointAnnotation.



来源:https://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!