17/08/16 15:36
giocala88
Salve ragazzi ho un problema con la geolocalizzazione in phonegap. Posto la procedura (Ubuntu 16.x):
Creo un nuovo progetto
Aggiungo il progetto alla piattaforma android
Aggiungo al progetto il plugin geolocation
A questo punto apro il file index.html (cartella : proj/www) e lo modifico come segue:
Quindi modifico il file index.js (cartella proj/www/js) come segue:
A questo punto digito i comandi
e
Parte l'app nel mio tablet android collegato al pc ma non geolocalizza un bel niente. Come posso fare? Sto impazzendo da 3 giorni
Grazie
Creo un nuovo progetto
cordova create proj com.example.proj Proj
Aggiungo il progetto alla piattaforma android
cordova platform add android --save
Aggiungo al progetto il plugin geolocation
cordova plugin add cordova-plugin-geolocation
A questo punto apro il file index.html (cartella : proj/www) e lo modifico come segue:
<!DOCTYPE html> <html> <head> <!-- Customize this policy to fit your own app's needs. For more guidance, see: https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy Some notes: * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: * Enable inline JS: add 'unsafe-inline' to default-src --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *"> <meta name="format-detection" content="telephone=no"> <meta name="msapplication-tap-highlight" content="no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <link rel="stylesheet" type="text/css" href="css/index.css"> <title>Hello World</title> </head> <body> <div class="app"> <h1>Apache Cordova</h1> <div id='geolocation'>Geo...</div> <div id="deviceready" class="blink"> <p class="event listening">Connecting to Device</p> <p class="event received">Device is Ready</p> </div> </div> <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script> <script type='text/javascript'> app.initialize(); </script> </body> </html>
Quindi modifico il file index.js (cartella proj/www/js) come segue:
var app = { // Application Constructor initialize: function () { this.bindEvents(); this.contents={}; this.watchID=null; }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function () { document.addEventListener('deviceready', this.onDeviceReady, false); }, // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function () { app.receivedEvent('deviceready'); app.contents = document.getElementById('geolocation'); app.Geo(); }, onSuccess: function(position) { app.contents.innerHTML = 'Lat : '+position.coords.latitude+', Lon : '+position.coords.longitude; }, onError: function(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); }, Geo: function() { watchID = navigator.geolocation.watchPosition(app.onSuccess, app.onError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }); }, // Update DOM on a Received Event receivedEvent: function (id) { var parentElement = document.getElementById(id); var listeningElement = parentElement.querySelector('.listening'); var receivedElement = parentElement.querySelector('.received'); listeningElement.setAttribute('style', 'display:none;'); receivedElement.setAttribute('style', 'display:block;'); console.log('Received Event: ' + id); } };
A questo punto digito i comandi
cordova build
e
cordova run android
Parte l'app nel mio tablet android collegato al pc ma non geolocalizza un bel niente. Come posso fare? Sto impazzendo da 3 giorni
Grazie
aaa