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

nemex-angular2-realtimegeolocation

v0.0.6

Published

Angular 2+ Realtime Geolocation Service

Downloads

5

Readme

Nemex Angular 2+ Realtime Geolocation Service

This service allows you to obtain realtime GPS locations within Angular 2+.

Note: Chrome and Safari requires the device connection to be over SSL (https://), make sure to debug and use SSL on your producation for this to work.

Installation

Install the package using the following command:

npm install nemex-angular2-realtimegeolocation

In your app module add the follwing code:

...
import { RealtimeGeolocationService } from 'nemex-angular2-realtimegeolocation';

@NgModule({
  ...
  // Import the service in order to use it within your app
  providers: [
    ...
    RealtimeGeolocationService
  ],
  ...
})

Usage

In order to obtain the device locations you must import the RealtimeGeolocation service in your app and into your component.

After that you need to use the start() method to obtain new locations. If you would like to stop, simply call the stop() method.

Here is an example of a map component which shows the current coordinates of the user (updated by intervals of 300 milliseconds):

import { Component, OnInit, Input } from '@angular/core';
import { RealtimeGeolocationService } from 'nemex-angular2-realtimegeolocation';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
  currentLocation:Coordinates = null;

  constructor(private locationService:RealtimeGeolocationService) { }

  ngOnInit() {
    try {  
        // Start obtaining realtime location when map component loads
        this.locationService.refreshInterval = 300;
        this.locationService.start(); 

        // Update the location on the map according to the current position of the user
        this.locationService.getLocationObservable().subscribe(
          position => {
            this.currentLocation = position.coords;
          }, 
          error => {
            // Usually called because of permission issues or an error obtaining the last position
            alert(error.message);
        });
    } catch(error) {
      // This error is usually called when device does not support geolocation at all
      alert(error);
    }
  }
}

Service options

The realtime geolocation service allows you to edit some of it's capabilities easily.

  • refreshInterval - The time (in milliseconds) between each user location updates. by default 1000.
  • locationOptions - The options delivered to the window.navigator.geolocation.getCurrentPosition. By default set to:
{ enableHighAccuracy: true }; 
  • listening - Returns true if location is still being listened to.

Instead of using a subscriber to get the location updates, you can register a callback to obtain the new positions:

this.locationService.onLocationObtained.subscribe(position => {
    // We have got a new position!
});

this.locationService.onLocationError.subscribe(error => {
    // An error had occured while trying to get the last position
});

You can also obtain the current position instead of listening to realtime locations using the following method:

// Returns an observable which returns the current position
getCurrentPosition().subscribe(position => {
  // We have obtained a position!
  console.log(position);
},
error => {
  // An error had occured
  console.error(error);
});