Google位置使用视口而不是半径

Google places use viewport instead of radius

本文关键字:视口 位置 Google      更新时间:2023-09-26

我如何使用viewport来找到与谷歌地图api而不是半径的地方,半径方法对我来说不是很准确。

。我使用类型限制只在伦敦的50,000米半径内得到nigh_club,并且我没有在我的标记中得到ministry of sound。

如果有人有关于如何改进和提出请求的建议,我将很高兴。

注意:搜索请求

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

使用LatLngBounds来定义基于定义视口的SW和NE坐标的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

你可以使用这个函数获得当前的视口半径

我在这里做的是获取google地图中当前视口的中心和东北点,然后我计算这两点之间的距离,就是这样。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
  });
}

注意:确保从google maps API导入几何库。引用https://developers.google.com/maps/documentation/javascript/geometry