【问题标题】:Google's radarSearch searches in rectangular instead of circular area?谷歌的radarSearch搜索矩形而不是圆形区域?
【发布时间】:2013-03-31 13:56:32
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我发现的一个快速解决方法是添加

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多