ios - 更改 map 坐标(MapKit)

标签 ios swift mapkit

我想使用个人坐标更改 map 位置。 (当我说“个人”时,不是“用户位置”,而只是随机坐标)。

这是我的 MapView Controller :

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

}

当我想更改 map 的位置时,我告诉我的函数“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(...)”行时,我的应用程序崩溃。当 Controller 的“viewDidLoad”告诉函数“mapLocationUpdate”时,应用程序将崩溃不会崩溃。

我认为这是因为 map 收费后无法在代码中更改 map 位置。

我该怎么办?

抱歉我的英语不好,我尽力了:-)!

谢谢!

最佳答案

为了在 map 上设置自定义位置,您应该执行与此类似的操作:

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)

关于ios - 更改 map 坐标(MapKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494181/

相关文章:

ios - iPhone中的Facebook SDK错误

ios - 在 swift 中,当我的类有同名的子类时如何引用最顶层的类

arrays - 如何匹配两个数组中相同的牌

ios - '安装应用程序' AppStoreView 就像 iOS 的 Facebook 应用程序一样

ios - 从 UINavigationController 中使用自定义转换关闭 UIViewController 时出现黑屏

swift - macCatalyst 应用程序 : how to close a window without terminating the app?

swift - 地址和建议的自动完成功能作为下拉菜单

objective-c - 如何在 View 加载时显示 map View 标注

iphone - 在使用Mapkit时,我可以看到完全可滚动的 map ,但没有看到Apple总部?

iphone - 保存增量 int 变量