Currently, I'm working on a project that animates a polyline and a circle from one point to another: http://jsfiddle.net/sumeetbansal/7nmz788j/, but I've been having trouble adapting some code I found to run several of these animated polylines simultaneously.
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(22.291, 53.027),
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
el = document.getElementById('map-canvas');
var map = new google.maps.Map(el,mapOptions);
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: '#393'
};
var start = new google.maps.LatLng (32.291, 3.027);
var endpt = new google.maps.LatLng (12.291, 103.027);
var coord = [start, endpt];
var line = new google.maps.Polyline({
path: coord,
strokeColor: '#393',
strokeOpacity: 1,
strokeWeight: 1,
geodesic: true,
map: map,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
});
var startpt = new google.maps.Marker({
position: start,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: '#393'
},
map: map,
});
animateCircle();
var step = 0;
var numSteps = 250;
var timePerStep = 1;
var interval = setInterval(function() {
step += 1;
if (step > numSteps) {
clearInterval(interval);
} else {
var theMotion = google.maps.geometry.spherical.interpolate(start,endpt,step/numSteps);
line.setPath([start, theMotion]);
}
}, timePerStep);
}
function animateCircle() {
var count = 0;
window.setInterval(function() {
count = (count + 1) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
}, 20);
}
google.maps.event.addDomListener(window, 'load', initialize);
Here is the current version of that project: http://jsfiddle.net/sumeetbansal/39gmud42/. I included an array containing new coordinates for the polylines and used loops to go through each set of coordinates, but it doesn't seem to be working after I tried integrating the new code (from here: Plotting multiple polylines on Google Maps) and I've been stumped for the better part of 2 days. Here's the new code below:
var trackLine = [];
var trackLats = [
[
[14.735, -20.595],
[-13.913, 8.188]
],
[
[-14.788, 20.562],
[13.879, -8.230]
],
[
[14.784, -20.546],
[-13.818, 8.288]
],
[
[-14.837, 20.513],
[13.784, -8.329]
],
[
[14.892, -20.439],
[-13.758, 8.350]
]
];
var trackLons = [
[
[76.480, 90.967],
[68.509, 98.386]
],
[
[-115.254, -100.759],
[-123.226, -93.342]
],
[
[53.036, 67.521],
[45.065, 74.937]
],
[
[-138.698, -124.204],
[-146.669, -116.791]
],
[
[29.567, 44.049],
[21.570, 51.438]
]
];
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(22.291, 53.027),
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
el = document.getElementById('map-canvas');
var map = new google.maps.Map(el,mapOptions);
var trackCoords = new google.maps.MVCArray;
var i, j, k;
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: '#0099FF'
};
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
trackCoords = [];
for (k = 0; k < 2; k++) {
trackCoords.push(new google.maps.LatLng(trackLats[i][j][k],
trackLons[i][j][k]));
}
trackLine.push(new google.maps.Polyline({
path: trackCoords,
strokeColor: '#0099FF',
strokeOpacity: 1,
strokeWeight: 1,
geodesic: true,
map: map,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
}));
var startpt = new google.maps.Marker({
position: start,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
strokeColor: '#0099FF'
},
map: map,
});
animateCircle();
var step = 0;
var numSteps = 250;
var timePerStep = 1;
var interval = setInterval(function() {
step += 1;
if (step > numSteps) {
clearInterval(interval);
} else {
var theMotion = google.maps.geometry.spherical.interpolate(start,endpt,step/numSteps);
line.setPath([start, theMotion]);
}
}, timePerStep);
trackCoords.clear;
}
}
function animateCircle() {
var count = 0;
window.setInterval(function() {
count = (count + 1) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
}, 20);
}
google.maps.event.addDomListener(window, 'load', initialize);