本文介绍了Google Maps v3 directionsRenderer.setMap(null)无法清除以前的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从谷歌地图清除以前的路线。但是当你做另一个搜索时,来自先前搜索的Polyline和标记仍然显示。我尝试过使用directionsRenderer.setMap(null),但它没有任何区别。任何想法我做错了什么?

 (function(){
Maps.Directions = function(googleMap,数据,选项){
var directionsService = new google.maps.DirectionsService(),
directionsRenderer = new google.maps.DirectionsRenderer();
$ b $ function getDirections(){
directionsService.route(data.request,function(response,status){
if(status === google.maps.DirectionsStatus.OK){
directionsRenderer.setDirections(response);

$
$ R $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ;
})();


解决方案

这是因为您正在初始化一个新的 directionsRenderer 在函数的每次调用中。

使DirectionsRenderer更具全球性。和首先 setMap(null),然后初始化,然后 setMap(googleMap)



像这样:

 (function(){
var directionsRenderer;
Maps .Directions = function(googleMap,data,options){
if(directionsRenderer){
directionsRenderer.setMap(null);
}
var directionsService = new google.maps.DirectionsService ();

directionsRenderer = new google.maps.DirectionsRenderer();
function getDirections(){
directionsService.route(data.request,function(response,status){
if(status === google.maps.DirectionsStatus.OK){
directionsRenderer.setDirections(response);
}
});
}

directionsRenderer.setMap(googleMap);
getDirections();
};
})();


I am trying to clear previous directions from a Google Map. But when you do another search, the Polyline and markers from the previous search still show. I have tried using directionsRenderer.setMap(null), but it doesn't make any difference. Any ideas what I'm doing wrong?

(function () {
    Maps.Directions = function(googleMap, data, options) {
        var directionsService = new google.maps.DirectionsService(),
            directionsRenderer = new google.maps.DirectionsRenderer();

        function getDirections() {
            directionsService.route(data.request, function(response, status) {
                if (status === google.maps.DirectionsStatus.OK) {
                    directionsRenderer.setDirections(response);
                }
            });
        }
        directionsRenderer.setMap(null);
        directionsRenderer.setMap(googleMap);
        getDirections();
    };
})();
解决方案

This is because you are initializing a new directionsRenderer in every call of the function.

Make the directionsRenderer little more global. and first setMap(null), then initialize, then setMap(googleMap)

Like this:

(function () {
    var directionsRenderer;
    Maps.Directions = function(googleMap, data, options) {
        if(directionsRenderer){
            directionsRenderer.setMap(null);
        }
        var directionsService = new google.maps.DirectionsService();

        directionsRenderer = new google.maps.DirectionsRenderer();
        function getDirections() {
            directionsService.route(data.request, function(response, status) {
                if (status === google.maps.DirectionsStatus.OK) {
                    directionsRenderer.setDirections(response);
                }
            });
        }

        directionsRenderer.setMap(googleMap);
        getDirections();
    };
})();

这篇关于Google Maps v3 directionsRenderer.setMap(null)无法清除以前的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:22