GithubHelp home page GithubHelp logo

mkly / nggeolocation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ninjatronic/nggeolocation

0.0 2.0 0.0 52 KB

AngularJS support for HTML5 Geolocation

License: MIT License

JavaScript 96.01% HTML 3.99%

nggeolocation's Introduction

ngGeolocation

AngularJS support for HTML5 Geolocation API

Build Status

ngGeolocation provides AngularJS support for the HTML5 Geolocation API. It aims to provide a quick and easy way to consume geolocation information made available by modern browsers and HTML5 in AngularJS apps wtihout having to write custom SDK wrapping code. Everything exposed by the $geolocation service lives within the digest cycle, so there is no need to use $scope.$apply() in your controllers when consuming the service. ngGeolocation goes one step further than wrapping the HTML5 API calls and provides a property that can be $watched by your controllers which will always contain the latest position data available through the browser.

API Reference

Installation

Bower

The bower package name is ngGeolocation.

via bower.json

{
    "dependencies": {
        "ngGeolocation": ">=0.0.2"
    }
}

via CLI

bower install ngGeolocation

Include the installed scripts in your html...

<script src="bower_components/ngGeolocation.min.js"/>

Manual

Download the minified or unminified source.

Include the scripts in your html...

<script src="lib/ngGeolocation.min.js"/>

Usage

Make sure your app depends on the ngGeolocation module. Geolocation methods are available through the $geolocation service.

angular
    .module('myApp', ['ngGeolocation'])
    .controller('geolocCtrl', ['$geolocation', '$scope', function($geolocation, $scope) {
         $geolocation.getCurrentPosition({
            timeout: 60000
         }).then(function(position) {
            $scope.myPosition = position;
         });
    }]);

The $geolocation service can expose a property location whose value reflects the current position. To enable this feature a watch must be created using watchPosition. This method takes a PositionOptions object in the same manner as getCurrentPosition. There is no return value.

While this watch is active the value of the property location is periodically updated with the latest geolocation result. If an error has occurred the code and message are available via $geolocation.position.error.

The current watch can be cancelled using clearWatch.

angular
    .module('myApp', ['ngGeolocation'])
    .controller('geolocCtrl', ['$geolocation', '$scope', function($geolocation, $scope) {
        $geolocation.watchPosition({
            timeout: 60000,
            maximumAge: 250,
            enableHighAccuracy: true
        });
        $scope.myPosition = $geolocation.position; // this object updates regularly, it has 'error' property which is a 'truthy' and also 'code' and 'message' property if an error occurs
        
        //It has all the location data 
        '$scope.myPosition.coords'
        
        //It's truthy and gets defined when error occurs 
        '$scope.myPosition.error'
    }]);

Usage with angular-google-maps

Here's an example from @markmcdonald51 for using ngGeolocation with angular-google-maps...

angular
.module('MyApp', ['ngGeolocation', 'google-maps'])
.controller('geolocCtrl', ['$geolocation', '$scope', function($geolocation, $scope) {

  $geolocation.watchPosition({
    timeout: 60000,
    maximumAge: 250,
    enableHighAccuracy: true
  })

  $scope.$watch('myPosition.coords', function (newValue, oldValue) {
    $scope.map = {
      center: {
        latitude: newValue.latitude,
        longitude: newValue.longitude
      },
      zoom: 16
    };                      
  }, true);

});

Development

This project uses Grunt for tooling. The toolbelt dependencies are managed by NPM and the production code dependencies are managed by Bower. Fork the code and clone it into your workspace, then...

npm install
bower install --dev

Tests can be run manually with grunt test or automatically on changes with grunt.

NB The minified file is generated by grunt build - all console logging is stripped out of the minified file using grunt-strip.

Contributions

If you wish to contribute, please raise an issue or submit a pull request which includes:

  • A test or tests that demonstrate the issue found or feature added
  • A rebuilt minified file (ngGeolocation.min.js)

nggeolocation's People

Contributors

ninjatronic avatar dalager avatar abhikhatri avatar faceleg avatar acramatte avatar

Watchers

James Cloos avatar Mike Lay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.