Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created August 7, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/2563836a09b4831eff27283996a4048a to your computer and use it in GitHub Desktop.
Save dingo-d/2563836a09b4831eff27283996a4048a to your computer and use it in GitHub Desktop.
Get map midpoint.
function getMidpoint(point1, point2) {
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
if (typeof(Number.prototype.toDeg) === "undefined") {
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
}
}
var phi1 = point1.lat().toRad();
var lambda1 = point1.lng().toRad();
var phi2 = point2.lat().toRad();
var deltaLambda = (point2.lng()-point1.lng()).toRad();
var Bx = Math.cos(phi2) * Math.cos(deltaLambda);
var By = Math.cos(phi2) * Math.sin(deltaLambda);
var x = Math.sqrt((Math.cos(phi1) + Bx) * (Math.cos(phi1) + Bx) + By * By);
var y = Math.sin(phi1) + Math.sin(phi2);
var phi3 = Math.atan2(y, x);
var lambda3 = lambda1 + Math.atan2(By, Math.cos(phi1) + Bx);
return new google.maps.LatLng(phi3.toDeg(), (lambda3.toDeg()+540)%360-180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment