2016-12-04 106 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]); 
    } 
    } 
}