Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get center coordinates from MapKit and display in UILabel

Tags:

ios

swift

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.

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?

like image 272
Jaco Griesel Avatar asked Dec 06 '22 19:12

Jaco Griesel


2 Answers

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.

like image 150
App Dev Guy Avatar answered Dec 08 '22 08:12

App Dev Guy


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!

like image 33
Jaco Griesel Avatar answered Dec 08 '22 08:12

Jaco Griesel