如何在phonegap中显示纬度,经度,标题

时间:2012-01-17 05:12:43

标签: jquery android cordova

我想显示纬度,经度,标题在相同的应用程序中使用phonegap.i尝试了phonegap相同的程序。如果我得到纬度,longtude,我不能得到标题是null.is有什么方法?

<!DOCTYPE html>
<html>
  <head>
    <title>Compass Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
    <link rel="stylesheet" href="css/compass.css"/>

    <script type="text/javascript" charset="utf-8">
       // The watch id references the current `watchHeading`
    var watchID = null;


    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        var options = { frequency: 500 };
        watchID = navigator.compass.watchHeading(onSuccess, onError, options);
        //watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);


    }

/*
    // Start watching the compass
    //
    function startWatch() {
         // Update compass every 3 seconds
        var options = { frequency: 500 };

        watchID = navigator.compass.watchHeading(onSuccess, onError, options);
    }

    // Stop watching the compass
    //
    function stopWatch() {
        if (watchID) {
            navigator.compass.clearWatch(watchID);
            watchID = null;
        }
    }
*/
    // onSuccess: Get the current heading
    //
    function onSuccess(position) {
        var element = document.getElementById('ang');
        var deg_heading = document.getElementById('heading');
        //var lat = position.coords.latitude; 
        //var lng = position.coords.longitude;
        var deg = position.magneticHeading;
        var actual_deg = 360;
        //var deg = '180';
        //alert ("lat/lng: "+lat+","+lng);
        deg_heading.innerHTML = 'Heading: ' + (actual_deg - deg)  + '&#176';// + " lat:"+lat+" long:"+lng;

        element.style.webkitTransform = "rotate(-"+ deg +"deg)";


    }

    // onError: Failed to get the heading
    //
    function onError(compassError) {
        alert('Compass error: ' + compassError.code);
    }

    </script>
  </head>
   <body>
   <!-- <button onclick="startWatch();">Start Watching</button>
    <button onclick="stopWatch();">Stop Watching</button> -->
    <div id="compass" class="comapss">
        <div class="compass_direction"><img id="ang" src="img/direction.png"/></div>
    </div>
    <div id="heading"></div>    
  </body> 
</html>

2 个答案:

答案 0 :(得分:0)

无论出于何种原因,我必须在某些项目中监视navigator.compass进行初始化。偶尔它会在deviceReady触发之后才会存在。我通常使用这样的东西:

var intervalId = setInterval(function() {
   if( navigator.compass ) {
      clearInterval(intervalId);
      intervalId = navigator.compass.watchHeading(onSuccess, onError, options);
   }
}, 250);

答案 1 :(得分:0)

function locationFinder() {
    if (navigator.geolocation) {
        function success(pos) {
            current_lng = pos.coords.longitude;
            current_lat = pos.coords.latitude;
            console.log("longitude get" + current_lng);
            console.log("latitude get" + current_lat);
        }
        function fail(error) {
            console.log("error 2");
        }
        // Find the users current position. Cache the location for 5 minutes,
        // timeout after 6 seconds
        navigator.geolocation.getCurrentPosition(success, fail, {
            maximumAge : 500000,
            enableHighAccuracy : true,
            timeout : 6000
        });
    } else {
        window.plugins.toast.showLongBottom("Please check GPS Setting",
                function(a) {
                    console.log('toast success: ' + a)
                }, function(b) {
                    console.log('toast error: ' + b)
                });
    }
}