0

Is there any way, using MapKit in iOS, to search a place on Google Maps or whatever, and then determine if such a place exists? For example, if I type "New York city," will the search return anything that I can use to determine whether or not "New York city" actually exists? (as opposed to searching "York City New" or something)

Thanks in advance!

The Awesome Guy
  • 322
  • 5
  • 15

1 Answers1

0

There is a Google geocoder API that you can use for this. I think the iOS geocoder uses the same service, but I could be mistaken. It's quite easy to use.

I use the google geocoder, and it returns a lot of useful information, and when there is more than one location that might match your query, it returns an array of choices.

Also, try some of the sources mentioned in this SO question.

UPDATE

There is a built in geocoder class called CLGeocoder. It is available in iOS 5 and deprecates MKReverseGeocoder. (Reverse geocoding doesn't do what you want, but CLGeocoder does forward and reverse.) These apparently use the Google API and you must adhere to the Google user agreement.

Community
  • 1
  • 1
Jim
  • 5,940
  • 9
  • 44
  • 91
  • I have two questions though about CLGeocoder… The first is: does it actually determine if a place exists? As far as I can tell from the API, it just finds the coordinates for the input (unless would there be some way for me to check if a place exists based on whether or not it returns coordinates?) Also, would this class work for areas? I see it would work for a specific address, but what if I just typed in "Cupertino"? – The Awesome Guy Feb 22 '12 at 19:31
  • CLGeocoder retuns a CLPlacemark object. This appeas to behave like a dictionary that contains much of the same information that the Google geocoder returns. (As I said, I think it uses the Google API). To test your general query, try this link: http://maps.googleapis.com/maps/api/geocode/json?address=cupetino&sensor=false . See the documentation for more info. Try other broader or narrower location specification and see what happens. (substitute your own in place of "cupertino") I think either CLGeocoder or the Google equivalent will do what you are asking. – Jim Feb 22 '12 at 19:55
  • Google provides status codes, so if the place doesn't exist (per their interpretation), then there is a code for that. – Jim Feb 22 '12 at 19:59
  • Wow thanks! That's actually really great and exactly what I was looking for! Just two more questions lol… Which do you think is better (in terms of speed, etc), the JSON output or the XML output? Also, obviously a phone has a location sensor, but if I'm not looking up the user's current location, then it doesn't matter whether or not I set sensor= to true or false, right? – The Awesome Guy Feb 22 '12 at 21:08
  • I think most people prefer JSON for this type of data. It's easy to convert JSON strings into NSDictionary. There are easy-to-use JSON kits available, and it is built into iOS5.If you are new to JSON, there is a learning curve, but it's not steep. For example, after converting JSON to a dictionary, check whether an object in your dictionary is NSNumber or NSString. Also, in some cases where one or more items can be returned (like if two locations match your query), you should test to see if the object in your dictionary is an array or not before processing it. – Jim Feb 22 '12 at 21:17
  • I don't know why Google asks about the sensor, and you are probably right. I tried it both ways in my browser, and they both work. Google is probably just collecting statistics. – Jim Feb 22 '12 at 21:23
  • One thing to be aware of – with the Google Geocoding API and with MKReverseGeocoder, their Terms of Service *require* you to use the API call only in order to display that result on a Google map (MKMapView). The new CLGeocoder class has no such restriction, probably because Apple is allegedly no longer using Google's service with it. – poetmountain May 17 '12 at 21:21