使用MapKit在地图上使用图钉显示自定义位置(Swift)

时间:2018-08-28 13:53:40

标签: swift annotations location mkmapview

我正在尝试在我的应用程序中创建一个视图,以显示餐厅的位置。当用户进入视图时,它会显示类似enter image description here

的内容

但是用图钉和放大的视图显示餐厅的位置。我是MapKit的新手,无法取得任何进展。

这就是我的观点。 enter image description here

这就是我的视图控制器的组成部分。

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 个答案:

答案 0 :(得分:1)

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

}

这应该做到。希望对您有所帮助!