更新:这个答案可能不再是最好的方法了。有关更多详细信息,请参阅答案下方的评论。
除了Pekka 已经建议的内容之外,您可能还想连接', UK'
到您的address
,如下例所示:
<!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>
截屏:
我发现这是非常可靠的。另一方面,以下示例表明,在这种情况下,region
参数和bounds
参数都没有任何影响:
<!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>