【问题标题】:Google places use viewport instead of radiusGoogle 地方使用视口而不是半径
【发布时间】:2015-12-08 06:43:49
【问题描述】:

如何使用视口使用 google maps api 而不是半径来查找地点,半径方法对我来说不太准确。

即我使用类型限制仅在伦敦 50.000 米半径范围内获得 nigh_club,但我的标记中没有得到声音部。

如果有人对如何改进和提出请求有任何建议,我会很高兴。

注意:搜索请求

var request = {
  location: location,
  radius: 3000,
  //types: ['night_club, bar']
  keyword: 'night+club'
};

【问题讨论】:

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


    【解决方案1】:

    使用LatLngBounds 根据定义视口的西南和东北坐标来定义location,而不是LatLng + radius

    var viewport = new google.maps.LatLngBounds(
        new google.maps.LatLng(southLat, westLng), // SW
        new google.maps.LatLng(northLat, eastLng)  // NE
    );
    
    var request = {
        location: viewport,
        // types: ['night_club', 'bar'] 
        keyword: 'night club'
    };
    

    https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds

    【讨论】:

    • 你有例子吗?
    • 刚刚在答案中添加了一个示例。它没有经过测试,但你明白了;)
    【解决方案2】:

    您可以使用此函数获取当前视口半径

    我在这里做的是在谷歌地图中获取当前视口的中心和东北点,然后我计算这两个点之间的距离,就是这样。结果在meters中给出

    var bounds = window.map.getBounds();
    var ne_bounds = bounds.getNorthEast();
    var center = bounds.getCenter();
    var radius = google.maps.geometry.spherical.computeDistanceBetween(center, ne_bounds);
    

    您也可以通过创建这样的圆圈来检查这一点

    function draw_map_bounds_circle() {
      var bounds = window.map.getBounds();
      var ne_bounds = bounds.getNorthEast();
      var circle_center = bounds.getCenter();
      var circle_radius = google.maps.geometry.spherical.computeDistanceBetween(circle_center, ne_bounds);
      
      const newCircle = new google.maps.Circle({
        strokeColor: "#7CA0C0",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: "#7CA0C0",
        fillOpacity: 0.35,
        map: window.map,
        center: circle_center,
        radius: circle_radius
      });
    }
    

    注意: 确保从谷歌地图 API 导入几何库。参考。 https://developers.google.com/maps/documentation/javascript/geometry

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2011-10-15
      • 2016-06-09
      • 2017-12-02
      • 2019-06-25
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多