从 MapKit 中获取中心坐标并显示在 UILabel 中
Get center coordinates from MapKit and display in UILabel
我正在使用 XCode 7.2 和 Swift 2.1 开发一个 iOS 应用程序,并且我已经在我的应用程序中成功实现了 MapKit 地图。地图加载完美并以用户当前位置为中心。
现在我想在用户将地图中心移动到新位置时检索中心坐标。这个新位置的坐标将在按下按钮时 "captured"(参见随附 gif 中的 "Set Grid Reference" 按钮)并显示在标签中。
Move from A to B and set new center coordinates
我最接近的答案是 ,我已经实现了该代码,但我仍然无法弄清楚如何通过单击 IBOutlet 按钮用坐标更新标签我创造了。
我在这里错过了什么?!
非常感谢任何帮助 - 提前致谢!
----------------后续问题-----------------
既然我们已经解决了这个问题并且我们得到了用新的中心坐标填充的标签,我还有一个问题 - 很可能是一个菜鸟的疏忽,但我在这里的学习曲线很陡,所以请忍耐一下...
我们已经成功确定了中心坐标,现在它们在我们创建的函数中设置为 mapLongitude 和 mapLatitude。
我还有另外两个变量(newTargetLong 和 newTargetLat),它们构成了将传递给下一个视图控制器,我想:
let newTargetLong = mapLongitude
let newTargetLat = mapLatitude
以便可以使用.append
指令将新的纬度和经度添加到数组中。
但出于某种原因,我无法获得该函数的那两个值 "out"。我需要做什么才能做到这一点?
在 class 的顶部与其他声明一起声明 var center = ""
。在下面的方法中,它应该自动更改 center
:
的值
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}
当您按下按钮时,将标签的值设置为中心的值。
self.yourLabelName.text = center
要显示为"Latitude: ... Longitude: ...",请执行以下操作:
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。
首先我假设:您不确定何时何地更新标签的文本(您创建的)
然后,根据您链接的 post 关于如何获取当前中心坐标,
我想你可以尝试在这个有趣的地方添加一行:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var center = mapView.centerCoordinate
#add a line here to update the label
}
由于每次用户重新定位地图时,都会调用此委托函数,标签会自动更新为当前新的中心坐标。
好的,所以我对@AppDevGuy 提供的代码进行了一些小的改动。这是我所做的:
func mapView(myMap: MKMapView, regionDidChangeAnimated animated: Bool) {
let center = myMap.centerCoordinate
let mapLatitude = center.latitude
let mapLongitude = center.longitude
let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"
self.TargetGridReference.text = latAndLong
}
对于按下按钮:
@IBAction func SetTargetGridReference(sender: UIButton) {
return mapView(myMap, regionDidChangeAnimated: true)
}
UILabel 中的输出如下所示:
Lat: -34.5678901234567
Long: 19.1234567890123
我不确定这是笨拙还是优雅,但它很有魅力!我检查了输出,坐标准确无误!
由此产生的最后一个问题:有没有办法将这些经纬度值缩短为 6 位数字,而不是目前的 13 位数字?
谢谢@AppDevGuy!衷心感谢!
要缩短坐标,请使用“%.4f”格式。这是我用于经纬度和缩短坐标的一根绳子的示例,我相信你能弄清楚它是如何工作的。
myLabel.text = "\(String(format: "%.4f", location.coordinate.latitude)), \(String(format: "%.4f", location.coordinate.longitude))"
这会将我所在位置的坐标打印到一个标签中。 .4 代表小数位。所以你可以选择你想要的任何数字,你想要坐标多长或多短。
我正在使用 XCode 7.2 和 Swift 2.1 开发一个 iOS 应用程序,并且我已经在我的应用程序中成功实现了 MapKit 地图。地图加载完美并以用户当前位置为中心。
现在我想在用户将地图中心移动到新位置时检索中心坐标。这个新位置的坐标将在按下按钮时 "captured"(参见随附 gif 中的 "Set Grid Reference" 按钮)并显示在标签中。
Move from A to B and set new center coordinates
我最接近的答案是
我在这里错过了什么?!
非常感谢任何帮助 - 提前致谢!
----------------后续问题-----------------
既然我们已经解决了这个问题并且我们得到了用新的中心坐标填充的标签,我还有一个问题 - 很可能是一个菜鸟的疏忽,但我在这里的学习曲线很陡,所以请忍耐一下...
我们已经成功确定了中心坐标,现在它们在我们创建的函数中设置为 mapLongitude 和 mapLatitude。
我还有另外两个变量(newTargetLong 和 newTargetLat),它们构成了将传递给下一个视图控制器,我想:
let newTargetLong = mapLongitude
let newTargetLat = mapLatitude
以便可以使用.append
指令将新的纬度和经度添加到数组中。
但出于某种原因,我无法获得该函数的那两个值 "out"。我需要做什么才能做到这一点?
在 class 的顶部与其他声明一起声明 var center = ""
。在下面的方法中,它应该自动更改 center
:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}
当您按下按钮时,将标签的值设置为中心的值。
self.yourLabelName.text = center
要显示为"Latitude: ... Longitude: ...",请执行以下操作:
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。
首先我假设:您不确定何时何地更新标签的文本(您创建的)
然后,根据您链接的 post 关于如何获取当前中心坐标, 我想你可以尝试在这个有趣的地方添加一行:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var center = mapView.centerCoordinate
#add a line here to update the label
}
由于每次用户重新定位地图时,都会调用此委托函数,标签会自动更新为当前新的中心坐标。
好的,所以我对@AppDevGuy 提供的代码进行了一些小的改动。这是我所做的:
func mapView(myMap: MKMapView, regionDidChangeAnimated animated: Bool) {
let center = myMap.centerCoordinate
let mapLatitude = center.latitude
let mapLongitude = center.longitude
let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"
self.TargetGridReference.text = latAndLong
}
对于按下按钮:
@IBAction func SetTargetGridReference(sender: UIButton) {
return mapView(myMap, regionDidChangeAnimated: true)
}
UILabel 中的输出如下所示:
Lat: -34.5678901234567
Long: 19.1234567890123
我不确定这是笨拙还是优雅,但它很有魅力!我检查了输出,坐标准确无误!
由此产生的最后一个问题:有没有办法将这些经纬度值缩短为 6 位数字,而不是目前的 13 位数字?
谢谢@AppDevGuy!衷心感谢!
要缩短坐标,请使用“%.4f”格式。这是我用于经纬度和缩短坐标的一根绳子的示例,我相信你能弄清楚它是如何工作的。
myLabel.text = "\(String(format: "%.4f", location.coordinate.latitude)), \(String(format: "%.4f", location.coordinate.longitude))"
这会将我所在位置的坐标打印到一个标签中。 .4 代表小数位。所以你可以选择你想要的任何数字,你想要坐标多长或多短。