Detecting Which City User Is In

86 Views Asked by At

Once we request the coordinates. How do we know that the location is in New York or LA? Or neither?

How do we calculate it?

I have no idea how to do it. Any help please

P.S.: Reverse Geocoding will convert the coordinate to name. But That is not my question. I am asking how do we know that this location is within a city or not? Is it by comparing string generated by the reverse geocoding, cause it sounds "error prone" to me

2

There are 2 best solutions below

2
matt On

What you're looking for is called reverse geocoding. Look at the CLGeocoder class and the reverseGeocodeLocation:completionHandler: method.

2
ebby94 On

I assume that you don't want to use Apple's Geocoding because it isn't reliable. You can use convert the coordinates into location using Google API.

let urlPath: String = "http://maps.googleapis.com/maps/api/geocode/json?latlng=\(self.latitude),\(self.longitude)&sensor=true_or_false
let recievedLocation : NSMutableData! = self.getJSON(urlPath)
let recievedDictonary = self.parseJSON(recievedLocation)
let returnedPlaces : NSArray = recievedDictonary["results"] as! NSArray

Replace the latitude and longitude in the URL with your coordinates. The returnedPlaces will have an array of dictionaries. This will be in JSON format and you'll need to do some formatting. Use this link to get an idea of how the returned data will be. Let me know if you need help in formatting the received data.