Geolib.js
I created a small JavaScript library to provide some basic geo functions like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc.
Usage:
To calculate distance between two geo coordinates
geolib.getDistance({"latitude": 51.511928, "longitude": 7.463536}, {"latitude": 51.510318, "longitude": 7.524133}, 10); // -> 4200 (Accuracy 10m)
Takes 2 or 3. First 2 arguments must be an object with a latitude and a longitude property (e.g. {latitude: 52.518611, longitude: 13.408056}). Coordinates can be in sexagesimal or decimal format. 3rd argument is accuracy (in meters). So a calculated distance of 1248 meters with an accuracy of 100 is returned as 1200.
Return value is always an integer and represents the distance in meters.
To convert it into miles use:
geolib.convertUnit('mi', value)
Convert sexagesimal to decimal
geolib.sexagesimal2decimal("51° 29' 46\" N"); // -> 51.49611111
Convert decimal to sexagesimal
geolib.decimal2sexagesimal(51.49611111); // -> 51° 29' 46.00
Download:
https://github.com/manuelbieh/geolib
Demo:
http://www.manuel-bieh.de/publikationen/scripts/geolib/demo.html
Veröffentlicht: 6.08.2011, 12:45 Uhr
Rubrik:
Tags: Geo, Geolocation, Javascript
Diskussion: 4 Kommentare
Social Media:
4 Kommentare zu “Geolib.js”
Die Trackback-URL lautet
August 6th, 2011 at 17:07
Schönes Ding, und noch ein kleines Lehrstück, wie man seine eigene kleine Library baut. Danke, Manuel!
August 23rd, 2011 at 18:08
Hey, I just checked out your geo lib and i must say that your distance equation is very inaccurate. The most accurate approximation is the vincenty formula. Check out this site to improve your lib: http://www.movable-type.co.uk/scripts/latlong-vincenty.html
greets seb
August 23rd, 2011 at 18:11
I will take a look at this, thank you!
Juli 5th, 2013 at 20:25
Mobile Safari / iOS exhibits a rounding/float imprecision error which causes useDecimal to think a long decimal isn’t decimal, e.g. -79.55374145507812 becomes -79.55374145507813. When it comes to geolocation, though, a decimal at that position doesn’t really matter… we worked around this by roughly truncating it and reparsing it (because a trailing 0 will also trigger this exception!).
It was hard to track down, because all Safari logs is „Unknown format. from undefined at line 0″…