HTML5地理定位實(shí)例

字號(hào):


    html5 獲取坐標(biāo)代碼如下:
    代碼如下:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>test1.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    </head>
    <body>
    <div id="demo">點(diǎn)擊這個(gè)按鈕,獲得您的坐標(biāo):</div>
    <button onclick="getLocation()">試一下</button>
    <script type="text/javascript">
    var x=document.getElementById("demo");
    function getLocation(){
    if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(showPosition);
    }else{
    x.innerHTML="瀏覽器不支持!??!";
    }
    }
    function showPosition(position){
    x.innerHTML="Latitude: "+position.coords.latitude+"<br/>Longitude: "+position.coords.longitude;
    }
    </script>
    </body>
    </html>
    經(jīng)測(cè)試,在IE9 、firefox、chrome、opera上都可以成功獲取到坐標(biāo)位置,但是safari 5.x上卻不能返回坐標(biāo),暫時(shí)木有找到原因。成功的案例里頭,chrome響應(yīng)的速度最快,其次是opera,然后是IE9,firefox居然是最慢的。個(gè)人表示對(duì)firefox很失望,不過chrome倒是越來棒了。