JavaScript - Geometry

From NoskeWiki
Jump to navigation Jump to search


NOTE: This page is a daughter page of: JavaScript

Here's some code for calculating latitude and longitude information in JavaScript. Al the code below has been taken from this fabulous page by Movable Type Scripts, then turned into functions and use the Google Maps Javascript API V3 Reference.

Halfway between two points (longitude/latitude)

 * Returns the exact longitude and latitudes halfway between two
 * latitude/longitude pairs on the globe. Note that this "closest"
 * point, to two other points may sometimes cross over the north or
 * south pole.
 * @param {!number} latitude1 The latitude of the first point (in degress).
 * @param {!number} longitude1 The longitude of the first point.
 * @param {!number} latitude2 The latitude of the second point.
 * @param {!number} longitude2 The latitude of the second point.
 * @return {Object}  // google.maps.LatLng.
var midPoint = function(latitude1, longitude1, latitude2, longitude2) {
  var DEG_TO_RAD = Math.PI / 180;     // To convert degrees to radians.

  // Convert latitude and longitudes to radians:
  var lat1 = latitude1 * DEG_TO_RAD;
  var lat2 = latitude2 * DEG_TO_RAD;
  var lng1 = longitude1 * DEG_TO_RAD;
  var dLng = (longitude2 - longitude1) * DEG_TO_RAD;  // Diff in longtitude.

  // Calculate mid-point:
  var bx = Math.cos(lat2) * Math.cos(dLng);
  var by = Math.cos(lat2) * Math.sin(dLng);
  var lat = Math.atan2(
      Math.sin(lat1) + Math.sin(lat2),
      Math.sqrt((Math.cos(lat1) + bx) * (Math.cos(lat1) + bx) + by * by));
  var lng = lng1 + Math.atan2(by, Math.cos(lat1) + bx);

  return new google.maps.LatLng(lat / DEG_TO_RAD, lng / DEG_TO_RAD);

Distance between two points (longitude/latitude)

 * Returns the distance in kilometers between two coordinates.
 * @see:
 * @param {!number} lat1 The latitude of the first point (in degrees).
 * @param {!number} lng1 The longitude of the first point.
 * @param {!number} lat2 The latitude of the second point.
 * @param {!number} lng2 The longitude of the second point.
 * @return {number} The distance in kilometers between the points.
var distanceBetweenPoints = function(lat1, lng1, lat2, lng2) {
  var RADIUS_EARTH = 6371;            // Radius of the earth in kilometers.
  var DEG_TO_RAD = Math.PI / 180;     // To convert degrees to radians.

  var dLat = (lat2 - lat1) * DEG_TO_RAD;
  var dLng = (lng2 - lng1) * DEG_TO_RAD;
  lat1 = lat1 * DEG_TO_RAD;
  lat2 = lat2 * DEG_TO_RAD;

  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.sin(dLng / 2) * Math.sin(dLng / 2) *
          Math.cos(lat1) * Math.cos(lat2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

  return RADIUS_EARTH * c;

Code license
For all of the code on my site... if there are specific instruction or licence comments please leave them in. If you copy my code with minimum modifications to another webpage, or into any code other people will see I would love an acknowledgment to my site.... otherwise, the license for this code is more-or-less WTFPL (do what you want)! If only copying <20 lines, then don't bother. That said - if you'd like to add a web-link to my site or (better yet) the specific page with code, that's a really sweet gestures! Links to the page may be useful to yourself or your users and helps increase traffic to my site. Hope my code is useful! :)