更改地图坐标(MapKit)

时间:2016-04-08 07:42:01

标签: ios swift mapkit

我想用personnal coordonate更改地图的位置。 (当我说“personnal”时,不是“用户位置”,而只是随机的coordonate)。

这是我的MapView控制器:

class MapSubViewController: UIViewController {

  @IBOutlet weak var mapView: MKMapView!

  let regionRadius: CLLocationDistance = 1000

  override func viewDidLoad() {
    super.viewDidLoad()
    self.mapLocationUpdate(70.0, longitude: 70.0, title: "")
  }

  func mapLocationUpdate(latitude: Double, longitude: Double, title: String) {
    let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    self.centerMapOnLocation(location, title: title)
  }

  func centerMapOnLocation(location: CLLocationCoordinate2D, title: String) {
    let coordinateRegion = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    self.mapView.setRegion(coordinateRegion, animated: false)
    let annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = title
    self.mapView.addAnnotation(annotation)
  }

}

当我想要更改地图的位置时,我告诉我的函数“mapLocationUpdate”:

例如:

  func addMapView(xPosition: Double, yPosition: Double, title: String) {
    self.mapSubViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("MapSubViewController") as? MapSubViewController)!
    self.mapSubViewController.mapLocationUpdate(xPosition, longitude: yPosition, title: clubName)
[...]
}

当执行“self.mapSubViewController.mapLocationUpdate(...)”行时,我的应用程序崩溃。当它是告诉函数“mapLocationUpdate”的控制器的“viewDidLoad”时,应用程序不会崩溃。

我认为这是因为地图收费后代码中的地图位置无法更改。

我该怎么做?

对不起我的英语,我尽我所能:-)!

Thx!

1 个答案:

答案 0 :(得分:0)

要在地图上设置自定义位置,您应该使用与此类似的内容:

let pLat = 43.6109200
let pLong = 3.8772300
let center = CLLocationCoordinate2D(latitude: pLat, longitude: pLong)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

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