0

我使用 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>

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

4

1 回答 1

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

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

于 2013-03-31T14:51:27.847 回答