问题
I'm accepting a user input which is supposed to be a geographic location. I would like to validate that what the user has entered describes a location--e.g., postal code, airport code, street address, city name, etc.
The obvious answer is to use the Google Geocode API to do a request for the user's input and see if I get any results.) However the geocode ToS forbids this:
...the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.
Does anyone know of alternatives?
回答1:
Yahoo also has a geolocation API which has a less-restrictive TOS. By querying for a location, if you receive a non-null
result set then you know its a valid place.
# A query for New York...
http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20geo.places%20where%20text='New+York'&format=json
# Results in a response
{"query":{"count":10,"created":"2013-01-11T18:40:32Z","lang":"en-US","results":{"place":[... places ... ] }}}
whereas
# A query for junk
http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20geo.places%20where%20text='kqpo234xk'&format=json
# Results in null
{"query":{"count":0,"created":"2013-01-11T18:39:21Z","lang":"en-US","results":null}}
来源:https://stackoverflow.com/questions/12042452/verify-user-input-location-string-is-a-valid-geographic-location