My goal is to have a single line drawn from the closest marker to a static position. I can determine which marker is the closest by sorting the distances, but if a new marker is added and is closer than the previous closest one, a new polyline is drawn with the old one still present.
var sorted = pathArr.sort(function (a, b) {
var aValue = Math.abs(parseFloat(a['distance']));
var bValue = Math.abs(parseFloat(b['distance']));
if (typeof aValue && bValue == "number") {
return (aValue > bValue) ? 1 : ((bValue > aValue) ? -1 : 0);
}
return (aValue > bValue) ? 1 : ((bValue > aValue) ? -1 : 0);
});
console.log(sorted);
pathCoordinate = [{lat: lat1, lng: lon1}, {lat: sorted[0].lat, lng: sorted[0].lng}];
line = new google.maps.Polyline({
path: pathCoordinate,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
line.setMap(map);
I've attempted using
line.setMap(null);
without success.