Get the coordinates of the MapKit center (Swift 2) and display in UILabel

I am developing an iOS application using Xcode 7.2 and Swift 2.1, and I have successfully implemented the MapKit map in my application. The map loads perfectly and focuses on the user's current location.

Now I want to get the coordinates of the center if the user moves the center of the map to a new location. These new location coordinates will be "captured" with the click of a button (see the "Set Grid" button in the attached gif) and displayed on the label.

Move from A to B and set new center coordinates

The closest I came to the answer here , and I implemented this code, but I still can’t figure out how to update the shortcut with coordinates by clicking the IBOutlet button that I created.

What am I missing here ?!

Any help would be greatly appreciated - thanks in advance!

---------------- NEXT QUESTION ------------------

Now that we have solved the problem, and we have a shortcut filled with the new coordinates of the center, I have one more question - most likely, the supervision of the nob, but I'm on a steep learning curve here, so please ...

We successfully determined the coordinates of the center, and now they were set as mapLongitude and mapLatitude inside the function we created.

I have two other variables ( newTargetLong and newTargetLat ) that make up part of the array of values ​​that will be passed to the next view controller, and I want

let newTargetLong = mapLongitude
let newTargetLat = mapLatitude

.append.

- "" . ?

+4
3

var center = "" . center:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    center = mapView.centerCoordinate
}

, .

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)
}
+7

, , @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! !

+4

: , ( )

, , , :

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

, , func , .

0

Source: https://habr.com/ru/post/1620953/


All Articles