Google 地图地点 javascript 文本搜索:无法使用半径

Google Map Places javascript text search : can't get radius to work

我需要获取 google 个地点结果(javascript,而不是地点 api)以显示基于文本搜索的地点列表。

除 radius 参数外,一切正常,如您在此 JSFiddle 中所见。

map = new google.maps.Map(document.getElementById('map'), {
    center: pyrmont,
    zoom: 15
});
var request = {
    location: pyrmont,
    radius: '5000',
    query: cat_search
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);

如果你点击 "Restaurant" 几乎没问题,如果你点击 "Peintre" 它会得到超过 5000 半径的结果,如果你点击 "Fastfood et Snack" 结果会很疯狂,结果从 70000 米半径返回。

注意:我从 jsfiddle 上的脚本 url 中删除了我的 google 地图键。结果与密钥相同。密钥与 google map javascript api ?

不相关

参见 the documentation,位置和半径用于偏向结果,而不是限制结果。如果您不想在半径之外显示结果,您可以将它们从显示的输出中移除。

文档指出:

bounds Type: LatLngBounds Bounds used to bias results when searching for Places (optional). Both location and radius will be ignored if bounds is set. Results will not be restricted to those inside these bounds; but, results inside it will rank higher.

location Type: LatLng|LatLngLiteral The center of the area used to bias results when searching for Places.

radius Type: number The radius of the area used to bias results when searching for Places, in meters.

删除指定半径外结果的代码片段

var map;
var service;

function initMap(param_lat, param_lng, cat_search) {

  var pyrmont = new google.maps.LatLng(param_lat, param_lng);

  map = new google.maps.Map(document.getElementById('map'), {
    center: pyrmont,
    zoom: 15
  });

  var request = {
    location: pyrmont,
    radius: 5000,
    query: cat_search
  };


  service = new google.maps.places.PlacesService(map);
  service.textSearch(request, callback);

  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      var max_iteration = (results.length < 5) ? results.length : 5;
      var resultCount = 0;
      for (var i = 0; i < results.length; i++) {
        if (google.maps.geometry.spherical.computeDistanceBetween(results[i].geometry.location, pyrmont) < request.radius) {
          console.log(results[i]);
          var request2 = {
            placeId: results[i].place_id
          };
          service = new google.maps.places.PlacesService(map);
          service.getDetails(request2, callback2);
          resultCount++;
        }
      }
      if (resultCount == 0) {
        $("#res").prepend("No Results inside search area");
      }
    } else {
      $("#res").prepend("No Results, status " + status);
    }
  }
}



function callback2(place, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    var photos = place.photos;
    if (!photos) {
      temp_var = "<img width='30' src='" + place.icon + "' />";
    } else {
      temp_var = "<img  src='" + photos[0].getUrl({
        'maxWidth': 30,
        'maxHeight': 30
      }) + "' />";
    }
    temp_var += place.name;
    temp_var += " :" + (google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(50.4167, 4.4333), place.geometry.location) / 1000).toFixed(1) + " km [" + place.geometry.location.lat() + ", " + place.geometry.location.lng() + "]<br />";
    $("#res").prepend(temp_var);
  }
}

function toRad(Value) { // Converts numeric degrees to radians
  return Value * Math.PI / 180;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry"></script>
<a onclick="initMap( 50.4167, 4.4333,'Fastfood et Snack');">Fastfood et Snack</a>

<br />
<a onclick="initMap( 50.4167, 4.4333,'Restaurant');">Restaurant</a>

<br />
<a onclick="initMap( 50.4167, 4.4333,'Peintre');">Peintre</a>

<div id="map" style="display:none;"></div>
<div id="res"></div>