JSFiddle - React, Tailwind, and code Playground
HTML
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<!-- CHECK ELEMENT ID also UPDATE CSS -->
<div id="map_canvas"></div>
CSS
html, body, #map_canvas { margin: 0; padding: 0; height: 100% }
JavaScript
var map;
var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP };
var markers = [];
function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
from1 = new google.maps.LatLng(0,0);
to1 = new google.maps.LatLng(30,12);
speed1 = 1.5
from2 = new google.maps.LatLng(-30,15);
to2 = new google.maps.LatLng(10,-100);
speed2 = 1.25;
from3 = new google.maps.LatLng(0,-50);
to3 = new google.maps.LatLng(0,50);
speed3 = 1.1
addMarker(from1,to1, speed1);
addMarker(from2,to2, speed2);
addMarker(from3,to3, speed3);
}
function addMarker(pos, dest, spd) {
var marker = new google.maps.Marker({
map: map,
position: pos,
destination: dest
});
// FORK // Removed 'on click' to execute immediately and subsituted 'this' to 'marker'
fromLat = marker.position.lat();
fromLng = marker.position.lng();
toLat = marker.destination.lat();
toLng = marker.destination.lng();
// store a LatLng for each step of the animation
frames = [];
// FORK // Increase percent * speed
for (var percent = 0; percent < 1; percent += (0.0025 * spd)) {
curLat = fromLat + percent * (toLat - fromLat);
curLng = fromLng + percent * (toLng - fromLng);
frames.push(new google.maps.LatLng(curLat, curLng));
}
move = function(marker, latlngs, index, wait, newDestination) {
marker.setPosition(latlngs[index]);
if(index != latlngs.length-1) {
// call the next "frame" of the animation
setTimeout(function() {
move(marker, latlngs, index+1, wait, newDestination);
}, wait);
}
...