Snippet so7945210
Multiple Points Along Line between two known Geo Coords?
<!DOCTYPE html>
<!-- http://stackoverflow.com/a/7945210 -->
<!-- Author Jiri @ http://stackoverflow.com/users/805681/jiri -->
<!-- Created 03-Nov-2011 -->
<html>
<head>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">
</script>
<script type="text/javascript">
var map;
function initialize() {
var myOptions = {
zoom: 7,
center: new google.maps.LatLng(47.9, 5.0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var p1 = new google.maps.LatLng(47.362, 8.571);
var p2 = new google.maps.LatLng(48.858, 2.331);
var m1 = createMarker(map, p1, "point 1");
var m2 = createMarker(map, p2, "point 2");
divide(p1, p2, 6);
}
function bearing(lat1, lng1, lat2, lng2) {
var dLon = lng2 - lng1;
var cos_2 = Math.cos(lat2);
var y = Math.sin(dLon) * cos_2;
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*cos_2*Math.cos(dLon);
var theta = Math.atan2(y, x);
return theta;
}
function distance(lat1, lng1, lat2, lng2) {
var R = 6371000; // meters
var dLat = lat2 - lat1;
var dLon = lng2 - lng1;
var sinLat = Math.sin(dLat/2);
var sinLng = Math.sin(dLon/2);
var a = sinLat * sinLat +
sinLng * sinLng * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
function offset(lat1, lng1, bearing, distance) {
var R = 6371000; // meters
var d = distance/R;
var cos_d = Math.cos(d);
var sin_d = Math.sin(d);
var sin_1 = Math.sin(lat1);
var cos_1 = Math.cos(lat1);
var lat2 = Math.asin(sin_1*cos_d + cos_1*sin_d*Math.cos(bearing));
var sin_2 = Math.sin(lat2);
var lng2 = lng1 +
Math.atan2(Math.sin(bearing)*sin_d*cos_1, cos_d-sin_1*sin_2);
return {lat: lat2, lng: lng2};
}
function createMarker(map, point, title) {
return new google.maps.Marker({
map: map,
position: point,
title: title
});
}
function divide(p1, p2, n) {
var lat1 = p1.lat() * Math.PI / 180;
var lng1 = p1.lng() * Math.PI / 180;
var lat2 = p2.lat() * Math.PI / 180;
var lng2 = p2.lng() * Math.PI / 180;
var heading = bearing(lat1, lng1, lat2, lng2);
var D = distance(lat1, lng1, lat2, lng2);
for (var i = 1; i < n; ++i) {
var p = offset(lat1, lng1, heading, i * D/n);
var lat = p.lat * 180 / Math.PI;
var lng = p.lng * 180 / Math.PI;
var latLng = new google.maps.LatLng(lat, lng);
var mc = createMarker(map, latLng, "div " + i);
}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 800px; height: 500px"></div>
</body>
</html>