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

ember-cli-geo

v4.0.0

Published

Geolocation service for Ember.js web apps

Downloads

100

Readme

ember-cli-geo

Code Climate Build Status

This addon is a go-to solution for integrating HTML5 Geolocation API into your Ember.js web app. It is production-ready and backwards compatible.

Installation

ember install ember-cli-geo

Usage

getLocation()

getLocation() gets user location from the browser and writes its coordinates to currentLocation property on the service. Accepts geoOptions as an argument. Returns an Ember.RSVP.Promise which is either resolved with geoObject containing all data about user location or is rejected with reason which explains why geolocation failed. It is used like this:

this.get('geolocation').getLocation().then(function(geoObject) {
  // do anything with geoObject here
  // you can also access currentLocation property and manipulate its data however you like
});

It corresponds to getCurrentPosition() in HTML5 Geolocation API. Learn more at getCurentPosition() on MDN. It emits an event geolocationSuccess with an object describing the geolocation when the position is available. If it fails, it emits an event geolocationFail with a reason.

trackLocation()

trackLocation() gets user location and setups a watcher which observes any changes occuring to user location. It then constantly updates currentLocation with the most recent location coordinates.

It accepts geoOptions as an argument. Returns an Ember.RSVP.Promise which is either resolved with geoObject containing all data about user location or is rejected with reason which explains why geolocation failed. It accepts an optional callback function, that is called whenever the position is updated. It emits an event geolocationSuccess with an object describing the geolocation whenever a new position is available. If it fails, it emits an event geolocationFail with a reason.

It is used like this:

this.get('geolocation').trackLocation().then(function(geoObject) {
  // do anything with geoObject here
  // currentLocation is constantly updated if user location is changed
});
// or
this.get('geolocation').trackLocation(null, (geoObject) => { /* will be called with new positiond */ })
// or
const service = this.get('geolocation');
service.on('geolocationSuccess', (geoObject) => { { /* will be called with new position */);

It corresponds to watchPosition() in HTML5 Geolocation API. Learn more at watchPosition() on MDN.

stopTracking

stopTracking() stops the app from continuously updating the user location.

It accepts an optional boolean parameter that clears currentLocation if it's true.

It is used like this:

this.get('geolocation').stopTracking(true);

It corresponds to watchPosition() in HTML5 Geolocation API. Learn more at clearWatch() on MDN.

currentLocation

currentLocation is a property of geolocation service which stores the array of user location coordinates in the format of [lat, lon]. It is used like this:

this.get('geolocation').get('currentLocation');

geoObject

geoObject is an object which contains all data about user location. Both getLocation() and trackLocation() promises are resolved with it. It looks like this:

{
  coords: {
    accuracy: 100,
    altitude: 0,
    altitudeAccuracy: 0,
    heading: NaN,
    latitude: 37.789,
    longitude: -122.412,
    speed: NaN
  },
  timestamp: 1435861233751
}

It corresponds to Position object in HTML5 Geolocation API. Learn more at Position object on MDN.

reason

reason is an error object which contains data about why geolocation has failed. Both getLocation() and trackLocation() promises are rejected with it. It corresponds to PositionError object in HTML5 Geolocation API. Learn more at PositionError object on MDN.

geoOptions

geoOptions is an optional object that can be passed to both getLocation() and trackLocation() to customize geolocation query. If you didn't pass it to functions, then next defaults will be automatically passed:

{
  enableHighAccuracy: false,
  timeout: Infinity,
  maximumAge: 0
}

It corresponds to PositionOptions object in HMTL5 Geolocation API. Learn more at PositionOptions object on MDN.

Usage Examples

Setup geolocation service

In order to use geolocation inside of your Ember.Route you should directly inject it to the one:

export default Ember.Route.extend({
  geolocation: Ember.inject.service()
});

Get user location and display it in your template

You need to implement a custom action which will call the geolocation service. In your route:

// app/routes/geolocator.js

export default Ember.Route.extend({

  actions: {
    getUserLocation: function() {
      this.get('geolocation').getLocation().then(function(geoObject) {
        var currentLocation = this.get('geolocation').get('currentLocation');
        this.controllerFor('geolocator').set('userLocation', currentLocation);
      });
    }
  }
});

In your controller:

// app/controllers/geolocator.js

export default Ember.Controller.extend({
  userLocation: null
});

In your template:

// app/templates/geolocator.hbs

<button type="button" {{action 'getUserLocation'}}>Geolocate me!</button>
{{#if userLocation}}
  {{userLocation}}
{{/if}}

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.