Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ng2-haversine

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng2-haversine

An Angular 2 library to calculate the distance between a pair of coordinates using the Haversine formula.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
106
decreased by-28.86%
Maintainers
1
Weekly downloads
 
Created
Source

ng2-haversine

NPM

npm version Dependency Status

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:

import { HaversineService } from "ng2-haversine";

@NgModule({
    ...
    providers: [HaversineService]
    ...
})
export class 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:

import { HaversineService, GeoCoord } from "ng2-haversine";

@Component({ ... })
export class MyComponent {
    constructor(private _haversineService: HaversineService) { }
}

Then, use one of the provided functions in HaversineService to calcule the distance between two points:

tryHaversine(): void {

    let madrid: GeoCoord = {
        latitude: 40.416775,
        longitude: -3.703790
    };

    let bilbao: GeoCoord = {
        latitude: 43.262985,
        longitude: -2.935013
    };

    let meters = this._haversineService.getDistanceInMeters(madrid, bilbao);
    let kilometers = this._haversineService.getDistanceInKilometers(madrid, bilbao);
    let miles = this._haversineService.getDistanceInMiles(madrid, bilbao);

    console.log(`
        The distance between Madrid and Bilbao is:
            - ${meters} meters
            - ${kilometers} kilometers
            - ${miles} miles
    `);
}

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 with latitude: number and longitude: 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:

(function (global) {
    System.config({
        paths: {
            "npm:": "node_modules/"
        },
        map: {
            ...
            "ng2-haversine": "npm:ng2-haversine"
        },
        packages: {
            ...
            "ng2-haversine": {
                main: "./index.js",
                defaultExtension: "js"
            }
        }
    });
})(this);

That's it :)

License

Code released under the MIT license.

Keywords

FAQs

Package last updated on 09 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc