Get center coordinates from MapKit and display in UILabel

how to search for location and display results using apple’s mapkit swift
swift mapkit annotation
mapkit tutorial swift 4
swift mapkit current location
add multiple annotations to mapview ios swift
mkmarkerannotationview
mkannotation
mkpointannotation

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?

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.

Get center coordinates form MapKit (Swift 2) and display in UILabel, Declare var center = "" at the top of your class with other declarations. In the method below, it should automatically change the value of center : The map coordinate at the center of the map view. func show Annotations ([MKAnnotation], animated : Bool) Sets the visible region so that the map displays the specified annotations.

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!

Get center coordinates from MapKit and display in UILabel, 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  Changes the center coordinate of the map and optionally animates the change. func show Annotations ([MKAnnotation], animated : Bool) Sets the visible region so that the map displays the specified annotations.

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.

IOS6 Mapkit licensing, terms of use, How do I search locations and display results using Apple's MapKit Swift? Map coordinates are numerical references to locations on the WoW maps. You can display your coordinates with many AddOns. They are also called tloc, an abbreviation for "Thottbot location". It consists of a set of x/y coordinates, which is two numbers ranging from 0 to 100, representing a location on the current zone map.

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.

iOS 5 Recipes: A Problem-Solution Approach, . If you are going to show vehicles' locations on map and those vehicles are your customers (you charge for the service), you need GMAP Business edition and Premier API. Transform coordinates for position on a map - converting latitude / longitude degrees Coordinates on a map - pick GPS lat & long or coordinates in a projection system From MapTiler Team

MapKit Tutorial: Getting Started, #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> @interface SBViewController : UIViewController <MKMapViewDelegate> { MKMapView *mapViewUserMap; UILabel The region is the portion of the map that is currently being displayed. The region consists of a center coordinate and a distance in latitude and longitude  mapkit.Coordinate Region. A rectangular area on a map defined by a center coordinate and a span, expressed in degrees of latitude and longitude. The horizontal

How to search for location and display results using Apple's MapKit, You'll use this to set the starting coordinates of the map view to a point in Honolulu. The MapKit view camera keeps track of its distance from the view center when zooming in and out let detailLabel = UILabel() detailLabel. You can center the map on a given coordinate, specify the size of the area you want to display, and annotate the map with custom information. When you initialize a map view, you specify the initial region for that map to display by setting the region property of the map. A region is defined by a center point and a horizontal and vertical distance, referred to as the span.

Swift 101. Convert coordinates to city names and back, Get user location; Set up a Map Kit View; Use UISearchController to display which is a combination of the map center (coordinate) and zoom level (span). I want to set a label on a whole different view controller as the  Use the MapKit framework to embed maps directly into your own windows and views. You can add annotations and overlays to the map to call out points of interest or user destinations. You can also provide text completion for users typing in the name of a point of interest.

Comments
  • Thanks, @app-dev-guy Quick question. When declaring center as a variable, what type does it have? I've followed your instructions and I'm getting the label populated with the following: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
  • @JacoGriesel will respond shortly.
  • @JacoGriesel Incorrect answer, will get back to you shortly.
  • @JacoGriesel thank you for your patience. Please see above. I tested and it works.
  • Still working on those required reputations to be able to effect an upvote... :( But I'll return here once I have it! ;-)
  • Thanks for the shoutout :-)
  • Nope, it doesn't work, @david-hu. Any suggestions why?
  • try to print sth inside this func to see if it is actually being called
  • if this function is being called when you recenter the map, and the label should be updated