Google Maps Geocoding API - Country Offset, Fancy Results

I am connecting to the Google Maps API with PHP to geocode some starting points for a rental station locator application.

These starting points do not have to be exact addresses; city ​​names are enough. As a starting point, answers to geocoding with accuracy equal to or greater than 4 (city / locality), and the search for surrounding rental stations are used.

The application should work in Germany. When a local name is ambiguous (i.e. there is more than one place for that name), I want to display a list of features.

This is not a problem in general: if you do an ambiguous search, the Google XML output returns a list of <PlaceMark> elements, not just one.

Obviously, I need to shift the geocoding towards Germany, therefore, if someone enters a zip code or the name of a place that exists in other countries, only hits in Germany actually appear.

I thought I could achieve this by adding , de or , Deutschland to the search query. This works mostly fine, but in some cases produces strange and unbearable results.

There are, for example, 27 settlements in Germany called Neustadt . ( Wikipedia )

When I search only Neustadt :

 http://maps.google.com/maps/geo/hl=de&output=xml&key=xyz&q=Neustadt 

I get at least six of them that I could live with (maybe others are not included, or parts of another locality or something else).

However, when I search for Neustadt, de or Neustadt, Deutschland , or Neustadt, Germany , I get only one of twenty-seven locations, for no apparent reason - this is not the largest, most accuracy are accurate and do not have other unique characteristics.

Does anyone know why this is so, and what can I do about it?

I tried the region parameter, but to no avail - when I do not use , de , zip codes (for example, 50825 will be allowed to their American colleagues, not German.

My current workaround is to add the name of the country when the input is only numeric, and otherwise filter out only the results in it, but that sounds awful kludgy. Does anyone know a better way?

+4
source share
3 answers

This is definitely not an exhaustive answer, but just a few notes:

  • You are using an old version of the V2 geocoding API , which Google has recently deprecated in favor of the new V3 API . Google offers to use the new service from now on, and although I have no real experience with the new version, it seems that they have improved the service at different points, especially with the structure of the response. You do not need an API key to use the new service, and you just need to use a slightly different URL:

    http://maps.google.com/maps/api/geocode/xml?address=Neustadt&sensor=false

  • You mentioned that you filter labels for their precision property. Note that this field no longer appears in the results of the new geocoding API, but in any case, I think it was still not very reliable in the old API.

  • You can try using the bounds and region parameters of the new API, but I still doubt that they will solve your problem.

I believe that Google Geocoder is a great tool when you give it the full address, at least in the format "street, locality, country", and it is also very reliable in other formats when it does not need to deal with any ambiguities ( geocoding "London, UK" has always worked for me).

However, in your case, I would prefer to pre-calculate all the coordinates of each German location and simply process the geocoding myself from your database. I think this is entirely feasible because your application is localized to only one country. It seems that every city on Wikipedia's β€œList of German Cities” has coordinates stored in a neat little <span> that looks very easy to parse

 <span class="geo">47.84556; 8.85167</span> 

There are sixteen Neustadts on this list, which could be better than Google six :)

+2
source
+1
source

Found this question looking for this exact problem. Then the implemented Bing Maps API works much better. However, he has his own quirks. For example, if you pass the airport code, it will show you 6 different solutions, each of which corresponds to each airport terminal.

0
source

Source: https://habr.com/ru/post/1310242/


All Articles