2011-06-07 50 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