【问题标题】:Google Places Web API incorrectly throwing error and breaking maps on nearbySearchGoogle Places Web API 在附近搜索上错误地抛出错误并破坏地图
【发布时间】:2018-01-31 03:02:52
【问题描述】:

我正在尝试从 Places Web API 中提取一些地点,以便在地图上绘制一些标记。似乎它错误地抛出了一个错误:

Uncaught Error: Missing parameter. You must specify location.

in places_impl.js:35

我的代码如下:

var bounds = map.getBounds();
var service = new google.maps.places.PlacesService(map);

service.nearbySearch({
    bounds: bounds,
    type: ['natural_feature']
}, callback);

它应该按照文档工作:

此方法接受具有以下字段的请求:

以下之一: bounds,必须是定义矩形搜索区域的 google.maps.LatLngBounds 对象;

位置和半径;前者采用 google.maps.LatLng 对象,后者采用简单整数,以米为单位表示圆的半径。最大允许半径为 50 000 米。请注意,当 rankBy 设置为 DISTANCE 时,您必须指定位置,但不能指定半径或边界。

https://developers.google.com/maps/documentation/javascript/places#place_search_requests

【问题讨论】:

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


    【解决方案1】:

    问题是你调用服务时bounds无效。

    地图是异步初始化的,直到bounds_changed 事件第一次触发时边界才可用。

    等到'bounds_changed'事件在地图上触发后再使用map.getBounds()

    google.maps.event.addListener(map, 'bounds_changed', function() {
      var bounds = map.getBounds();
      var service = new google.maps.places.PlacesService(map);
    
      service.nearbySearch({
        bounds: bounds,
        type: ['natural_feature']
      }, callback);
    });
    

    proof of concept fiddle

    代码 sn-p:

    var geocoder;
    var map;
    var infowindow = new google.maps.InfoWindow();
    
    function initialize() {
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      google.maps.event.addListener(map, 'bounds_changed', function() {
        var bounds = map.getBounds();
        var service = new google.maps.places.PlacesService(map);
    
        service.nearbySearch({
          bounds: bounds,
          type: ['natural_feature']
        }, callback);
      });
    
      function callback(results, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);
          }
        }
      }
    
      function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });
    
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
      }
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
    <div id="map_canvas"></div>

    【讨论】:

    • 等待 bounds_changed 事件触发完成了这项工作。然而,在文档中:如果地图尚未初始化(即 mapType 仍为 null),或者尚未设置中心和缩放,则结果为 null 或未定义。 在我的情况下,我是正在设置初始化地图和中心和缩放。所以还是很奇怪。无论如何,干杯。
    • 地图是异步初始化的,直到bounds_changed事件第一次触发时边界才可用。
    • 我明白了。感谢您的帮助
    猜你喜欢
    • 2020-08-28
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    相关资源
    最近更新 更多