昨天接到一个在h5获取经纬度的需求,看了文档后,代码其实很简单,但在浏览器上调试就比较蛋疼了...

代码:

function successfulCallback(position) {
console.log('-----------successfulCallback--------',position)
alert("获取地理位置成功!");
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log(longitude, latitude);
}
function failCallback(error) {
var text;
switch (error.code) {
case error.PERMISSION_DENIED:
text = "用户拒绝对获取地理位置的请求。";
break;
case error.POSITION_UNAVAILABLE:
text = "位置信息是不可用的。";
break;
case error.TIMEOUT:
text = "请求用户地理位置超时。";
break;
case error.UNKNOWN_ERROR:
text = "未知错误。";
break;
}
alert(text);
}
// 获取当前经纬度
function getPosition() {
console.log('-----------getPosition--------')
//判断浏览器是否支持HTML5 定位
if ("geolocation" in navigator) {
console.log('-----------support navigator--------')
navigator.geolocation.getCurrentPosition(
successfulCallback,
failCallback
);
} else {
alert("你的浏览器不能使用HTML5定位");
}
}

关于geolaction的介绍,我想这个文档会比我讲得更详细:

https://developer.mozilla.org/zh-CN/docs/Web/API/Geolocation/Using_geolocation

获取经纬度方式有两种:

1.navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options) 【适用于对经度要求不高的,可获得低精度,后两个参数可选】

2.navigator.geolocation.watchPosition(successCallback, errorCallback, options) 【适用于对经度有要求的,可获得高精度,后两个参数可选】

  ps:watchPosition()会返回一个 ID,唯一地标记该位置监视器。可将该ID 传给clearWatch(ID)来停止监视用户位置。

本地无服务调试:

1.是否获取地理位置的提示框有时不出来.... 很尴尬;

2.出来了,点击允许就没然后了.... 也很尴尬

3.点击允许后,获得了想要的经纬度 ... 大概要看浏览器心情

公司测试环境看效果:

是否同意获取地理位置的弹框始终不出来.....  我一度以为是代码问题or环境问题【公司项目比较老】

后发现这个提示:

搜了下CanIus里各浏览器支持情况:

查看其CanIUse里的notice,会发现,geolocation越往后基本就只支持https请求的,点击测试环境里浏览器提示里的链接,里面提供了非安全环境(即http环境)测试方法:

https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins

粘贴部分原文如下:

Testing Powerful Features

If you are a developer that needs to keep testing a site using a Powerful Feature but you only have a non-secure test server, you have several options:
    1. Secure the server with a publicly-trusted certificate. If the server is reachable from the Internet, several public CAs offer free, automatically-renewed server certificates.
    2. http://localhost is treated as a secure origin, so if you're able to run your server from localhost, you should be able to test the feature on that server.
    3. You can run chrome with the --unsafely-treat-insecure-origin-as-secure="http://example.com" flag (replacing "example.com" with the origin you actually want to test), which will treat that origin as secure for this session. Note that on Android and ChromeOS this requires having a device with root access/dev mode. (This flag is broken in Chrome 63 but fixed in Chrome 64 and later. Prior to Chrome 62, you must also include the --user-data-dir=/test/only/profile/dir to create a fresh testing profile for the flag to work.)
    4. Create a self-signed certificate for temporary testing. Direct use of such a certificate requires clicking through an invalid certificate interstitial, which is otherwise not recommended. Note that because of this interstitial click-through (which also prevents HTTPS-response caching), we recommend options (1) and (2) instead, but they are difficult to do on mobile. See this post on setting up a self-signed certificate for a server for more information on how to do this.

      An alternative approach is to generate a self-signed root certificate which you place into the trust store of the developer PC/devices, and then issue one or more certificates for the test servers. Trusting the root certificate means that Chrome will treat the site as secure and load it without interstitials or impacting caching. One easy way of setting up and using a custom root certificate is to use the open source mkcert tool.

We continue to invest in improved methods for testing powerful features on insecure origins, and we'll update this page once we've developed them. Feel free to contribute ideas to security-dev@chromium.org.
 
因为开发在测试环境调试,故采用了第三个方式:
1.在chrome输入:chrome://flags/#unsafely-treat-insecure-origin-as-secure
2.直接找 Insecure origins treated as secure 或者全局搜索
3.把要设置的域名输入对应框内,如:http://test.com ,设置起为enable,然后Relaunch浏览器即可
ps: 最后获取地理位置提示弹窗会出来,但并不是每次都出来,且有时回调响应很慢,要等... 确实蛮尴尬
 

h5定位geolaction无法调试解决方法的更多相关文章

  1. Segger RTT : Real Time Terminal SRAM 调试解决方法

    http://segger.com/jlink-real-time-terminal.html Real Time Terminal SEGGER's Real Time Terminal (RTT) ...

  2. ADF控件ID变化引发JS无法定位控件的解决方法

    原文地址:ADF控件ID变化引发JS无法定位控件的解决方法作者:Nicholas JSFF定义的控件ID到了客户端时往往会改变.例如在JSFF中的一个的ID为"ot1",但是当这个 ...

  3. Android 10不能使用uiautomatorviewer定位元素的终极解决方法

    Android app 元素定位除了使用Appium Inspector 外,还可以使用Android SDK 里tools中的uiautomatorviewer 工具.但今天打算使用 uiautom ...

  4. 关于百度地图js api的getCurrentPosition定位不准确的解决方法

    很久之前帮大叔解决了一个gps坐标转换为百度地图坐标的问题.今天大叔又给我讲百度地图定位不准.我查了一下api,用了官方给出的这样一组函数. //创建查询对象 var geolocation = ne ...

  5. Android 进入页面默认定位到ListView的解决方法

    由于ListView会默认去获取焦点,如果说ListView在页面的下方的话,那么点击条目进入新页面并退出,那么这时候就会定位到ListView这里,而不是展示头部.   解决这个问题,只需要在Lis ...

  6. autodesk fbx sdk sample里面的工程无法调试解决方法

    1.项目属性->常规中的目标文件名改为1 2.链接器->调试中的生成程序数据库文件改成:$(OutDir)1.pdb 3.连接器->常规中的输出文件改成:$(OutDir)1.exe ...

  7. 关于selenium无法定位动态元素的解决方法

    今天在写一个关于登录的自动化脚本时,在对元素定位时总会报下面的错:

  8. Thinkphp5.1允许uni-app的H5跨域请求接口解决方法

    情景: uni-app使用vue框架开发混合APP,虽然APP或者小程序没有跨域,但希望就是写完这个既有H5,又有APP,小程序等,所以能通过后端解决跨域最好.但是不知道是vue的原因还是什么,在PH ...

  9. h5页面滑动卡顿解决方法

    解决方式: 给滚动的元素加样式:-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling(允许独立的滚动区域和触摸回弹) 如果值为au ...

随机推荐

  1. http协议以及get和post请求

    HTTP协议是网络传输信息的一种规范. 就好比两个人之间的交流,甲只会讲英语,乙只会说汉语,结果是他们必然无法开怀畅谈. HTTP协议也类   GET 请求获取 Request-URI 所标识的资源 ...

  2. Python实现杨辉三角,超详细!

    巧妙实现杨辉三角代码 def triangles(): N=[1] #初始化为[1],杨辉三角的每一行为一个list while True: yield N #yield 实现记录功能,没有下一个ne ...

  3. Python threading中lock的使用

    版权声明: https://blog.csdn.net/u012067766/article/details/79733801在多线程中使用lock可以让多个线程在共享资源的时候不会“乱”,例如,创建 ...

  4. RBAC 基于权限的访问控制 serviceaccount -- clusterRole clusterRoleBinding

    1.Role , RoleBinding 的作用对象都是namespace. 2.通过RoleRef,可以看到,RoleBinding对象通过名字,直接引用前面定义的Role,实现subject(us ...

  5. CF908D New Year and Arbitrary Arrangement 期望、DP

    题目传送门 题意:给出正整数$pa,pb,k$,最开始你有一个空串,每一次你有$\frac{pa}{pa + pb}$的概率向串最后放一个$a$,有$\frac{pb}{pa + pb}$的概率向串最 ...

  6. JVM调优——之CMS 常见参数解析

    最近在学习使用CMS这个GC,这里记录下常用的参数. 1. UseCMSCompactAtFullCollection 与 CMSFullGCsBeforeCompaction 有一点需要注意的是:C ...

  7. es5中for...in 和es6中 for..of遍历

    //定义一个数组 var arr=['A','B','C']; //定义一个对象 var obj={name:'张三',age:20} // for..in 遍历数组 得到索引 for(var x i ...

  8. Flutter - 自动生成Android & iOS图标

    对于要发布的app来说,做图标是一个麻烦的事,你需要知道N个图标的分辨率,然后用PhotoShop一个个修改导出. PS好图标之后,按照各自的位置放进去. ********************** ...

  9. [原]Veracrypt使用Yubikey作为安全令牌

    今天刚刚到货Yubikey 5 美亚 直邮 ,易客满国际,国内居然是顺丰配送,点个赞. 必备的控件 https://developers.yubico.com/yubikey-manager-qt/R ...

  10. 初级Java工程师面试所遇面试题

    1.servlet的生命周期 : 一.百度百科 : 1.客户端请求servlet: 2.加载servlet类到内存: 3.实例化并调用init()方法初始化servlet: 4.调用service() ...