npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng2-haversine

v0.1.1

Published

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

Downloads

622

Readme

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.