Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
}



}
like image 503
Yash Rraj Sood Avatar asked Nov 27 '25 18:11

Yash Rraj Sood


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!

like image 140
Starsky Avatar answered Nov 29 '25 11:11

Starsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!