2011-09-12 29 views
3

我剛開始使用Google Places API(http://code.google.com/apis/maps/documentation/javascript/places.html),但我無法看到獲得傳遞範圍的語法,而不是latLng對象和半徑。將界限傳遞到Google地點API

在它說的API文檔:

This method takes a request with the following fields: Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangle in which to search; or a location and radius; the former takes a google.maps.LatLng object, and the radius takes a simple integer, representing the circle's radius in meters.

我的代碼如下所示:

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); 

當我運行頁面,我從谷歌服務出現錯誤Uncaught Error: Property bounds not specified。如果我將req對象更改爲

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

它正常工作。任何關於傳遞邊界對象而不是lat/lng/radius的正確語法的想法?

+0

也應該補充說,當我提交 'var req時,我得到同樣的錯誤。 。 .location:bnds' – julio

+0

我得到「物業位置沒有指定。」當只通過界限時。你有沒有解決這個問題?似乎是API中的一個錯誤。 – Gady

回答

-1

也許此代碼段將幫助您

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

+0

這並不能真正解決上述Places API的任何問題。 – Gady

0

我很確定Google Places API沒有邊界選項/參數。我在這裏至少沒有看到它提到的任何地方:https://developers.google.com/places/documentation/

只有半徑似乎。

+0

界限是一個選項,請參閱此https://developers.google.com/maps/documentation/javascript/places#TextSearchRequests –

+1

@Mike,您的鏈接指向Google Places API(一種Web服務,HTTP),此討論涉及Google Maps Api Places Library(前端,Javascript)。在後端API中沒有邊界參數是令人遺憾的。 –

1

該文檔顯示,界定是一個選項:

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

Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangular search > area; or a location and radius; the form .....

我有這個問題,所以我拿出其他參數爲完整的錯誤顯示:

Uncaught Error: Property bounds is invalid (perhaps because of other properties).

因此,取出rankBy或關鍵字或類型參數,直到它工作 - 然後將它們添加回

這些參數爲我工作: var arg = { bounds:map.getBounds(), types:['bar','airport'] };

使類型爲完全,你想要它。

界工作比nearbySearch

0

下面的例子,我在雷達尋找更好的展示瞭如何獲得一個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'); 
    } 
});