在html页面中显示用户地理位置

时间:2014-09-01 08:14:38

标签: javascript html5 cordova geolocation

我是html5的新手并使用cordova,其代码如下。一旦我使用html5地理位置对象,我需要在用户当前屏幕中显示该值。代码如下

function onDeviceReady() {
            // Get the most accurate position updates available on the
            // device.
            var options = { enableHighAccuracy: true };
            watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
        }
        // onSuccess Geolocation       
        function onSuccess(position) {
            var element = document.getElementById('geolocation');
            element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
                            'Longitude: ' + position.coords.longitude + '<br />' +
                            '<hr />' + element.innerHTML;



}

                            document.write=(element);

<body class="sapUiBody" role="application">
    <p id="geolocation"need to display user geo location here </p>



</body>

1 个答案:

答案 0 :(得分:1)

HTML代码:

<p id="geolocation">your coordinates:</p>

JavaScript代码:

var x = document.getElementById("geolocation");
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
}

function showPosition(position) {
    x.innerHTML+="<br>Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
}