【发布时间】:2012-06-14 14:00:57
【问题描述】:
我有一个使用地点 API 的网站 pune.org。但是,正如您所看到的,如果我选择标记,则按行列出的列表只会不断添加并最终使整个地图区域变得无用。
如何重置列表,或者理想情况下仅显示当前选择的标记的列表?
谢谢, 桑迪普
【问题讨论】:
标签: google-maps-api-3 google-places-api
我有一个使用地点 API 的网站 pune.org。但是,正如您所看到的,如果我选择标记,则按行列出的列表只会不断添加并最终使整个地图区域变得无用。
如何重置列表,或者理想情况下仅显示当前选择的标记的列表?
谢谢, 桑迪普
【问题讨论】:
标签: google-maps-api-3 google-places-api
您在代码中多次实例化 Places Service,例如每次地图平移时。您需要在开始时设置一个名为 service 的全局变量,然后实例化 Places Service 一次(*在您的地图初始化期间是合适的)。然后您可以在其他函数中简单地调用service.search 方法,而不是一遍又一遍地实例化服务。
所以:
var service;
\\Rest of other global variables
function initializeMap(initialLat, initialLng, detectCurrentLocation, radius,
zoomLevel, defaultType) {
initialLocation = new google.maps.LatLng(initialLat, initialLng);
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom : zoomLevel,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
service = new google.maps.places.PlacesService(map); //Only do this once!
...//rest of your code
}
然后删除代码中service = new google.maps.places.PlacesService(map); 的其他实例,只调用service.search(request, callback);
【讨论】: