2011-06-07 53 views
0

當調用搜索服務API谷歌地圖Javascript API第3:搜索請求

var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316); 
var request = { location: pyrmont, radius: '500', types: ['store'] }; 
service = new google.maps.places.PlacesService(map); 
service.search(request, callback);
  1. 是有必要給搜索區域的位置/半徑?
  2. 那麼如果我想搜索一個位於全球不在指定區域的位置,如google在http://maps.google.com/處怎麼樣。

--- UPDATE ---

這是我更新的代碼爲@Trott建議,但我在我的回調函數獲取狀態= 「ZERO_RESULTS」。

var keyword='search placename'; 
var request = { name : keyword, 
bounds : new google.maps.LatLngBounds(
google.maps.LatLng(-89.999999,-179.999999), 
google.maps.LatLng(89.999999,179.999999) 
)}; 
service = new google.maps.places.PlacesService(map); 
service.search(request, callback); 

function callback(results, status) 
{ 
    console.log("status="+status); 
    if (status == google.maps.places.PlacesServiceStatus.OK) 
    { 
     for (var i = 0; i < results.length; i++) 
     console.log(results[i]); 
    } 
} 

我做錯了什麼?

+0

在走的權利,但確實很快:我強烈懷疑爲「搜索地名」一谷歌搜索的地方會拿出空的,尤其是考慮如何疏Google地方資訊數據庫目前正在使用。嘗試搜索「博物館」,看看是否得到任何結果。 – Trott 2011-06-23 00:52:57

回答

4
  1. 呼叫至search()必須包含一個位置和半徑,或者一個邊界(作爲對象的LatLngBounds)。這記錄在http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests

  2. 如果您指定一個涵蓋整個世界的LatLngBnds,您會看到會發生什麼。還沒有嘗試過,但如果它起作用,那麼它應該具有搜索整個世界的效果,而不必偏向特定的位置。

LatLngBounds記錄在http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds。我想你會想要做這樣的事情:

var sw = new google.maps.LatLng(-89.999999,-179.999999); 
var ne = new google.maps.LatLng(89.999999,179.999999); 
var bounds = new google.maps.LatLngBounds(sw,ne); 
+0

感謝@Trott對你的迴應,我得到了你的2分,但很遺憾地說,沒有足夠的關於LatLong的知識來構建覆蓋整個世界的界限。你可以幫我做這件事嗎。謝謝。 – 2011-06-07 18:18:35

+0

+1爲你的第二點 – 2011-06-07 18:18:58

+0

好的,我添加了代碼,**我認爲**應該創建一個覆蓋(幾乎)整個世界的邊界。 – Trott 2011-06-07 20:43:50