When I fetch Google Maps Geocoding API from my browser, it's working, but not from my PHP script.
Working : https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France
Returns :
{
"results" : [
{
"address_components" : [
{
"long_name" : "La Pérelle",
"short_name" : "La Pérelle",
"types" : [ "colloquial_area", "political" ]
},
],
"formatted_address" : "La Pérelle, 38380, France",
// other stuffs
"partial_match" : true,
"place_id" : "ChIJZzDRRAX4ikcRnZ-vb1tnTgY",
"types" : [ "colloquial_area", "political" ]
}
],
"status" : "OK"
}
Not working :
<?php
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France';
echo file_get_contents($url);
Returns:
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Any ideas?
Update : some people are not seeing any results from their browser too. Could Google use my position to show decent results?
(Absence of) language preference is most commonly behind this issue.
When you send the API request from your browser:
https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France
it may happen that your browser language preferences is set to prefer French as the first language, which makes your browser include send the Accept-Language header along with all requests.
Google Geocoding API will check for the language
GET parameter, and in absence of that it will check for the Accept-Language header as a backup. If your browser is set to prefer content in French, that should make the above request equivalent to
https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France&language=fr
For me, because my browser is set to prefer English, the first request returns "La Perelle, 73130 Saint-François-Longchamp, France" while the second returns "La Pérelle, 38380 Saint-Pierre-de-Chartreuse, France".
Since the introduction of the new geocoder in late 2016, the region
parameter is no longer the only one that influences results, the language
parameter does influence results now:
The preferred language has a small influence on the set of results that the API chooses to return, and the order in which they are returned. The geocoder interprets abbreviations differently depending on language, such as the abbreviations for street types, or synonyms that may be valid in one language but not in another. For example, utca and tér are synonyms for street in Hungarian.
That said, if "La Perelle, 73130 Saint-François-Longchamp, France" is just a bad results for the above query (La Pérelle, Saint-Pierre-de-Chartreuse, France) please consider filing a bug report.
I think is related to region region biasing.
Try adding the region
parameter (although you might need to pre-calculate it when building your request, if you have requests for many different regions).
E.g., for me, from Madrid, Spain, this request brings zero results:
https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France
But, if I add the region=fr
parameter
https://maps.google.com/maps/api/geocode/json?sensor=false®ion=fr&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France
I get:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Chartreuse Mountains",
"short_name" : "Chartreuse Mountains",
"types" : [ "establishment", "natural_feature" ]
},
{
"long_name" : "Saint-Pierre-d'Entremont",
"short_name" : "Saint-Pierre-d'Entremont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Isere",
"short_name" : "Isere",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auvergne-Rhône-Alpes",
"short_name" : "Auvergne-Rhône-Alpes",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "73670",
"short_name" : "73670",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Chartreuse Mountains, 73670 Saint-Pierre-d'Entremont, France",
"geometry" : {
"location" : {
"lat" : 45.3773525,
"lng" : 5.827970700000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.3787014802915,
"lng" : 5.829319680291503
},
"southwest" : {
"lat" : 45.3760035197085,
"lng" : 5.826621719708499
}
}
},
"place_id" : "ChIJSXJWFav4ikcREQZmf4QdYMc",
"types" : [ "establishment", "natural_feature" ]
}
],
"status" : "OK"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With