0

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

JayVDiyk
  • 4,277
  • 22
  • 70
  • 135
  • Reverse geocoding does not return a string, it returns an array of `CLPlacemark` objects. These have a `locality` property, which is the "city". However, what constitutes a "city" varies across the world and may also differ from your requirements. – Michael Mar 09 '16 at 04:27
  • Are you asking how you find out what city they're in, or if they're inside of city limits (as opposed to in a rural area)? The name of the city will typically be the "locality" of a `CLPlacemark` you can get from reverse geocoding, but there's no way to tell if they're inside the city limits, from the current API. – wjl Mar 09 '16 at 06:48

2 Answers2

0

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 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 – JayVDiyk Mar 09 '16 at 04:06
  • Well it's what there is. – matt Mar 09 '16 at 04:30
0

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.

ebby94
  • 3,131
  • 2
  • 23
  • 31
  • Are you saying geocoding is unreliable in general, or Apple's geocoding service? This still uses geocoding. – wjl Mar 09 '16 at 06:45
  • Apple's geocoding's service is unreliable. From what I've seen, the address returned by geocoding is usually never accurate. Using Google API returns the exact address for the specified coordinates. Apple's geocoding maybe accurate for many places, but Google's location is generally accurate for whatever coordinate you specify. – ebby94 Mar 09 '16 at 06:53