2016-12-04 36 views
0

我正在尋找一種方法將搜索查詢傳遞給谷歌地圖JS API。因此,我只能將搜索框合併到Google地圖中。我正在開發一個應用程序,用戶不必輸入到谷歌。例如,在我的着陸頁上會出現一個「波士頓學校」。點擊它可以突出波士頓的學校。 類似這樣的Schools in Boston通過搜索查詢谷歌地圖JS api

我找不到用戶在搜索框中鍵入的情況下獲得相同功能的方法。 我在等待一個API調用,如https://maps.googleapis.com/maps/api/js?callback=initMap&q=schools+in+boston,但找不到任何。請幫忙。

感謝

+0

這裏的API文檔:https://developers.google.com/maps/documentation/javascript/places# TextSearchRequests和他們提供的代碼: var request = { location:pyrmont, radius:'500', query:'restaurant' }; service = new google.maps.places.PlacesService(map); service.textSearch(request,callback); } –

回答

3

這裏的API文檔:https://developers.google.com/maps/documentation/javascript/places#TextSearchRequests和代碼,他們提供:

var map; 
var service; 
var infowindow; 

function initialize() { 
    var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316); 

    map = new google.maps.Map(document.getElementById('map'), { 
     center: pyrmont, 
     zoom: 15 
    }); 

    var request = { 
    location: pyrmont, 
    radius: '500', 
    query: 'restaurant' 
    }; 

    service = new google.maps.places.PlacesService(map); 
    service.textSearch(request, callback); 
} 

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