Latest photoblog


Blog » 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.


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



4 Kommentare zu “Geolib.js”

  1. Grafik: Gravatardrwitt:

    Schönes Ding, und noch ein kleines Lehrstück, wie man seine eigene kleine Library baut. Danke, Manuel!

  2. Grafik: Gravatarsebveloper:

    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:
    greets seb

  3. Grafik: GravatarManuel:

    I will take a look at this, thank you!

  4. Grafik: GravatarKeilaron:

    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″…

Die Trackback-URL lautet