Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert coordinates to City name?

How to get an address from coordinates using MapKit?

I have this code when long press on the map it gets the coordinates:

func didLongPressMap(sender: UILongPressGestureRecognizer) {      if sender.state == UIGestureRecognizerState.Began {         let touchPoint = sender.locationInView(self.mapView)         let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)         var annotation = MKPointAnnotation()         annotation.coordinate = touchCoordinate         annotation.title = "Your position"         self.mapView.addAnnotation(annotation) //drops the pin         println("lat:  \(touchCoordinate.latitude)")         var num = (touchCoordinate.latitude as NSNumber).floatValue         var formatter = NSNumberFormatter()         formatter.maximumFractionDigits = 4         formatter.minimumFractionDigits = 4         var str = formatter.stringFromNumber(num)         println("long: \(touchCoordinate.longitude)")         var num1 = (touchCoordinate.longitude as NSNumber).floatValue         var formatter1 = NSNumberFormatter()         formatter1.maximumFractionDigits = 4         formatter1.minimumFractionDigits = 4         var str1 = formatter1.stringFromNumber(num1)         self.adressLoLa.text = "\(num),\(num1)"                 } } 

and I want to print in annotation.title the complete address (street, city, zip, country).

like image 722
Tevfik Xung Avatar asked Jan 01 '15 23:01

Tevfik Xung


People also ask

How do I get a city name from geolocation?

You can use https://ip-api.io/ to get city Name. It supports IPv6.

How do I turn my coordinates into a location?

1. Go to maps.google.com. 2. Type out the coordinates into the search bar — using either the degrees, minutes, and seconds (DMS) format, the degrees and decimal minutes (DMM) format, or decimal degrees (DD) format — then hit enter or click on the search icon.

How do I convert latitude and longitude to an address?

Reverse geocoding is the process to convert the latitude and longitude coordinates to a readable address. Type the lat long coordinates and press Convert button. Reverse geocoded address will shown below. Also the municipality, subdivision and country name can be found.


1 Answers


SWIFT 4.2 : EDIT


MapKit framework does provide a way to get address details from coordinates.

You need to use reverse geocoding of map kit. CLGeocoder class is used to get the location from address and address from the location (coordinates). The method reverseGeocodeLocation will returns the address details from coordinates.

This method accepts CLLocation as a parameter and returns CLPlacemark, which contains address dictionary.

So now above method will be updated as:

@objc func didLongPressMap(sender: UILongPressGestureRecognizer) {      if sender.state == UIGestureRecognizer.State.began {         let touchPoint = sender.location(in: mapView)         let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: self.mapView)         let annotation = MKPointAnnotation()         annotation.coordinate = touchCoordinate         annotation.title = "Your position"         mapView.addAnnotation(annotation) //drops the pin         print("lat:  \(touchCoordinate.latitude)")         let num = touchCoordinate.latitude as NSNumber         let formatter = NumberFormatter()         formatter.maximumFractionDigits = 4         formatter.minimumFractionDigits = 4         _ = formatter.string(from: num)         print("long: \(touchCoordinate.longitude)")         let num1 = touchCoordinate.longitude as NSNumber         let formatter1 = NumberFormatter()         formatter1.maximumFractionDigits = 4         formatter1.minimumFractionDigits = 4         _ = formatter1.string(from: num1)         self.adressLoLa.text = "\(num),\(num1)"          // Add below code to get address for touch coordinates.         let geoCoder = CLGeocoder()         let location = CLLocation(latitude: touchCoordinate.latitude, longitude: touchCoordinate.longitude)         geoCoder.reverseGeocodeLocation(location, completionHandler:             {                 placemarks, error -> Void in                  // Place details                 guard let placeMark = placemarks?.first else { return }                  // Location name                 if let locationName = placeMark.location {                     print(locationName)                 }                 // Street address                 if let street = placeMark.thoroughfare {                     print(street)                 }                 // City                 if let city = placeMark.subAdministrativeArea {                     print(city)                 }                 // Zip code                 if let zip = placeMark.isoCountryCode {                     print(zip)                 }                 // Country                 if let country = placeMark.country {                     print(country)                 }         })     } } 
like image 120
Kampai Avatar answered Oct 05 '22 15:10

Kampai