2013-04-23 53 views
0
// This method accepts a `Position` object, which contains 
// the current GPS coordinates 
function onSuccess(position) { 
    var element = document.getElementById('geolocation'); 
    element.innerHTML = 'Latitude: '    + position.coords.latitude         + '<br />' + 
          'Longitude: '   + position.coords.longitude    + '<br />' + 
          'Altitude: '   + position.coords.altitude    + '<br />' + 
          'Accuracy: '   + position.coords.accuracy    + '<br />' + 
          'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '<br />' + 
          'Heading: '   + position.coords.heading    + '<br />' + 
          'Speed: '    + position.coords.speed     + '<br />' + 
          'Timestamp: '   +           position.timestamp    + '<br />'; 
} 

它是從PhoneGap的自己的網站約地理位置採取的例子,但有一些問題,我不能悄悄地理解,我們使用範圍COORDS'成員一樣lantitude,lontitude位置變量對象,但是我們如何創造職位在js中的對象,即使沒有可變類型的東西。的PhoneGap GeoLocation中變種類型

回答

1

「位置」變種是通過PhoneGap的創建,並傳遞給回調成功功能,當你做一個:

navigator.geolocation.getCurrentPosition(onSuccess, onError); 

的的onSuccess功能:

function onSuccess(position) { 
    //Process the position here 
} 

從PhoneGap的API的完整的例子文檔:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Exemple Geolocation</title> 

    <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 

    // Attendre que PhoneGap soit prêt 
    // 
    document.addEventListener("deviceready", onDeviceReady, false); 

    // PhoneGap est prêt 
    // 
    function onDeviceReady() { 
     navigator.geolocation.getCurrentPosition(onSuccess, onError); 
    } 

    // Fonction de callback onSuccess, reçoit un objet Position 
    // 
    function onSuccess(position) { 
     var element = document.getElementById('geolocation'); 
     element.innerHTML = 'Latitude : '    + position.coords.latitude   + '<br/>' + 
          'Longitude : '    + position.coords.longitude   + '<br/>' + 
          'Altitude : '    + position.coords.altitude   + '<br/>' + 
          'Précision : '    + position.coords.accuracy   + '<br/>' + 
          'Précision de l'altitude : ' + position.coords.altitudeAccuracy + '<br/>' + 
          'Direction : '    + position.coords.heading   + '<br/>' + 
          'Vitesse : '     + position.coords.speed    + '<br/>'; 
    } 

    // Fonction de callback onError, reçoit un objet PositionError 
    // 
    function onError(error) { 
     alert('code : ' + error.code + '\n' + 
       'message : ' + error.message + '\n'); 
    } 

    </script> 
    </head> 
    <body> 
    <p id="geolocation">Recherche de géolocalisation...</p> 
    </body> 
</html> 

希望這有助於!再見!

+0

whoaa,速度非常快,謝謝幫助 – oknsnl 2013-04-23 15:24:24

+0

沒問題:)!不要忘記將這個答案標記爲解決問題。 – Antoine 2013-04-24 08:19:35

+0

你知道我該怎麼做? – oknsnl 2013-05-01 20:51:37