ng2-haversine
An Angular 2 library to calculate the distance between a pair of coordinates using the Haversine formula.
Getting started
Install ng2-haversine
using npm
within your Angular 2 app directory:
$ npm install ng2-haversine --save
Once the package is installed, set the provider for HaversineService
in your AppModule
:
;
Now you're ready to use ng2-haversine
!
How to
You can use HaversineService
from a Service or from a Component: just inject it as a dependency. Remember to import GeoCoord
as well:
;
Then, use one of the provided functions in HaversineService
to calcule the distance between two points:
tryHaversine: void
As you have guessed:
getDistanceInMeters(coord1: GeoCoord, coord2: GeoCoord): number
calculates a distance in meters.getDistanceInKilometers(coord1: GeoCoord, coord2: GeoCoord): number
calculates a distance in kilometers.getDistanceInMiles(coord1: GeoCoord, coord2: GeoCoord): number
calculates a distance in miles.GeoCoord
is an interface withlatitude: number
andlongitude: number
.
Using SystemJS
To load ng2-haversine
using SystemJS is as simple as this. Open your systemjs.config.js
file and set paths
, map
and packages
properties this way:
{ System;}this;
That's it :)
License
Code released under the MIT license.