Reputation: 381
I have a project that i draw polyline on googlemaps using coordinates queried from database. Those coordinates show a cars daily route. When i query another days route, it collides with the previous one. I tried to delete the previous one or just set its map to null or just set its visible property to false. But none worked. Is there any other methods? here is my code:
drawPath = function () {
$.ajax({
type:'POST',
dataType:'json',
data:'cmd=getPath&id='+<?php echo $this->input->get('id'); ?>+'&date='+$("#time").val(),
url: 'index.php/ajax_controller',
success: function(data) {
var pathLatLng,nextLatLng;
var line;
var gBas=0;
var dBas=0;
for (var i = 0; i < data.length-1; i++) {
var lat1=data[i].LAT1.substring(0,2)+"."+data[i].LAT1.substring(2);
var lng1=data[i].LNG1.substring(1,3)+"."+data[i].LNG1.substring(3);
pathLatLng = new google.maps.LatLng(lat1,lng1);
lat1=data[i+1].LAT1.substring(0,2)+"."+data[i+1].LAT1.substring(2);
lng1=data[i+1].LNG1.substring(1,3)+"."+data[i+1].LNG1.substring(3);
nextLatLng = new google.maps.LatLng(lat1,lng1);
var color="#1BE09E";
var opacity={opacity: 0.50};
var path=[pathLatLng,nextLatLng];
var polyLineOpt={
path: path,
strokeColor: color,
strokeOpacity: 1.0,
strokeWeight: 2,
geodesic: true,
map: map
}
line = new google.maps.Polyline(polyLineOpt);
}
},
error:function(){
}
});
}
As you can see i query the coordinates with AJAX. I also tried to use line as a global variable.
Upvotes: 0
Views: 131
Reputation: 12592
It should work when you delete the variable that hold the created polyline hence delete line. But because you create many polyline in the for loop you need to backup all the objects somewhere, most likely an array or an object, too. Then you can delete them all in one loop.
Upvotes: 1