Get center coordinates from MapKit and display in UILabel

情到浓时终转凉″ 提交于 2019-12-21 17:14:13

问题


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.

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.

Move from A to B and set new center coordinates

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.

What am I missing here?!

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...

We have successfully determined the center coordinates and they are now set as mapLongitude and mapLatitude inside the function we've created.

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

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?


回答1:


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
}

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

Adjust the %.5f according to your preference of number of decimal places.




回答2:


OK, so I got it working with some small alterations to the code that @AppDevGuy provided. Here's what I've done:

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
}

And for the button press:

@IBAction func SetTargetGridReference(sender: UIButton) {

    return mapView(myMap, regionDidChangeAnimated: true)
}

The output in the UILabel looks like this:

Lat: -34.5678901234567
Long: 19.1234567890123

I'm not sure if this is clunky or elegant, but it works like a charm! I've checked the output and the coordinates are spot on!

One last question arising from this: Is there any way to shorten those values of the latitude and longitude to say 6 digits, instead of the 13 it currently have?

Thanks @AppDevGuy! Sincerely appreciated!




回答3:


First I am assuming: you are not sure when and where to update the text of the Label(you created)

Then,based your linked post about how to get the current center coordinate, I think you can try added a line in this fun:

 func mapView(mapView: MKMapView, regionDidChangeAnimated animated:     Bool) {
var center = mapView.centerCoordinate
#add a line here to update the label
} 

Since every time user recenter the map, this delegate func will be called, and the label will automatically updated with current new center coordinate.




回答4:


To shorten the coordinate use the "%.4f" format. Here is an example of a piece of cord i used for my lat and long with a shortened coordinate, I'm sure you can work out how it works.

myLabel.text = "\(String(format: "%.4f", location.coordinate.latitude)), \(String(format: "%.4f", location.coordinate.longitude))"

This prints the coordinates from my location into one label. the .4 represents the decimal places. so you can choose whatever number you want for how long or short you want the coordinates.



来源:https://stackoverflow.com/questions/34378118/get-center-coordinates-from-mapkit-and-display-in-uilabel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!