Charmie
Charmie

Reputation: 2610

Google Map V3 remove polyline overlays

I guess i have to give up now and ask since my deadline is near. Guys, I would like to remove my polylines in my google map. It says in my research is that it's easy as doing this:

myInstance.setMap(null);

However, complication starts since I have this code:

jQuery.ajax({
        type: "POST",  
        url: url,
        async: false, 
        dataType: 'json',
        data: data,
        success: function(result){
            var size = 0, key;
            for (var i = 0; i < result.data.length; i++) {
                if(i==0){
                    var pointA = new google.maps.LatLng( result.data[i].t.latitude, result.data[i].t.longtitude );
                    Polyline.push(pointA);
                }
                if(i == 1){
                    pointA = pointA;
                    var pointB = new google.maps.LatLng( result.data[i].t.latitude, result.data[i].t.longtitude );
                    var color = result.data[i].t.color;
                    Polyline.push(pointB);
                    LinePath = new google.maps.Polyline({
                        path:Polyline,                                                                                                                              
                        strokeColor:color,
                        strokeOpacity:1.0,
                        strokeWeight:5
                        });
                    polyLinePath.push(LinePath); 
                    LinePath.setMap(map);
                }
                if(i>1){
                    pointA = pointB;
                    Polyline = [];
                    Polyline.push(pointA);
                    var pointB = new google.maps.LatLng( result.data[i].t.latitude, result.data[i].t.longtitude );
                    Polyline.push(pointB);
                    var color = result.data[i].t.color;
                    LinePath = new google.maps.Polyline({
                        path:Polyline,                                                                                                                              
                        strokeColor:color,
                        strokeOpacity:1.0,
                        strokeWeight:5
                        }); 
                    LinePath.setMap(map);
                    polyLinePath.push(LinePath);
                    Polyline = [];
                    Coordinates= [];
                }
            }


}


        }

    });

LinePath is being used every now and then, the data that is being fed into this changes and does not retain value of path in it. So everytime I call clearMap();

function clearMap(){
  LinePath.setMap(null);
}

Nothing is being cleared out of my map. I have read related posts and researched about it. I actually have a couple of commented out codes before i posted this here due to my attempts to achieve my goal. Please help

Upvotes: 0

Views: 2319

Answers (1)

Manatok
Manatok

Reputation: 5706

You seem to be creating multiple instances of LinePath and pushing them into polyLinePath.

Try clearing all of them:

function clearMap(){
   if (polyLinePath){
       for (var i=0; i<polyLinePath.length; i++){
          polyLinePath[i].setMap(null);
       }
   }
}

Upvotes: 3

Related Questions