新聞中心
html5怎么實現(xiàn)調(diào)用gps獲取地理位置具體代碼
代碼如下:
創(chuàng)新互聯(lián)長期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為留壩企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè),留壩網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
/**
* 以下為html5代碼,獲取地理位置
*/
function getLocation() {
//檢查瀏覽器是否支持地理位置獲取
if (navigator.geolocation) {
//若支持地理位置獲取,成功調(diào)用showPosition(),失敗調(diào)用showError
// alert("正在努力獲取位置...");
var config = { enableHighAccuracy: true, timeout: 5000, maximumAge: 30000 };
navigator.geolocation.getCurrentPosition(showPosition, showError, config);
} else {
//alert("Geolocation is not supported by this browser.");
alert("定位失敗,用戶已禁用位置獲取權(quán)限");
}
}
/**
* 獲取地址位置成功
*/
function showPosition(position) {
//獲得經(jīng)度緯度
var x = position.coords.latitude;
var y = position.coords.longitude;
//配置Baidu Geocoding API
var url = "" +
"callback=renderReverse" +
"location=" + x + "," + y +
"output=json" +
"pois=0";
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
success: function (json) {
if (json == null || typeof (json) == "undefined") {
return;
}
if (json.status != "0") {
return;
}
setAddress(json.result.addressComponent);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("[x:" + x + ",y:" + y + "]地址位置獲取失敗,請手動選擇地址");
}
});
}
/**
* 獲取地址位置失敗[暫不處理]
*/
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("定位失敗,用戶拒絕請求地理定位");
//x.innerHTML = "User denied the request for Geolocation.[用戶拒絕請求地理定位]"
break;
case error.POSITION_UNAVAILABLE:
alert("定位失敗,位置信息是不可用");
//x.innerHTML = "Location information is unavailable.[位置信息是不可用]"
break;
case error.TIMEOUT:
alert("定位失敗,請求獲取用戶位置超時");
//x.innerHTML = "The request to get user location timed out.[請求獲取用戶位置超時]"
break;
case error.UNKNOWN_ERROR:
alert("定位失敗,定位系統(tǒng)失效");
//x.innerHTML = "An unknown error occurred.[未知錯誤]"
break;
}
}
/**
* 設(shè)置地址
*/
function setAddress(json) {
var position = document.getElementById("txtPosition");
//省
var province = json.province;
//市
var city = json.city;
//區(qū)
var district = json.district;
province = province.replace('市', '');
position.value = province + "," + city + "," + district;
position.style.color = 'black';
}
html5 如何調(diào)用安卓手機獲取gps坐標
HTML5中可以通過IP,WIFI信息,GPS,來實現(xiàn)地理定位,當然相關(guān)精度也是有所不同,所以如果要精確導(dǎo)航就得使用GPS信息。
下面是一段HTML5結(jié)合百度地圖API來獲取位置的代碼:
div?id="allmap"/div當前定位地址:a?id="du-gps"/aspan/span/div
script
var?map?=?new?BMap.Map("allmap");
var?geolocation?=?new?BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus()?==?BMAP_STATUS_SUCCESS){
map.panTo(r.point);
//alert('您的位置:'+r.point.lng+','+r.point.lat);
var?pt?=?r.point;
var?geoc?=?new?BMap.Geocoder();
geoc.getLocation(pt,?function(rs){
var?addComp?=?rs.addressComponents;
//alert(addComp.province?+?",?"?+?addComp.city?+?",?"?+?addComp.district?+?",?"?+?addComp.street?+?",?"?+?addComp.streetNumber);
$("#du-gps").text(addComp.district+addComp.street+addComp.streetNumber);
});
}
else?{
alert('failed'+this.getStatus());
}
},{enableHighAccuracy:?true})
/script
html5獲取地理位置,如果用戶拒絕了,之后每次加載都提示:拒絕獲取地理位置,如何獲取權(quán)限:如果用
1、獲取百度地理位置坐標,首先在搜索引擎中搜索。
2、搜索結(jié)果頁面如下,點擊第一個鏈接。
3、接著打開的界面如下,鼠標放到自己要獲取的地理位置。
4、比如選擇的是地壇公園,如下圖紅色的區(qū)域,點擊地壇公園。
5、上面就會有它的經(jīng)緯度,直接點擊復(fù)制就可以了。
HTML5的Geolocation接口可以獲取手機的gps定位嗎?
Geolocation接口不可以獲取手機的gps定位,這個api具有自己特有的定位方式。
定位用戶的位置
HTML5 Geolocation API 用于獲得用戶的地理位置。
鑒于該特性可能侵犯用戶的隱私,除非用戶同意,否則用戶位置信息是不可用的。
用法如下:
!DOCTYPE html
html
body
p id="demo"點擊這個按鈕,獲得您的位置:/p
button onclick="getLocation()"試一下/button
div id="mapholder"/div
script src=""/script
script
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
lat=position.coords.latitude;
lon=position.coords.longitude;
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('mapholder')
mapholder.style.height='250px';
mapholder.style.width='500px';
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
};
var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
/script
/body
/html
html5的地理定位功能getCurrentPosition還能用嗎?
能用,可以實現(xiàn)的,HTML5可以使用手機的GPS信息,利用百度等地圖的開放API就可以了。\x0d\x0aHTML5中可以通過IP,WIFI信息,GPS,來實現(xiàn)地理定位,當然相關(guān)精度也是有所不同,所以如果要精確導(dǎo)航就得使用GPS信息。\x0d\x0a下面是一段HTML5結(jié)合百度地圖API來獲取位置的代碼:\x0d\x0a\x0d\x0a當前定位地址:\x0d\x0a\x0d\x0avar map = new BMap.Map("allmap");\x0d\x0avar geolocation = new BMap.Geolocation();\x0d\x0ageolocation.getCurrentPosition(function(r){\x0d\x0aif(this.getStatus() == BMAP_STATUS_SUCCESS){\x0d\x0amap.panTo(r.point);\x0d\x0aalert('您的位置:'+r.point.lng+','+r.point.lat);\x0d\x0avar pt = r.point;\x0d\x0avar geoc = new BMap.Geocoder();\x0d\x0ageoc.getLocation(pt, function(rs){\x0d\x0avar addComp = rs.addressComponents;\x0d\x0aalert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);\x0d\x0a$("#du-gps").text(addComp.district+addComp.street+addComp.streetNumber);\x0d\x0a});\x0d\x0a}\x0d\x0aelse {\x0d\x0aalert('failed'+this.getStatus());\x0d\x0a}\x0d\x0a},{enableHighAccuracy: true})\x0d\x0a
回答于?2022-11-16
當前名稱:html5獲取gps,html5獲取GPS
URL鏈接:http://www.ef60e0e.cn/article/phciho.html