Google offset in jquery scripting

A very simple question that doesn't seem to work out right now, google talks about the region, based here:

https://developers.google.com/maps/documentation/geocoding/#RegionCodes

It uses the following parameter:

region: 

This is the im code that works with: http://jsfiddle.net/spadez/Jfdbz/19/

My question is: how do I pass my "ctryiso" variable to this parameter in my script? When I try, nothing changes, so when ctryiso is installed in the USA and I print in London, it still geocodes London, England. I heard this may be a little unreliable, but I don't think my implementation is correct.

Any help would be greatly appreciated.

+6
source share
4 answers

Usually you have:

 geocoder.geocode({ address: query } 

You should pass region as it is (as you mentioned):

 geocoder.geocode({ address: query, region: SOME_VARIABLE_WITH_REGION_CODE } 

And that should work, though ... it seems like it is not, and people have more of the same problem.

The (ugly) solution is to add it to the address, for example:

 geocoder.geocode({ address: query + ', ' + SOME_VARIABLE_WITH_REGION_CODE } 

I just tested it and it works, but as I said, this is not a โ€œgoodโ€ one, nor the right way to do this is just a workaround.

As we see here: https://developers.google.com/maps/documentation/javascript/reference?hl=th-TH&csw=1#GeocoderRequest

You can pass address , bounds , location and region . Since the area does not seem to work, I will try to use bounds by first getting the coordinates for the desired area:

https://developers.google.com/maps/documentation/javascript/reference?hl=th-TH&csw=1#LatLngBounds

+2
source
  • You can try adding the region parameter to the query string when you load the Google Maps API, although by default it should be US

      https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&region=US 
  • You can also add ", USA" to the address that you pass to the geocoder - this works very well, but it is ugly.

  • You can also return all the results and analyze them and display only those that are in the country of interest. In most cases, you will need to do this anyway, since there are several locations with the same name in most countries .... This is the preferred method as if someone in London, USA, you get London, KY and London, HE and the user / you still need to decide which one they want ...

0
source

As indicated in another answer, you can include the region in your geocoding request to offset (not limit) the result.

However, in this particular case, London in the UK is still considered a better result than London in the USA. Therefore, you can further limit the result (vs bias) using GeocoderComponentRestrictions , as described in Geocoding Service Docs

I made a fiddle showing this type of filtering, requiring the results to be in the USA.

You will notice that the result is in London, USA, but if you remove GeocoderComponentRestrictions from the script code, the first result is now in London, despite the shift in the region.

However, itโ€™s important to note that the offset really works. For example, if you now change the region to "ES" and the address "Toledo", the result will be in Spain. If you delete the offset, it will be set to Toledo USA by default.

tl; dr uses region for offset, and GeocoderComponentRestrictions for restriction.

0
source
 function getLatLong(address){ var geo = new google.maps.Geocoder; geo.geocode({'address':address},function(results, status){ if (status == google.maps.GeocoderStatus.OK) { return results[0].geometry.location; } else { alert("Geocode was not successful for the following reason: " + status); } }); } I am unable to understand your exact need but, I hope it will help you and if not then please tell me your exact need in short way 
0
source

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


All Articles