2016-08-05 29 views
1

Wie füge ich die Schaltfläche richtig hinzu, so dass das GPS oder die Geolocation erst nach einem Klick auf die Schaltfläche aktiviert wird?Wie wird der GPS-Standort nur angezeigt, nachdem Sie auf die Schaltfläche geklickt haben?

ist hier mein Code:

<button id="Btn1">Get Current Position </button> 

und fügen Sie diese in Ihre jQuery:

</p><button id="Btn1" onclick="getCurrentPosition();">Get Current Position </button></p> 
    <script> 
    document.addEventListener("deviceready", onDeviceReady, false); 
    function onDeviceReady() { 
     console.log("navigator.geolocation works well"); 
    } 

    // onSuccess Callback 
    // This method accepts a Position object, which contains the 
    // current GPS coordinates 
    // 
    function onSuccess (position) { 
     alert('Latitude: '   + position.coords.latitude   + '\n' + 
       'Longitude: '   + position.coords.longitude   + '\n' + 
       'Altitude: '   + position.coords.altitude   + '\n' + 
       'Accuracy: '   + position.coords.accuracy   + '\n' + 
       'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' + 
       'Heading: '   + position.coords.heading   + '\n' + 
       'Speed: '    + position.coords.speed    + '\n' + 
       'Timestamp: '   + position.timestamp    + '\n'); 
    }; 

    function onError(error) { 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
    } 

    navigator.geolocation.getCurrentPosition(onSuccess, onError); 

    </script> 

</body> 

</html> 

Antwort