2016-06-07 28 views
0

已更新: 我試圖將多個類型的Google地點API請求作爲數組發送。但是,我無法將數組變量與從複選框值添加的位置列表一起傳遞。任何幫助將不勝感激。Google Places API - 使用複選框中的多個地點類型

這工作:

var poi = new google.maps.LatLng(lt,lg); 
var gplaces = ['shopping_mall','bus_station']; 

var request = { 
    location: poi, 
    radius: '500', 
    types: gplaces 
    }; 

但不是這樣的:

var poi = new google.maps.LatLng(lt,lg); 
var gplaces = [] 

$('#chklist:checked').each(function() { 
    gplaces.push($(this).val()); 
}); 

var request = { 
    location: poi, 
    radius: '500', 
    types: gplaces 
    }; 

在第二種情況下,該API將返回的地方默認列表(如果我沒有通過任何類型的所有)。

我試過一個console.log(gplaces),它包含['shopping_mall','bus_station']。

我也檢查了console.log(請求),我可以看到請求中列表的各個元素。

有人可以幫助我嗎?

+0

你能粘貼更多的代碼嗎?它對我很好用 – weigreen

+0

@weigreen我已經更新了現在的問題。請檢查。 – Hrishikesh

+0

你可以創建一個小提琴嗎? – weigreen

回答

0

我試過了你的兩個代碼,它適用於我。也許問題不在那裏。嘗試再次檢查它以確認錯誤的位置。

這裏是我測試的樣本。

infowindow = new google.maps.InfoWindow(); 
var service = new google.maps.places.PlacesService(map); 
var gplaces = ['shopping_mall','bus_station'] 
service.nearbySearch({ 
location: pyrmont, 
radius: '500', 
types: gplaces 
}, callback); 

在這裏,他們都在工作。

infowindow = new google.maps.InfoWindow(); 
var service = new google.maps.places.PlacesService(map); 
service.nearbySearch({ 
location: pyrmont, 
radius: 500, 
types: ['shopping_mall','bus_station'] 
}, callback); 

也用於附加信息,則types參數被棄用2016年2月16日,通過只支持一個type每搜索請求的新類型參數替換。 (但是,這些類型可能仍會在搜索結果中返回)。將支持使用已棄用功能的請求,直至2017年2月16日,之後所有文本搜索都必須使用新的實現。

只是提醒這個deprecation announcement

+0

對不起,你是對的。傳遞一個數組變量確實工作正常。然而,就我而言,數組('gplaces')從複選框中獲取值。我現在已經更新了這個問題。請再看一遍。 – Hrishikesh