【问题标题】:passing bounds to google places API将边界传递给谷歌地方 API
【发布时间】:2011-09-12 19:39:16
【问题描述】:

我刚刚开始使用 Google Places API (http://code.google.com/apis/maps/documentation/javascript/places.html),但我似乎无法获得正确的语法传入边界而不是 latLng 对象和半径。

在 API 文档中它说:

此方法接受具有以下字段的请求: 任一个: bounds,它必须是定义要搜索的矩形的 google.maps.LatLngBounds 对象;或者 位置和半径;前者采用 google.maps.LatLng 对象,半径采用简单整数,以米为单位表示圆的半径。

我的代码如下所示:

var bnds      = map.getBounds();
console.log(bnds); // this returns a valid bounds object!
var req       = {
                 bounds: bnds
              };
var service = new google.maps.places.PlacesService(map);
service.search(req, callback);

当我运行该页面时,我从 Google 服务收到错误 Uncaught Error: Property bounds not specified。如果我将 req 对象更改为

var req    = {
               location: latlngobject,
               radius: '500'
             }

它工作正常。关于传递边界对象而不是纬度/经度/半径的正确语法的任何想法?

【问题讨论】:

  • 还应该补充一点,我在提交var req. . .location: bnds时遇到同样的错误
  • 我收到“未指定物业位置”。仅在界内传球时。你有没有解决过这个问题?似乎是 API 中的一个错误。

标签: javascript google-maps-api-3


【解决方案1】:
【解决方案2】:

文档 do 表明 bounds 是一个选项:

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

以下之一: bounds,必须是定义矩形搜索 > 区域的 google.maps.LatLngBounds 对象;或者 位置和半径;表格.....

我遇到了这个问题,所以我取出了其他参数,如完整错误所示:

未捕获的错误:属性边界无效(可能是因为其他属性)。

所以取出 rankBy 或关键字或类型参数,直到它起作用 - 然后将它们添加回来

这些参数对我有用: var arg = { bounds:map.getBounds(), types:['bar','airport'] };

根据需要使类型尽可能完整。

Bounds 在雷达搜索中比在附近搜索中更适合我

【讨论】:

    【解决方案3】:

    我很确定 Google Places API 没有边界选项/参数。我至少在这里没有看到任何地方提到它:https://developers.google.com/places/documentation/

    似乎只有半径。

    【讨论】:

    • @Mike,您的链接指向 Google Places API(网络服务,HTTP),本次讨论涉及 Google Maps Api Places Library(前端,Javascript)。遗憾的是,de 后端 API 中没有 bounds 参数。
    【解决方案4】:

    以下示例向您展示如何从 Geocoder() 结果中获取边界,并将您的 PlacesService() 结果限制在该范围内,并分配适当的默认位置标记。

    var geocoder = new google.maps.Geocoder(),
        map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }),
        rect = new google.maps.Rectangle({
            strokeColor: '#FF0000',
            strokeOpacity: 0.3,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.33,
            map: map
        }),
        markers_amount = 10,
        markers = [];
    
    // GEOCODE
    geocoder.geocode({
        'address': 'Vienna'
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var geometry = results[0].geometry,
                places = new google.maps.places.PlacesService(map);
    
            map.setCenter( bounds.getCenter() );
            // SETS THE BOUNDS HERE
            rect.setBounds( bounds );
    
            places.search({
                bounds: bounds,
                // https://developers.google.com/places/documentation/supported_types
                types: [
                    // Until there's enough meta data for Google, 
                    // they label everything as an 'establishment'.
                    'establishment'
                ]
            }, function (results, status) {
                if (status == google.maps.places.PlacesServiceStatus.OK) {
                    console.log( results );
                    for (var i = 0; i < markers_amount; i++) {
                        markers.push(new google.maps.Marker({
                            position: results[i].geometry.location,
                            map: map,
                            icon: image = {
                                url: results[i].icon,
                                size: new google.maps.Size(71, 71),
                                origin: new google.maps.Point(0, 0),
                                anchor: new google.maps.Point(17, 34),
                                scaledSize: new google.maps.Size(25, 25)
                            }
                        }));
                    }
                }
            });
            //console.log(markers);
        } else {
            return $('#map').html('could not locate you');
        }
    });
    

    【讨论】:

      【解决方案5】:

      也许这段代码 sn-p 会帮助你

      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      var lngSpan = northEast.lng() - southWest.lng();
      var latSpan = northEast.lat() - southWest.lat();

      【讨论】:

      • 这并不能真正解决上述 Places API 的任何问题。
      猜你喜欢
      • 2019-04-19
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多