因朋友需要,之前看了下關(guān)于h5的手機(jī)定位,目前寫了個(gè)demo在這里貼出來,感興趣的朋友可以看一下。
目前的版本只是demo,仍有幾個(gè)問題需要完善一下,問題如下:
1,如何將經(jīng)緯度等數(shù)據(jù)發(fā)送到被定位人看不到的頁面上。
2,如何繞過或或強(qiáng)制讓打開鏈接的人允許使用定位(彈窗)。
3,目前或取經(jīng)緯度后,要自行用谷歌地球去分析用戶位置(通過衛(wèi)星地圖定位并顯示),如何簡(jiǎn)化這一部分,讓被定位者的經(jīng)緯度自動(dòng)生成地圖圖片并一起導(dǎo)入到其他頁面(總之不能讓被定位者察覺自己被定位的這一事實(shí))。
!DOCTYPE HTML> html> head> meta charset="utf-"> meta name="viewport" content="width=device-width, initial-scale=, maximum-scale="> title>demo../title> script type="text/javascript" src="http://libs.useso.com/js/jquery/../jquery.min.js">/script> style type="text/css"> .demo{width:px; margin:px auto px auto} .geo{margin-top:px} .demo p{line-height:px; font-size:px} .demo p span,#baidu_geo,#google_geo{font-weight:bold} /style> /head> body> div id="main"> div class="demo"> p>地理坐標(biāo):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 />經(jīng)度:'+ position.coords.longitude); var latlon = position.coords.latitude+','+position.coords.longitude; //baidu var url = "http://api.map.baidu.com/geocoder/v/?ak=Cbdaebdbbbabcebcallback=renderReverselocation="+latlon+"output=jsonpois="; $.ajax({ type: "GET", dataType: "jsonp", url: url, beforeSend: function(){ $("#baidu_geo").html('正在定位...'); }, success: function (json) { if(json.status==){ $("#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==){ $("#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("定位失敗,用戶拒絕請(qǐng)求地理定位"); break; case error.POSITION_UNAVAILABLE: alert("定位失敗,位置信息是不可用"); break; case error.TIMEOUT: alert("定位失敗,請(qǐng)求獲取用戶位置超時(shí)"); break; case error.UNKNOWN_ERROR: alert("定位失敗,定位系統(tǒng)失效"); break; } } getLocation(); /script> /body> /html>
以上代碼就是實(shí)現(xiàn)h5和ajax手機(jī)定位代碼,后續(xù)還會(huì)持續(xù)更新完善,請(qǐng)大家持續(xù)關(guān)注腳本之家網(wǎng)站。
標(biāo)簽:股票 佛山 商洛 辛集 揭陽 荊州 咸寧 紅河
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于h5 ajax實(shí)現(xiàn)手機(jī)定位(demo)》,本文關(guān)鍵詞 基于,ajax,實(shí)現(xiàn),手機(jī),定位,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。