Why this code doesn't work on device but works with ionic serve?


#1
This is index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Geo Location</title>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' https://maps.googleapis.com/ https://maps.gstatic.com/ https://mts0.googleapis.com/ 'unsafe-inline' 'unsafe-eval'">

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>


  </head>
  <body ng-app='starter'>
    This is GeoLocation App

    <!-- your app's js -->
    <script src="js/app.js"></script>

    <script type="text/javascript">
    function showlocation() {
       navigator.geolocation.getCurrentPosition(callback);
    }

    function callback(position) {
       document.getElementById('latitude').innerHTML = position.coords.latitude;
       document.getElementById('longitude').innerHTML = position.coords.longitude;
    }
    </script>


    <input type="button" value="Show Location"
        onclick="javascript:showlocation()" />   <br/>

    Latitude: <span id="latitude"></span>       <br/>

    Longitude: <span id="longitude"></span>
  </body>
</html>

This in app.js:

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $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);


  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
  });
})

This is style.css:
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $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);


      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

#2

if you’re using android as the device u need to add location permission to android manifest.