Search code examples
iosswiftmapkitmkmapviewdelegate

MapKit - Swift Example


I'm trying work with MapKit in Swift. I need to show the area of ​​the map the current User Location and point of interest next to it, however these points of interest must have a different pattern of visualization User itself. I know that there is the need to implement the delegate of MapViewAnnotation, put in my code it does not run. Could someone help me with an example?

This is my code.

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()


        let latArray = [-23.528657, -23.518514, -23.533796, -23.533796]
        let longArray = [-46.484008, -46.486495, -46.495533, -46.476690]

        var lat: CLLocationDegrees = -23.527096772791133
        var long: CLLocationDegrees = -46.48964569157911

        var latDelta: CLLocationDegrees = 0.01
        var longDelta: CLLocationDegrees = 0.01

        var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta)

        var mypos: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat,long)

        var myreg: MKCoordinateRegion = MKCoordinateRegionMake(mypos, theSpan)

        self.mapView.setRegion(myreg, animated: true)

        var myposannot = MKPointAnnotation()

        myposannot.coordinate = mypos
        myposannot.title = "Me"
        myposannot.subtitle = "I am here!"

        self.mapView.addAnnotation(myposannot)

        for var i=0; i<4; ++i {

            var latCli: CLLocationDegrees = latArray[i]
            var longCli: CLLocationDegrees = longArray[i]

            var myposCli : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latCli,longCli)

            var myposannotCli = MKPointAnnotation()

            myposannotCli.coordinate = myposCli
            myposannotCli.title = "Cliente" + " - " + String (i)
            myposannotCli.subtitle = "Anotacao" + " - " + String (i)


            self.mapView.addAnnotation(myposannotCli)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func mapViewAnnot(mapViewAnnot: MKMapView!,ViewForAnnotation annotation: MKAnnotation!) ->MKAnnotationView{

        if annotation is MKUserLocation{
            return nil
        }

        let reuseId = "pin"

        var pinView = mapViewAnnot.dequeueReusableAnnotationViewWithIdentifier(reuseId)   as? MKPinAnnotationView

        if(pinView == nil){
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red
            pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        } else {
            pinView!.annotation = annotation
        }
        return pinView!
    }

}

Solution

  • There are two main problems with the code:

    1. The viewForAnnotation delegate method is not named correctly and so the map view will not call it. The method declaration which is currently this:

      func mapViewAnnot(mapViewAnnot: MKMapView!,
          ViewForAnnotation annotation: MKAnnotation!) ->MKAnnotationView {
      

      is wrong. It should be this instead:

      func mapView(mapView: MKMapView!, 
          viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
      

      The method must be named mapView(mapView:viewForAnnotation:).

    2. The other problem is this line:

      var pinView = mapViewAnnot.dequeueReusableAnnotationViewWithIdentifier(reuseId)   as? MKPinAnnotationView
      

      The reference to some object named mapViewAnnot is meaningless and must be preventing the code from compiling. The line should be this:

      var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView