我正在构建一个移动Web应用程序,允许您记录给定位置的行为。为了让用户更容易输入,我想使用 Google 地方信息将附近商家的快速列表生成到表单中。
我已经在这里阅读了文档:在Android中使用Google Places API,但我在这里和其他地方可以找到的大部分内容都是特定于Android而不是HTML/JS
的任何帮助将不胜感激。
您需要加载位置库(这是对google.maps.api库的正常调用的额外参数)。
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization,drawing,places"></script>
然后创建一个 PlacesService:
var PlacesService = new google.maps.places.PlacesService(map);
有很多方法可以搜索地点。在此示例中,我将在当前地图边界内进行搜索
PlacesService.nearbySearch({
bounds: map.getBounds(),
//types: ['lawyer']
}, function(results, status, pagination) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for(var i=0;i<results.length;i++) {
place=results[i];
var marker=new google.maps.Marker({position: place.geometry.location, icon:place.icon, map:map});
}
if (pagination.hasNextPage) {
// in case you get more markers to fetch
pagination.nextPage();
}
} else {
console.log(results, status);
}
});