基於h5+ajax實現的手機定位


因朋友需要,之前看了下關於h5的手機定位,目前寫了個demo在這里貼出來,感興趣的朋友可以看一下。

目前的版本只是demo,仍有幾個問題需要完善一下,問題如下:

1,如何將經緯度等數據發送到被定位人看不到的頁面上。

2,如何繞過或或強制讓打開鏈接的人允許使用定位(彈窗)。

3,目前或取經緯度后,要自行用谷歌地球去分析用戶位置(通過衛星地圖定位並顯示),如何簡化這一部分,讓被定位者的經緯度自動生成地圖圖片並一起導入到其他頁面(總之不能讓被定位者察覺自己被定位的這一事實)。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>demo..</title>
<script type="text/javascript" src="http://libs.useso.com/js/jquery/1.7.2/jquery.min.js"></script>
<style type="text/css">
.demo{width:560px; margin:60px auto 10px auto}
.geo{margin-top:20px}
.demo p{line-height:32px; font-size:16px}
.demo p span,#baidu_geo,#google_geo{font-weight:bold}
</style>
</head>
<body>
<div id="main">
    <div class="demo">
        <p>地理坐標:<span id="latlon"></span></p>
        <div class="geo">
            <p>百度地圖定位位置:</p>
            <p id="baidu_geo"></p>
        </div>
        <div class="geo">
            <p>GOOGLE地圖定位位置:</p>
            <p id="google_geo"></p>
        </div>
    </div>
</div>
<script>
function getLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }else{
        alert("瀏覽器不支持地理定位。");
    }
}

function showPosition(position){
    $("#latlon").html("<br />緯度:"+position.coords.latitude +'<br />經度:'+ position.coords.longitude);
    var latlon = position.coords.latitude+','+position.coords.longitude;
    
    //baidu
    var url = "http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b&callback=renderReverse&location="+latlon+"&output=json&pois=0";
    $.ajax({ 
        type: "GET", 
        dataType: "jsonp", 
        url: url,
        beforeSend: function(){
            $("#baidu_geo").html('正在定位...');
        },
        success: function (json) { 
            if(json.status==0){
                $("#baidu_geo").html(json.result.formatted_address);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            $("#baidu_geo").html(latlon+"地址位置獲取失敗"); 
        }
    });
    
    //google
    var url = 'http://maps.google.cn/maps/api/geocode/json?latlng='+latlon+'&language=CN';
    $.ajax({ 
        type: "GET",
        url: url, 
        beforeSend: function(){
            $("#google_geo").html('正在定位...');
        },
        success: function (json) { 
            if(json.status=='OK'){
                var results = json.results;
                $.each(results,function(index,array){
                    if(index==0){
                    $("#google_geo").html(array['formatted_address']);
                    }
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            $("#google_geo").html(latlon+"地址位置獲取失敗"); 
        } 
    });
}

function showError(error){
    switch(error.code) {
        case error.PERMISSION_DENIED:
            alert("定位失敗,用戶拒絕請求地理定位");
            break;
        case error.POSITION_UNAVAILABLE:
            alert("定位失敗,位置信息是不可用");
            break;
        case error.TIMEOUT:
            alert("定位失敗,請求獲取用戶位置超時");
            break;
        case error.UNKNOWN_ERROR:
            alert("定位失敗,定位系統失效");
            break;
    }
}

getLocation();
</script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM