ios10不能定位 window.navigator.geolocation.getCurrentPosition(定位第一节)
原文連接:
https://blog.csdn.net/michael_ouyang/article/details/54137709
---------------------------------------------------------?
問題分析:
目前由于許多蘋果用戶都升級(jí)到了iOS系統(tǒng),蘋果的iOS 10已經(jīng)正式對(duì)外推送,相信很多用戶已經(jīng)更新到了最新的系統(tǒng)。然而,如果web站沒有及時(shí)支持https協(xié)議的話,當(dāng)很多用戶在iOS 10下訪問很多網(wǎng)站時(shí),會(huì)發(fā)現(xiàn)都無(wú)法進(jìn)行正常精確定位,導(dǎo)致部分網(wǎng)站的周邊推薦服務(wù)無(wú)法正常使用。為何在iOS 10下無(wú)法獲取當(dāng)前位置信息?這是因?yàn)樵趇OS 10中,蘋果對(duì)webkit定位權(quán)限進(jìn)行了修改,所有定位請(qǐng)求的頁(yè)面必須是https協(xié)議的。如果是非https網(wǎng)頁(yè),在http協(xié)議下通過html5原生定位接口會(huì)返回錯(cuò)誤,也就是無(wú)法正常定位到用戶的具體位置,而已經(jīng)支持https的網(wǎng)站則不會(huì)受影響。
?
? ?目前提供的解決方案:
? 1、將網(wǎng)站的http設(shè)置為Https。
? 2、通過第三方解決,這也是我目前使用的方法。
?
?
? ?首先看以下兩段代碼:
代碼段一:
? ? ? ? ??1、在頁(yè)面引入js
?
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
?
?
? ? ? ? ?2、獲得定位方法??window.navigator.geolocation.getCurrentPosition:通過手機(jī)的webKit定位(目前ios系統(tǒng)對(duì)非https網(wǎng)站不提供支持)
?
navigator.geolocation.getCurrentPosition(translatePoint); //定位
function translatePoint(position) {
var currentLat = position.coords.latitude;
var currentLon = position.coords.longitude;
SetCookie("curLat", currentLat, 1);//設(shè)置cookie
SetCookie("curLng", currentLon, 1);//設(shè)置cookie
var gpsPoint = new BMap.Point(currentLon, currentLat);
var pt = new BMap.Point(currentLon, currentLat);
var geoc = new BMap.Geocoder();
geoc.getLocation(pt, function (rs) {
var addComp = rs.addressComponents;
SetCookie("curLat", currentLat, 1); //設(shè)置cookie
SetCookie("curLng", currentLon, 1); //設(shè)置cookie
//alert(JSON.stringify(addComp));
var city = addComp.city;
//獲得具體街道信息
var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;
$("#nowRoad").text(texts);
});
}
?
?
代碼段二:
? ? ? ? ??1、在頁(yè)面引入js
?
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
?
?
? ? ? ? ? ?2、獲得定位方法
?
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
var mk = new BMap.Marker(r.point);
currentLat = r.point.lat;
currentLon = r.point.lng;
SetCookie("curLat", currentLat, 1); //設(shè)置cookie
SetCookie("curLng", currentLon, 1); //設(shè)置cookie
var pt = new BMap.Point(currentLon, currentLat);
var geoc = new BMap.Geocoder();
geoc.getLocation(pt, function (rs) {
var addComp = rs.addressComponents;
SetCookie("curLat", currentLat, 1); //設(shè)置cookie
SetCookie("curLng", currentLon, 1); //設(shè)置cookie
var city = addComp.city;
var addComp = rs.addressComponents;
var texts = addComp.district + "-" + addComp.street + "-" + addComp.streetNumber;
//獲取地理位置成功,跳轉(zhuǎn)
});
}
});
?
以上兩端代碼中,最大的不同點(diǎn)在于:
第一段代碼使用的是
window.navigator.geolocation.getCurrentPosition()
此方法是通過UIwebview的內(nèi)核webKit進(jìn)行定位
?
第二段代碼使用的是
var geolocation = new BMap.Geolocation(); ?
geolocation.getCurrentPosition()
此方式是使用了百度api
附相關(guān)連接:http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html
?
?
另外,值得注意的一個(gè)問題是:
使用window.navigator.geolocation.getCurrentPosition()?獲取到的一個(gè)經(jīng)緯度,還不能直接使用,還需要經(jīng)過解密,才能獲取到準(zhǔn)備的地理位置
想知道如何把這個(gè)位置進(jìn)行解密轉(zhuǎn)換,請(qǐng)留意下一遍文章
http://blog.csdn.net/michael_ouyang/article/details/54378338
?
原文鏈接:http://blog.csdn.net/for12/article/details/52803787
總結(jié)
以上是生活随笔為你收集整理的ios10不能定位 window.navigator.geolocation.getCurrentPosition(定位第一节)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: count(1),count(*),co
- 下一篇: 这些Windows 10隐藏秘技,你知道