Rvfvl
Rvfvl

Reputation: 383

Swift MapKit - Annotations dont show in the MapView

I am trying to create a MapView using MapKit framework which looks for the current user location and displays restaurants nearby. However, when I am trying to show annotations then they don't show up.

I have tried printing (response.mapItems) to check if it works and the result is good because it prints information about some restaurants that were found in the console.

Therefore, I don't know why these are not annotated on the map.

Here is the code that I've made:

import UIKit
import MapKit
import CoreLocation

class RestaurantViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!

let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations[0]

    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.025, 0.025)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

    map.setRegion(region, animated: true)

    self.map.showsUserLocation = true
}

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "restaurant"

    request.region = map.region

    let search = MKLocalSearch(request: request)
    search.start { (response, error) in

        guard let response = response else {
            return
        }

        for item in response.mapItems {
            print(response.mapItems) - Console shows some restaunrant outputs that were correctly fetched
            let annotation = MKPointAnnotation()
            annotation.coordinate = item.placemark.coordinate
            annotation.title = item.name

            DispatchQueue.main.async {
                self.map.addAnnotation(annotation)
            }

        }

    }
}

}

Upvotes: 0

Views: 1035

Answers (2)

Jacob King
Jacob King

Reputation: 6157

You annotation implementation is a little off, allow me to explain how it should be.

Adding annotations to your map relies on two things.

  1. A class conforming to MKAnnotation.
  2. A subclass of MKAnnotationView.

Your map has annotations added to it but it does not know how to show them, so it shows nothing. You can tell it how to show them by implementing viewForAnimation. Take a look at my example below:

class PinAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D

    init(coordinate: CLLocationCoordinate2D) {
        self.coordinate = coordinate
        super.init()
    }
}

class PinAnnotationView: MKAnnotationView {

    @available(*, unavailable)
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    init(annotation: PinAnnotation) {
        super.init(annotation: annotation, reuseIdentifier: nil)

        self.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

        self.image = UIImage(named: "mypinimage") // or whatever.
    }
}

Here we have the definition for the two objects I spoke of above.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if let imageAnnotation = annotation as? ImageAnnotation {
            let view = ImageAnnotationView(annotation: imageAnnotation)
            return view
        }

        return nil
    }

Here we have the implementation of viewForAnnotation that I spoke of above.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Implement viewForAnnotation

   func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?

see my demo here customPinAnnotationButton

Upvotes: 0

Related Questions