Skip to content

Instantly share code, notes, and snippets.

@gauravmehla
Last active March 8, 2017 11:14
Show Gist options
  • Save gauravmehla/db556a603e8fc6d797f2150ba0e69ad0 to your computer and use it in GitHub Desktop.
Save gauravmehla/db556a603e8fc6d797f2150ba0e69ad0 to your computer and use it in GitHub Desktop.
angular.module('pit', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaGeolocation) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function(position) {
console.log(position);
};
// onError Callback receives a PositionError object
//
function onError(error) {
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
console.log(coords);
});
console.log(error);
}
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
// will execute when device is ready, or immediately if the device is already ready.
$cordovaGeolocation.getCurrentPosition(onSuccess, onError, options);
});
})
@gururajmbhandarkar
Copy link

The code should be inside $ionicPlatform.ready because this will execute first and also device ready function should be called for the geolocation to work

@gururajmbhandarkar
Copy link

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    // onSuccess Callback
    // This method accepts a Position object, which contains the
    // current GPS coordinates
    //
    var onSuccess = function(position) {
        console.log(position);
    };

    // onError Callback receives a PositionError object
    //
    function onError(error) {
        $.getJSON('https://ipinfo.io/geo', function(response) { 
        var loc = response.loc.split(',');
        var coords = {
            latitude: loc[0],
            longitude: loc[1]
        };
        console.log(coords);
        });  

        console.log(error);
    }

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };

    // will execute when device is ready, or immediately if the device is already ready.
    $cordovaGeolocation.getCurrentPosition(onSuccess, onError, options);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment