Show custom location with pin on Map with MapKit (Swift)

Viewed 3157

I am trying to create a view for in my app which shows the location of a restaurant. When the user enters the view, it shows something like this enter image description here

But instead shows the location of the restaurant with a pin and a more zoomed in view. I am new to MapKit and have not been able to make any progress.

This is what my view consists of. enter image description here

And this is what my view controller consists of.

import UIKit
import MapKit

class FD1ViewController: UIViewController {


@IBOutlet weak var mapView: MKMapView!
private let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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



}
1 Answers
import UIKit
import MapKit

class FD1ViewController: UIViewController {

   @IBOutlet weak var mapView: MKMapView!

   override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      //Create the pin location of your restaurant(you need the GPS coordinates for this)
      let restaurantLocation = CLLocationCoordinate2D(latitude: 111.0, longitude: 111.0)

      //Center the map on the place location
      mapView.setCenter(restaurantLocation, animated: true)
   }

}

This should do it. I hope it helps!

Related