可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using Google's Geocoder to find lat lng coordinates for a given address.
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{
'address': address,
'region': 'uk'
}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
});
address
variable is taken from an input field.
I want to search locations only in UK. I thought that specifying 'region': 'uk'
should be enough but it's not. When I type in "Boston" it's finding Boston in US and I wanted the one in UK.
How to restrict Geocoder to return locations only from one country or maybe from a certain lat lng range?
Thanks
回答1:
UPDATE: This answer may not be the best approach anymore. See the comments below the answer for more details.
In addition to what Pekka already suggested, you may want to concatenate ', UK'
to your address
, as in the following example:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Geocoding only in UK Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px"></div>
<script type="text/javascript">
var mapOptions = {
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: new google.maps.LatLng(54.00, -3.00),
zoom: 5
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var geocoder = new google.maps.Geocoder();
var address = 'Boston';
geocoder.geocode({
'address': address + ', UK'
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position:results[0].geometry.location,
map: map
});
}
});
</script>
</body>
</html>
Screenshot:
I find that this is very reliable. On the other hand, the following example shows that neither the region
parameter, nor the bounds
parameter, are having any effect in this case:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Geocoding only in UK Demo with Bounds</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 300px"></div>
<script type="text/javascript">
var mapOptions = {
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: new google.maps.LatLng(50.00, -33.00),
zoom: 3
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var geocoder = new google.maps.Geocoder();
// Define north-east and south-west points of UK
var ne = new google.maps.LatLng(60.00, 3.00);
var sw = new google.maps.LatLng(49.00, -13.00);
// Define bounding box for drawing
var boundingBoxPoints = [
ne, new google.maps.LatLng(ne.lat(), sw.lng()),
sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
];
// Draw bounding box on map
new google.maps.Polyline({
path: boundingBoxPoints,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
});
// Geocode and place marker on map
geocoder.geocode({
'address': 'Boston',
'region': 'uk',
'bounds': new google.maps.LatLngBounds(sw, ne)
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position:results[0].geometry.location,
map: map
});
}
});
</script>
</body>
</html>
回答2:
The following code will get the first matching address in the UK without the need to modify the address.
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{
'address': address,
'region': 'uk'
}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
for (var i=0; i<results.length; i++) {
for (var j=0; j<results[i].address_components.length; j++) {
if ($.inArray("country", results[i].address_components[j].types) >= 0) {
if (results[i].address_components[j].short_name == "GB") {
return_address = results[i].formatted_address;
return_lat = results[i].geometry.location.lat();
return_lng = results[i].geometry.location.lng();
...
return;
}
}
}
}
});
回答3:
Use componentRestrictions attribute:
geocoder.geocode({'address': request.term, componentRestrictions: {country: 'GB'}}
回答4:
The correct way of doing this is by providing componentRestrictions
For example:
var request = {
address: address,
componentRestrictions: {
country: 'UK'
}
}
geocoder.geocode(request, function(results, status){
//...
});
回答5:
According to the docs, the region parameter seems to set a bias only (instead of a real limit to that region). I guess when the API doesn't find the exact address in the UK place, it will expand it search no matter what region you enter.
I've fared pretty well in the past with specifying the country code in the address (in addition to the region).
I haven't had much experience with identical place names in different countries yet, though. Still, it's worth a shot. Try
'address': '78 Austin Street, Boston, UK'
it should return no address (Instead of the US Boston), and
'address': '78 Main Street, Boston, UK'
Should return the Boston in the UK because that actually has a Main Street.
Update:
How to restrict Geocoder to return locations only from one country or maybe from a certain lat lng range?
You can set a bounds
parameter. See here
You would have to calculate a UK-sized rectangle for that, of course.
回答6:
I found problems with putting ",UK", setting the region to UK and setting bounds. However, doing ALL THREE seems to fix it for me. Here's a snippet:-
var sw = new google.maps.LatLng(50.064192, -9.711914)
var ne = new google.maps.LatLng(61.015725, 3.691406)
var viewport = new google.maps.LatLngBounds(sw, ne);
geocoder.geocode({ 'address': postcode + ', UK', 'region': 'UK', "bounds": viewport }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
.....etc.....
回答7:
I tried with the following:
geocoder.geocode( {'address':request.term + ', USA'}
And its working for me for particular region (US country).
回答8:
I always found this to be pretty easy to filter through since the results can vary and the region doesn't seem to work.
response( $.map( results, function( item ) {
if (item.formatted_address.indexOf("GB") != -1) {
return {
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
}
}
}
回答9:
I prefer a hybrid approach.
- Use componentRestrictions to severely limit to country first.
If that doesn't yield enough results, search more broadly (and re-introduce bias if necessary)
function MyGeocoder(address,region)
{
geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address, 'componentRestrictions': { 'country': region } }, function (r, s) {
if (r.length < 10) geocoder.geocode({ 'address': address /* could also bias here */ }, function (r2, s2) {
for (var j = 0; j < r2.length; j++) r.push(r2[j]);
DoSomethingWithResults(r);
});
else DoSomethingWithResults(r);
});
}
function DoSomethingWithResults(r)
{
// Remove Duplicates
var d = {};
r = r.filter(function (e) { var h = e.formatted_address.valueOf(); var isDup = d[h]; d[h] = true; return !isDup; });
// Do something with results
}
回答10:
I find that for a lot of ambiguous queries, the US always takes precedence in Google, even if you try to tell it where to look. You can look at the response and perhaps ignore it if output country=US?
That is the main reason I stopped using Google Geocoder a while ago and started building my own two years ago.
https://geocode.xyz/Boston,%20UK will always return the UK location. You can make extra sure by adding region=UK: https://geocode.xyz/Boston,%20UK?region=UK
回答11:
For the united kingdom, you have to use GB for region. UK is not the ISO country code!
回答12:
I had to filter results to a country myself today. I found out that the two character country code in componentRestrictions:country: does not work. But the full country name does.
This is the full_name in the address_components from a result.