谷歌的雷达搜索搜索矩形而不是圆形区域?

时间:2013-03-31 13:56:32

标签: google-maps google-maps-api-3 google-places

我使用Google的雷达搜索从某个区域获取一些标记。我使用这样的查询:

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>

为什么会这样?不应给出半径,在圆形区域内给出结果并给出边界给出矩形区域的结果吗?

1 个答案:

答案 0 :(得分: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
   });
}

仍然不确定这种奇怪行为的原因。