Oppure

Loading
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
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 8-|

Grazie
aaa
17/08/16 21:46
pierotofy
Questo:

alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');


Viene invocato?

Ci sono errori nella console di debug di Android Studio / Eclipse? E nell'output della webview? (Vedi: developers.google.com/web/tools/chrome-devtools/debug/remote-debugging/…)

Se aumenti il timeout che succede? E se imposti enableHighAccuracy a false?

Il mio blog: piero.dev
17/08/16 22:42
giocala88
Viene invocato l'errore Timeout, alchè se aumento il timeout a 10min e/o imposto enableHighAccuracy a false non succede nulla.
aaa
18/08/16 14:21
pierotofy
Controlla che ci sia <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> nel tuo manifest.

E prova a riavviare il dispositivo (seriamente, prova).
Il mio blog: piero.dev
20/08/16 9:49
giocala88
Era un problema di poermessi. Ora però sorge il problema che in Android la geolocalizzazione è lenta, parliamo di 3-4 minuti.
Soluzioni a riguardo?
aaa
20/08/16 17:19
pierotofy
Dove stai testando il dispositivo? Ricordati che all'interno (a casa, in ufficio) il segnale GPS spesso non è accessibile. Se sei connesso a WiFi, e il tuo dispositivo ha abilitato i dovuti permessi, allora il WiFi viene usato per aiutare ad identificare la posizione.

Vai all'aperto e prova di nuovo. Quanto tempo ci mette?
Il mio blog: piero.dev