基于h5 ajax實(shí)現(xiàn)手機(jī)定位(demo)
因朋友需要,之前看了下關(guān)于h5的手機(jī)定位,目前寫了個(gè)demo在這里貼出來(lái),感興趣的朋友可以看一下。
目前的版本只是demo,仍有幾個(gè)問(wèn)題需要完善一下,問(wèn)題如下:
1,如何將經(jīng)緯度等數(shù)據(jù)發(fā)送到被定位人看不到的頁(yè)面上。
2,如何繞過(guò)或或強(qiáng)制讓打開鏈接的人允許使用定位(彈窗)。
3,目前或取經(jīng)緯度后,要自行用谷歌地球去分析用戶位置(通過(guò)衛(wèi)星地圖定位并顯示),如何簡(jiǎn)化這一部分,讓被定位者的經(jīng)緯度自動(dòng)生成地圖圖片并一起導(dǎo)入到其他頁(yè)面(總之不能讓被定位者察覺自己被定位的這一事實(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=Cbdaebdbbbabceb&callback=renderReverse&location="+latlon+"&output=json&pois=";
$.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)站。
相關(guān)文章
使用Ajax實(shí)現(xiàn)簡(jiǎn)單的帶百分比進(jìn)度條實(shí)例
最近做項(xiàng)目遇到這樣的需求要求當(dāng)進(jìn)行文件長(zhǎng)傳保存等操作時(shí),能在頁(yè)面顯示一個(gè)帶百分比的進(jìn)度條,給用戶一個(gè)好的交互體驗(yàn),下面通過(guò)實(shí)例代碼給大家介紹基于ajax實(shí)現(xiàn)帶百分比進(jìn)度條效果,需要的的朋友參考下吧2017-07-07
Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)實(shí)例代碼
這篇文章主要介紹了Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)
這篇文章主要介紹了不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法,結(jié)合具體實(shí)例形式分析了三種不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax功能的相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-07-07
Ajax實(shí)現(xiàn)跨域訪問(wèn)的三種方法
本文給大家介紹了ajax實(shí)現(xiàn)跨域訪問(wèn)的3種解決方案,非常的實(shí)用,個(gè)人比較推薦第三種,小伙伴們可以著重看下。2015-06-06
談?wù)凙jax原理實(shí)現(xiàn)過(guò)程
Asynchronous javascript and xml(ajax),實(shí)現(xiàn)了客戶端與服務(wù)器進(jìn)行數(shù)據(jù)交流過(guò)程。使用技術(shù)的好處是:不用頁(yè)面刷新,并且在等待頁(yè)面?zhèn)鬏敂?shù)據(jù)的同時(shí)可以進(jìn)行其他操作2015-11-11

