Get center coordinates from MapKit and display in UILabel

青春壹個敷衍的年華 提交于 2019-12-04 10:25:40

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.

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!

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.

wilkinsonncl

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.

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