Google 个地方使用视口而不是半径

Google places use viewport instead of radius

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

即我使用类型限制只在伦敦的 50.000 米半径内获得 nigh_club 并且我没有在我的标记中获得声音部。

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

注意: 搜索请求

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 地图 API 导入几何库。参考。 https://developers.google.com/maps/documentation/javascript/geometry