谷歌的Geocoder返回错误的国家,忽略该地区的提示 [英] Google's Geocoder returns wrong country, ignoring the region hint

查看:119
本文介绍了谷歌的Geocoder返回错误的国家,忽略该地区的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var geocoder = new google.maps 

我使用Google的Geocoder查找给定地址的纬度坐标。 .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 ()
});

地址变量取自输入字段。



我只想在英国搜寻地点 。我认为指定'region':'uk'应该足够,但不是。当我输入波士顿时,它在美国找到了波士顿,我想要在英国的那个。

如何限制Geocoder仅从一个国家或从一个国家某些纬度范围?



谢谢

解决方案

UPDATE :这个答案可能不再是最好的方法。 Pekka已经建议,您可能想连接',UK'地址,如下例所示:

 < ;!DOCTYPE html> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title> Google地图仅在英国地区进行地理编码演示< / title>
< script src =http://maps.google.com/maps/api/js?sensor=false
type =text / javascript>< / script>
< / head>
< body>
< div id =mapstyle =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({$ b $'address':address +',UK'
},
函数(结果,状态){
if(状态== 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-typecontent =text / html; charset = UTF-8/>
< title> Google地图仅在英国进行地理编码带边界的演示< / title>
< script src =http://maps.google.com/maps/api/js?sensor=false
type =text / javascript>< / script>
< / head>
< body>


< 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();

//定义英国的东北部和西南部点
var ne = new google.maps.LatLng(60.00,3.00);
var sw = new google.maps.LatLng(49.00,-13.00);

//定义绘制边界框
var boundingBoxPoints = [
ne,new google.maps.LatLng(ne.lat(),sw.lng()),
sw,new google.maps.LatLng(sw.lat(),ne.lng()),ne
];

//在地图上绘制边界框
new google.maps.Polyline({
path:boundingBoxPoints,
strokeColor:'#FF0000',
strokeOpacity:1.0,
strokeWeight:2,
map: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>


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

解决方案

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>

这篇关于谷歌的Geocoder返回错误的国家,忽略该地区的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆