Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nearest City using Google Maps API

I'm not a coder or programmer, but I have hired someone to do so for me. I'm also not completely naive when it comes to computer capabilities...

I am being told by the developer that it is impossible to reverse geocode in google maps api and have it accurately return the correct city. I don't believe this because I see the api return a formatted address with the city in it. The developer is saying that they can only pull one field from the API and because city names are located in either the locality field or neighborhood fields, they have no way of determining which to use for the different coordinates. Are these true statements?

I am also being told that reverse geocoding for the nearest city when the coordinates exists over water (mostly in the ocean), is impossible. For example, is it true that a coordinate 5 miles directly west of San Diego is impossible to have it return San Diego as the nearest city?

I have searched on here for solutions and found a few related to this, which I have forwarded to the developer... but I am hoping to take this exact thread and show it to them as well. Are these things really impossible with Google Maps API?

Thank you for the help!

like image 874
user3663613 Avatar asked May 22 '14 06:05

user3663613


1 Answers

Look in this document for reverse geocoding in the Google Maps JavaScript API, for a detailed description how to convert coordinates in a readable address.

Your first statement is kind of true:

You can read all fields from the arrays in the results of the Geocoder if necessary. If the address_components array has an element locality in the types, the city name is part of it. But it's NOT guarenteed that there is always this element or the desired element contained. Have a look at the hierarchie of the types in the link above, which may be part of the response. When you say you have to search the elements neighborhood and locality, it sounds to me you wanna force a specific name. This is almost impossible to determine.

The second statemant, about the nearest city is true. Here is a statement from the docs:

Reverse geocoding is not an exact science. The geocoder will attempt to find the closest addressable location within a certain tolerance.

There is nothing stated about the dimensions of this tolerence. Your example of 5 miles west of San Diego can return data about San Diego, the North Pacific Ocean or Coronado, which is in San Diego County.

Trust your developer!

Edit: It depends what you wanna do. If you always wanna have the city name in your request, forget it you can't guarantee that.

There are some zipcode APIs which are described in this answer. But again, you have to get the Zipcode somehow, and the postal_code isn't always part of the response. There are no other APIs like geocoding ones which can convert coordinates to readable addresses. But there are some out there listed here.

like image 85
Steve Benett Avatar answered Sep 20 '22 12:09

Steve Benett