2013-03-31 42 views
0

我使用谷歌的雷达搜索从一个地区获得一些标记。我用一个查询喜欢这样的:谷歌的radarSearch搜索矩形而不是圆形区域?

const location=new google.maps.LatLng(59.33, 18.0674); 
const radius = 2000; 

var request={ 
    location: location, 
    radius: radius, 
    types: ["bus_station"] 
}; 
// ... 

的问题是从一个矩形区域,而不是一个圆形区域的查询返回结果。要看到更清楚,我画一个圆:

var circOptions={ 
    center: location, 
    radius: radius, 
    map: map 
}; 
circle = new google.maps.Circle(circOptions); 

这里是我的整个HTML代码(包括脚本&风格):

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&language=se"></script> 
    <script> 

    function init() { 

     const location=new google.maps.LatLng(59.33, 18.0674); 
     const radius = 2000; 

     // Make map 
     var mapOptions = { 
     center: location, 
     zoom: 11, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById('map'), mapOptions); 

     // Make a circle 
     var circOptions={ 
      center: location, 
      radius: radius, 
      map: map 
     }; 
     circle = new google.maps.Circle(circOptions); 

     // Get shops in radius 
     service = new google.maps.places.PlacesService(map); 
     var request={            //my request 
     location: location, 
     radius: radius, 
     types: ["bus_station"] 
     }; 
     service.radarSearch(request, function (results, status){ 
     if (status == google.maps.places.PlacesServiceStatus.OK){ 
      for (var i= 0, result; result=results[i]; i++) {  //add markers 
       var marker = new google.maps.Marker({ 
         map: map, 
         position: result.geometry.location, 
         reference: result.reference 
       }); 
      } 
     } 
     }); 

    } 
    google.maps.event.addDomListener(window, 'load', init); 

    </script> 
    <style>#map {height: 500px;width: 500px;}</style> 
</head> 
<body><div id="map"></div></body> 
</html> 

这究竟是为什么?不应给出半径,在圆形区域内给出结果,并给出矩形区域的结果?

回答

0

快速修复,我发现是做标记时添加

var distance=google.maps.geometry.spherical.computeDistanceBetween(location, result.geometry.location); 
    if (distance>radius) 
    continue; 

刚下for。所以它应该看起来像这样:

for (var i= 0, result; result=results[i]; i++) {  //add markers 
    distance=google.maps.geometry.spherical.computeDistanceBetween(location, result.geometry.location); 
    if (distance>radius) 
     continue; 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: result.geometry.location, 
     reference: result.reference 
    }); 
} 

虽然这种奇怪的行为,但仍然不确定。