0
votes

We've been using this API from quite some time. We could see addresses being returned out of the UK & hence changed the geocoding URL to restrict to UK only.

http://maps.googleapis.com/maps/api/geocode/xml?latLng=54,-2&address=gdfgsdgsd,GB

http://maps.googleapis.com/maps/api/geocode/xml?latLng=54,-2&address=gdfgsdgsd,GB&components=country:GB

Please check the results for both of the above URL's. The first URL returns ZERO_RESULTS which is correct. The second one always returns a lat long from centre of UK, no matter what search term we use.

You can also try replacing the value for 'address' to replicate the issue : 452376452334,^$%^$%^$%,458352462354^$%^$%^

2
why are you including both the latLng (reverse geocoding only) and address (geocoding only) parameters?geocodezip
Removing latLng parameter or searching for a 'real' address doesn't resolve the issue I have. The postcode/location is entered in a text box by the user & then we invoke Google to georesolve. It was working fine until we introduced the parameter components=country:UK. But we had to introduce this so that non UK results aren't returnedakimran82
You might want to indicate the actual problem you are trying to solve in your question and include that information.geocodezip

2 Answers

2
votes

As per the Google Geocoding API about Component Filtering

A query containing a component filter will only return the geocoding results that match the filter. If no matches are found, the geocoder will return a result that matches the filter itself.

Request :

https://maps.googleapis.com/maps/api/geocode/json?address=someincomprehesibleaddress&components=administrative_area:TX|country:US&key=YOUR_API_KEY

{
"results" : [
      {
         "address_components" : [
            {
               "long_name" : "Texas",
               "short_name" : "TX",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Texas, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.5007041,
                  "lng" : -93.5080389
               },
               "southwest" : {
                  "lat" : 25.8371638,
                  "lng" : -106.6456461
               }
            },
            "location" : {
               "lat" : 31.9685988,
               "lng" : -99.9018131
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.5015087,
                  "lng" : -93.5080389
               },
               "southwest" : {
                  "lat" : 25.8371638,
                  "lng" : -106.6456461
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJSTKCCzZwQIYRPN4IGI8c6xY",
         "types" : [ "administrative_area_level_1", "political" ]
      }
   ],
   "status" : "OK"
}

Component filtering will return a ZERO_RESULTS response only if you provide filters that exclude each other.

Request :

https://maps.googleapis.com/maps/api/geocode/json?components=administrative_area:TX|country:FR&key=YOUR_API_KEY

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}
1
votes

Looks like I have found a way to resolve this. The URL pattern I use now is : http://maps.googleapis.com/maps/api/geocode/xml?address=Leeds,UK|GB

The below URL now gives ZERO_RESULTs http://maps.googleapis.com/maps/api/geocode/xml?address=gdfgsdgsd,UK|GB

Also as Joyson suggested, we could also check for the country returned back & display an error message.