赞
踩
我正在使用XCode 7.2和Swift 2.1开发iOS应用程序,并且我已经在我的应用程序中成功实现了MapKit地图.地图加载完美,并以用户当前位置为中心.
现在,如果用户将地图中心移动到新位置,我想检索中心坐标.按下按钮可以"捕获"这个新位置的坐标(参见附加的gif中的"设置网格参考"按钮)并显示在标签中.
从A移动到B并设置新的中心坐标
我来回答最接近的是在这里,和我实现了代码,但我仍然无法弄清楚如何通过点击我创建了IBOutlet中按钮来更新坐标标签.
我在这里想念的是什么?!
任何帮助将非常感谢 - 提前感谢!
----------------关注问题------------------
现在我们已经解决了问题并且我们得到了新的中心坐标标签,我还有一个问题 - 最有可能是一个菜鸟疏忽,但我在这里有一个陡峭的学习曲线,所以请耐心等待.. .
我们已经成功确定了中心坐标,现在它们在我们创建的函数中设置为mapLongitude和mapLatitude.
我有两个其他变量(newTargetLong和newTargetLat),它们构成了一个值数组的一部分,将传递给下一个视图控制器,我想:
let newTargetLong = mapLongitude let newTargetLat = mapLatitude
这样就可以用.append
指令将新的纬度和经度添加到数组中.
但由于某种原因,我只是无法将这两个值"超出"该功能.我需要做些什么才能实现这一目标?
声明var center = ""
在与其他声明你的类的顶部.在下面的方法中,它应该自动更改以下值center
:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { center = mapView.centerCoordinate }
按下按钮时,将标签的值设置为center的值.
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 }
好的,所以我让它对@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!真诚的感谢!
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。