Skip to content

Commit

Permalink
added getCenterOfBounds()
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbieh committed May 6, 2016
1 parent 10dc0c4 commit 3e69bf4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
44 changes: 34 additions & 10 deletions dist/geolib.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*! geolib 2.0.20 by Manuel Bieh
* Library to provide geo functions like distance calculation,
* conversion of decimal coordinates to sexagesimal and vice versa, etc.
* WGS 84 (World Geodetic System 1984)
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
* @version 2.0.20
* @license MIT
/*! geolib 2.0.20 by Manuel Bieh
* Library to provide geo functions like distance calculation,
* conversion of decimal coordinates to sexagesimal and vice versa, etc.
* WGS 84 (World Geodetic System 1984)
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
* @version 2.0.20
* @license MIT
**/;(function(global, undefined) {

"use strict";
Expand Down Expand Up @@ -40,7 +40,7 @@
value: 180
},
sexagesimalPattern: {
value: /^([0-9]{1,3})°\s*([0-9]{1,3}(?:\.(?:[0-9]{1,2}))?)'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/
value: /^([0-9]{1,3})°\s*([0-9]{1,3}(?:\.(?:[0-9]{1,2}))?)'\s*(([0-9]{1,3}(\.([0-9]{1,4}))?)"\s*)?([NEOSW]?)$/
},
measures: {
value: Object.create(Object.prototype, {
Expand Down Expand Up @@ -522,6 +522,30 @@

},

/**
* Calculates the center of the bounds of geo coordinates.
*
* On polygons like political borders (eg. states)
* this may gives a closer result to human expectation, than `getCenter`,
* because that function can be disturbed by uneven distribution of
* point in different sides.
* Imagine the US state Oklahoma: `getCenter` on that gives a southern
* point, because the southern border contains a lot more nodes,
* than the others.
*
* @param array Collection of coords [{latitude: 51.510, longitude: 7.1321}, {latitude: 49.1238, longitude: "8° 30' W"}, ...]
* @return object {latitude: centerLat, longitude: centerLng}
*/
getCenterOfBounds: function(coords) {
var b = this.getBounds(coords);
var latitude = b.minLat + ((b.maxLat - b.minLat) / 2);
var longitude = b.minLng + ((b.maxLng - b.minLng) / 2);
return {
latitude: parseFloat(latitude.toFixed(6)),
longitude: parseFloat(longitude.toFixed(6))
};
},


/**
* Computes the bounding coordinates of all points on the surface
Expand Down
Loading

0 comments on commit 3e69bf4

Please sign in to comment.