# Geolib [![Build Status](https://secure.travis-ci.org/manuelbieh/Geolib.png?branch=master)](http://travis-ci.org/manuelbieh/Geolib) A small library to provide some basic geo functions like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc. [View demo](http://www.manuel-bieh.de/publikationen/scripts/geolib/demo.html)
geolib.getDistance( {latitude: 51.5103, longitude: 7.49347}, {latitude: "51° 31' N", longitude: "7° 28' E"} ); geolib.getDistance( {latitude: 51.5103, longitude: 7.49347}, {latitude: "51° 31' N", longitude: "7° 28' E"} ); // Working with W3C Geolocation API navigator.geolocation.getCurrentPosition( function(position) { alert('You are ' + geolib.getDistance(position.coords, { latitude: 51.525, longitude: 7.4575 }) + ' meters away from 51.525, 7.4575'); }, function() { alert('Position could not be determined.') }, { enableHighAccuracy: true } );
var spots = { "Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722}, "Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619}, "London Eye": {latitude: 51.503333, longitude: -0.119722}, "Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778}, "Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945}, "Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675}, "Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567} } geolib.getCenter(spots); geolib.getCenter([ {latitude: 52.516272, longitude: 13.377722}, {latitude: 51.515, longitude: 7.453619}, {latitude: 51.503333, longitude: -0.119722} ]);
geolib.isPointInside( {latitude: 51.5125, longitude: 7.485}, [ {latitude: 51.50, longitude: 7.40}, {latitude: 51.555, longitude: 7.40}, {latitude: 51.555, longitude: 7.625}, {latitude: 51.5125, longitude: 7.625} ] ); // -> true
// checks if 51.525, 7.4575 is within a radius of 5km from 51.5175, 7.4678 geolib.isPointInCircle( {latitude: 51.525, longitude: 7.4575}, {latitude: 51.5175, longitude: 7.4678}, 5000 );
// coords array geolib.orderByDistance({latitude: 51.515, longitude: 7.453619}, [ {latitude: 52.516272, longitude: 13.377722}, {latitude: 51.518, longitude: 7.45425}, {latitude: 51.503333, longitude: -0.119722} ]); // coords object geolib.orderByDistance({latitude: 51.515, longitude: 7.453619}, { a: {latitude: 52.516272, longitude: 13.377722}, b: {latitude: 51.518, longitude: 7.45425}, c: {latitude: 51.503333, longitude: -0.119722} });
var spots = { "Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722}, "Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619}, "London Eye": {latitude: 51.503333, longitude: -0.119722}, "Kremlin, Moscow": {latitude: 55.751667, longitude: 37.617778}, "Eiffel Tower, Paris": {latitude: 48.8583, longitude: 2.2945}, "Riksdag building, Stockholm": {latitude: 59.3275, longitude: 18.0675}, "Royal Palace, Oslo": {latitude: 59.916911, longitude: 10.727567} } // in this case set offset to 1 otherwise the nearest point will always be your reference point geolib.findNearest(spots['Dortmund U-Tower'], spots, 1)
// Calculate distance from Berlin via Dortmund to London geolib.getPathLength([ {latitude: 52.516272, longitude: 13.377722}, // Berlin {latitude: 51.515, longitude: 7.453619}, // Dortmund {latitude: 51.503333, longitude: -0.119722} // London ]); // -> 945235
geolib.getSpeed( {lat: 51.567294, lng: 7.38896, time: 1360231200880}, {lat: 52.54944, lng: 13.468509, time: 1360245600880}, {unit: 'mph'} ); // -> 66.9408 (mph)
geolib.useDecimal("51° 29' 46\" N"); // -> 51.59611111 geolib.useDecimal(51.59611111) // -> 51.59611111