Title: Camera i geolocalització
Course: CFGS - Desenvolupament d'aplicacions web > Section: Desenvolupament d'aplicacions mòbils

Camera

Body HTML

   <body>
      <script type="text/javascript" src="cordova.js"></script>
      <script type="text/javascript" src="js/index.js"></script>

      <h1>La meva primera APP</h1>
      <img src="" id="myImage">
      <button onclick="ferFoto()">Camera</button>

   </body>

Codi JavaScript

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

    console.log(device.cordova);

}

function ferFoto () {

   navigator.camera.getPicture(onSuccess, onFail, { quality: 50,destinationType: Camera.DestinationType.FILE_URI });


}

function onSuccess(imageURI) {
    var image = document.getElementById('myImage');
   image.src=imageURI;
}

function onFail(message) {
    alert('Failed because: ' + message);
}

Geolocation

HTML

   <body>
      <script type="text/javascript" src="cordova.js"></script>
      <script type="text/javascript" src="js/index.js"></script>

      <h1>Geoloc APP</h1>
      <div>Dades</div>
      <div id="geolocation"></div>

   </body>

JAVASCRIPT

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

   console.log(device.cordova);
   var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 1000, enableHighAccuracy: true });
}


// onSuccess Callback
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
//
function onSuccess(position) {
   console.log("Success position");
   var element = document.getElementById('geolocation');
   element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
      'Longitude: ' + position.coords.longitude     + '<br />' +
      '<hr />'      + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
   console.log("Error position");
   alert('code: '    + error.code    + '\n' +
         'message: ' + error.message + '\n');
}