且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

google放置api / google地图:“列表by ...”不会重置 - 保持地图区域的混乱

更新时间:2023-08-30 11:18:52

您在代码中多次实例化Places服务,每次地图平移时都是如此。您需要在开始时设置一个名为service的全局变量,然后实例化Places服务一次(*在您的映射初始化过程中将是合适的)。然后,您可以简单地在其他函数中调用 service.search 方法,而不是一遍又一遍地实例化该服务。





  var service; 
\\其他全局变量的复制
函数initializeMap(initialLat,initialLng,detectCurrentLocation,radius,$ b $ 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); //只做一次!
... //其余的代码
}

然后删除你的代码中的 service = new google.maps.places.PlacesService(map); 的其他实例,只需调用 service.search(request,callback) ;


I have a website pune.org that uses places API. As you can see however if I select markers the Listings by lines just keep getting added and eventually make the entire map area useless.

How can I reset the Listings by or ideally to just show Listings by of the marker that is currently selected?

Thanks, Sandeep

You're instantiating the Places Service several times in your code, every time the map pans for example. You need to set a global variable named service at the beginning, then instantiate the Places Service once (*during your map initialization would be appropriate). Then you can simply call the service.search method in your other functions, rather than instantiating the service over and over.

So:

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
}

Then remove the other instances of service = new google.maps.places.PlacesService(map); in your code and only call service.search(request, callback);