Given a set of existing Marker locations on a Google Maps view, how can I add the points to a PolyLine overlay?
I'm building a web tour application where the user is able to select the stop locations based on a set of GPS coordinates retrieved via an AJAX call. This part works fine, as all the points are showing on the map.
My issue is I want to add ONLY the marker locations to the polyline. Currently the points selected are added to a tourList array, which is converted to a JSON array and posted via a jquery ajax post call. So I know the click event handlers are working for one part.
This question almost fits my needs, except is it intended for Maps API v2, and I'm using V3.
What I've gotten so far:
//page-specific global variables
var visitPoints = new google.maps.MVCArray();
var polyLine;
var map;
window.onload= function(){
//set initial map location
map = new google.maps.Map(
document.getElementById("map"), mapOptions);
//set up polyline capability
var polyOptions = new google.maps.Polyline({
path: visitPoints,
map: map
});
polyLine = new google.maps.Polyline(polyOptions);
polyLine.setMap(map);
//get all the GPS locations in the database This function and makeMarkers works
//as designed
var request = $.ajax({
type:"GET",
url: "includes/phpscripts.php?action=cords",
dataType:"json",
success: makeMarkers
});
//Populate the map view with the locations and save tour stops in array
function makeMarkers(response){
console.log("Response Length: "+response.length)
for (var i=0; i< response.length; i++){
var marker= new google.maps.Marker({
position: new google.maps.LatLng(response[i].lat, response[i].lon),
map: map,
title: response[i].fileName
});
//anonymous function wrapper to create distinct markers
(function(marker){
google.maps.event.addListener(marker, 'click', function(){
tourList.push(marker); //add marker to tour list
visitPoints.push(marker.latlng); //add location to polyline array
console.log("Tour List length- # stops: "+tourList.length);
});
})(marker);
}
}
//listener for poline click
google.maps.event.addListener(map, 'click', updatePolyline)
} //end onload function
//updates the polyline user selections
function updatePolyline(event){
var path = polyLine.getPath();
path.push(event.latlng);
} //end updatePolyline
Currently, I'm not getting any script warnings in Firebug, but the updatePolyline function is never called.
Do I need to add a listener inside the marker listener to update the polyline?