6by*_*tes 30 javascript gps google-maps geolocation geocode
我正在使用Google的Geocoder来查找给定地址的lat lng坐标.
    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 变量取自输入字段.
我想只在英国搜索位置.我认为指定'region': 'uk'应该足够但不是.当我输入"波士顿"时,它在美国找到波士顿,我想在英国找到它.
如何限制Geocoder仅从一个国家或某个纬度范围返回位置?
谢谢
Ivo*_*cky 27
以下代码将获得英国第一个匹配的地址,而无需修改地址.
  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;
                    }
                }
            }
        }
    });
小智 26
geocoder.geocode({'address': request.term, componentRestrictions: {country: 'GB'}}
Dan*_*llo 22
更新:这个答案可能不再是最好的方法了.有关更多详细信息,请参阅答案下方的评论.
除了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>
Dem*_*tix 16
这样做的正确方法是提供componentRestrictions
例如:
var request = {
    address: address,
    componentRestrictions: {
        country: 'UK'
    }
}
geocoder.geocode(request, function(results, status){
    //...
});
根据文档,区域参数似乎只设置偏差(而不是对该区域的实际限制).我想当API在英国找不到确切的地址时,无论你输入什么地区,它都会扩展搜索范围.
我过去表现得非常好,在地址中指定了国家代码(除了该地区).不过,我对不同国家的相同地名没有多少经验.不过,值得一试.尝试
'address': '78 Austin Street, Boston, UK'
它应该不返回地址(而不是美国波士顿),和
'address': '78 Main Street, Boston, UK'
应归还英国的波士顿,因为它实际上有一条主街.
更新:
如何限制Geocoder仅从一个国家或某个纬度范围返回位置?
您可以设置bounds参数.看到这里
当然,你必须为此计算一个英国大小的矩形.
我发现将",英国",将该地区设置为英国并设置界限存在问题.然而,做所有这三件似乎都适合我.这是一个片段: -
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.....
| 归档时间: | 
 | 
| 查看次数: | 35520 次 | 
| 最近记录: |