Skip to content

Instantly share code, notes, and snippets.

Created August 11, 2014 23:48
Show Gist options
  • Save anonymous/ce32901c1850f5aaea8b to your computer and use it in GitHub Desktop.
Save anonymous/ce32901c1850f5aaea8b to your computer and use it in GitHub Desktop.
// Fast Polyline
var fastTrack;
var fastWaypoints = Array();
var fastDistance = 0;
var optimize = function(turnpoints, google, map) {
var headings = Array();
fastWaypoints = Array();
for (var i = 0; i < turnpoints.length; i++) {
// For all turnpoint excpet the last one.
if (i < turnpoints.length - 1) {
// Getting the heading.
var heading = google.maps.geometry.spherical.computeHeading(turnpoints[i].latLng, turnpoints[i + 1].latLng);
// Unsure heading is always positive.
if (heading < 0) heading += 360;
if (headings.length >= 1) {
// Switch first heading from 180°.
var pastHeading = headings[i- 1];
// We need to catch the right angle !!!
if (pastHeading > heading) {
pastHeading -= 180;
} else {
pastHeading += 180;
}
// Now we can get the average heading. (Bisectrix).
var middleHeading = (pastHeading + heading) / 2;
// Offset from the center to the radius to get the intermediary point.
var fastPoint = google.maps.geometry.spherical.computeOffset(turnpoints[i].latLng, turnpoints[i].radius, middleHeading);
}
}
else {
// For the first point just offset form the center to its radius following the heading of next turnpoint.
var fastPoint = google.maps.geometry.spherical.computeOffset(turnpoints[i].latLng, turnpoints[i].radius, heading);
}
headings.push(heading);
fastWaypoints.push(fastPoint);
incrementDistance(google, fastWaypoints, 'fast');
}
}
// For the last point just reverse the last heading from the center and offset to the radius.
if (headings.length >= 1) {
var newPoint = google.maps.geometry.spherical.computeOffset(turnpoints[i - 1].latLng, turnpoints[i - 1].radius, headings[headings.length - 1] - 180);
fastWaypoints.push(newPoint);
incrementDistance(google, fastWaypoints, 'fast');
}
if (fastTrack) fastTrack.setMap(null);
fastTrack = new google.maps.Polyline({
path: fastWaypoints,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
map:map
)};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment