gpt4 book ai didi

ios - 从 MapKit 中获取中心坐标并显示在 UILabel 中

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:55 26 4
gpt4 key购买 nike

我正在使用 XCode 7.2 和 Swift 2.1 开发一个 iOS 应用程序,并且我已经在我的应用程序中成功地实现了 MapKit map 。 map 加载完美,并以用户当前位置为中心。

现在我想在用户将 map 中心移动到新位置时检索中心坐标。按下按钮(请参阅附加 gif 中的“设置网格引用”按钮)将“捕获”这个新位置的坐标,并显示在标签中。

Move from A to B and set new center coordinates

我最接近的答案是 here ,并且我已经实现了该代码,但我仍然无法弄清楚如何通过单击我创建的 IBOutlet 按钮使用坐标更新标签。

我在这里错过了什么?!

非常感谢任何帮助 - 提前致谢!

----------------后续问题----------------

既然我们已经解决了这个问题并且我们得到了用新的中心坐标填充的标签,我还有一个问题 - 很可能是菜鸟的疏忽,但我在这里的学习曲线很陡,所以请耐心等待我...

我们已经成功确定了中心坐标,现在它们在我们创建的函数中被设置为 ma​​pLongitudema​​pLatitude

我有两个其他变量(newTargetLongnewTargetLat),它们构成将传递给下一个 View Controller 的值数组的一部分,我想:

let newTargetLong = mapLongitude
let newTargetLat = mapLatitude

以便可以使用 .append 指令将新的纬度和经度添加到数组中。

但出于某种原因,我无法从该函数中“取出”这两个值。我需要做什么才能做到这一点?

最佳答案

在类的顶部声明 var center = "" 以及其他声明。在下面的方法中,它应该自动改变center的值:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}

当您按下按钮时,将标签的值设置为中心的值。

self.yourLabelName.text = center

要显示为“纬度:...经度:...”,请执行以下操作:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let mapLatitude = mapView.centerCoordinate.latitude
let mapLongitude = mapView.centerCoordinate.longitude
center = "Latitude: \(mapLatitude) Longitude: \(mapLongitude)"
print(center)
self.yourLabelName.text = center
}

如果想格式化坐标显示更友好一点:

center = "\(String(format: "%.5f", mapLatitude)), \(String(format: "%.5f", mapLongitude))"

根据您对小数位数的偏好调整 %.5f。

关于ios - 从 MapKit 中获取中心坐标并显示在 UILabel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com