google places api/google maps: “listings by…” doesn't get reset-keeps cluttering the map area [closed]

寵の児 提交于 2019-12-11 16:27:52

问题


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


回答1:


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);



来源:https://stackoverflow.com/questions/11034766/google-places-api-google-maps-listings-by-doesnt-get-reset-keeps-clutteri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!