从MapKit获取中心坐标并显示在UILabel中 [英] Get center coordinates from MapKit and display in UILabel
问题描述
我正在使用XCode 7.2和Swift 2.1开发iOS应用,并且已经在我的应用中成功实现了MapKit地图.地图会完美加载,并以用户当前位置为中心.
I'm developing an iOS app using XCode 7.2 and Swift 2.1, and I've successfully implemented a MapKit map in my app. The map loads perfectly and centers on the user's current location.
现在,如果用户将地图中心移动到新位置,我想检索中心坐标.按下按钮(请参阅随附的gif中的设置网格参考"按钮),将捕获"该新位置的坐标,并显示在标签中.
Now I want to retrieve the center coordinates should the user move the center of the map to a new location. This new location's coordinates will be "captured" on the press of a button (see the "Set Grid Reference" button in the attached gif) and be displayed in a label.
最接近我的答案是此处,并且我已经实现了该代码,但是我仍然不知道如何通过单击创建的IBOutlet按钮来使用坐标更新标签.
The closest I've come to an answer is here, and I've implemented that code, but I still can't figure out how to update the label with the coordinates by clicking the IBOutlet button I've created.
我在这里想念什么?!
任何帮助将不胜感激-预先感谢!
Any help would be highly appreciated - thanks in advance!
----------------关注的问题------------------
----------------FOLLOW-ON QUESTION------------------
现在我们已经解决了问题,并在标签上填充了新的中心坐标,我还有一个问题-最有可能是菜鸟监督,但是我的学习曲线很陡峭,所以请耐心等待我...
Now that we've solved the problem and we got the label populated with the new center coordinates, I have one more question - most probably a noob oversight, but I'm on a steep learning curve here, so please bear with me...
我们已经成功确定了中心坐标,现在已在我们创建的函数中将其设置为 mapLongitude 和 mapLatitude .
We have successfully determined the center coordinates and they are now set as mapLongitude and mapLatitude inside the function we've created.
我还有另外两个变量( newTargetLong 和 newTargetLat ),这些变量构成了将传递给下一个视图控制器的值数组的一部分,我想:
I have two other variables (newTargetLong and newTargetLat) that forms part of an array of values that will be passed on to the next view controller, and I want to:
let newTargetLong = mapLongitude
let newTargetLat = mapLatitude
,以便可以使用.append
指令将新的纬度和经度添加到数组中.
so that the new latitude and longitude can be added to the array with an .append
instruction.
但是由于某种原因,我无法从该函数中取出"这两个值.我需要怎么做才能做到这一点?
But for some reason, I just can't get those two values "out" of that function. What do I need to do to accomplish that?
推荐答案
在类顶部使用其他声明声明var center = ""
.在下面的方法中,它应该自动更改center
的值:
Declare var center = ""
at the top of your class with other declarations. In the method below, it should automatically change the value of center
:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}
按下按钮时,将标签的值设置为center的值.
When you press your button set the value of your label to the value of center.
self.yourLabelName.text = center
要显示为纬度:...经度:...",请执行以下操作:
To display as "Latitude: ... Longitude: ..." do as follows:
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
}
如果要设置坐标格式以显示更友好的内容:
If you want to format the coordinates to display a little more friendly:
center = "\(String(format: "%.5f", mapLatitude)), \(String(format: "%.5f", mapLongitude))"
根据您偏好的小数位数调整%.5f.
Adjust the %.5f according to your preference of number of decimal places.
这篇关于从MapKit获取中心坐标并显示在UILabel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!