Html 地理定位 HTML5 enableHighAccuracy True , False 还是最佳选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9053262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geolocation HTML5 enableHighAccuracy True , False or Best Option?
提问by Erhan H.
i have a problem about HTML5 geolocation feature. I use the code below to get location data. I use "enableHighAccuracy: false" option to work with Cell Based GPS feature. Accurancy is low but response it too fast. But some people always use Built-in GPS with their mobile phone, so this code does not work for them. Bu if i change accurency option as "enableHighAccuracy: true" it works for them. But this time, the code uses only built-in GPS. not CELL based GPS.
我有一个关于 HTML5 地理定位功能的问题。我使用下面的代码来获取位置数据。我使用“enableHighAccuracy: false”选项来处理基于蜂窝的 GPS 功能。准确率低,但反应太快。但是有些人总是在他们的手机上使用内置 GPS,所以这个代码对他们不起作用。但是,如果我将精度选项更改为“enableHighAccuracy:true”,则它对它们有效。但这一次,代码仅使用内置 GPS。不是基于 CELL 的 GPS。
The question -> How can i do that : First, try to get position from Built-in GPS with timeout (e.g. 5000ms ) if position cannot be got in this time just look for Cell Based position for timeout (e.g. 10000ms) if position cannot be get in this time, return an error message .
问题 -> 我该怎么做:首先,如果此时无法获得位置,请尝试从内置 GPS 获取位置超时(例如 5000 毫秒),如果位置不能,则查找基于单元的位置超时(例如 10000 毫秒)进入这个时候,返回一个错误信息。
Here is the code that i use now.
这是我现在使用的代码。
Thanks in advance.
提前致谢。
function getLocationfromGoogle() {
navigator.geolocation.getCurrentPosition(
function(pos) {
$("#lat_field").val(pos.coords.latitude);
$("#long_field").val(pos.coords.longitude);
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
geocoder.geocode({ 'latLng': latLng}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//console.log(results[0].formatted_address);
$("#adresim").val(results[0].formatted_address);
}
else {
alert('Google convertion is not succesfully done.');
}
});
},function error(msg){
alert('Please enable your GPS position future.');
},{maximumAge:600000, timeout:5000, enableHighAccuracy: false}
);
}
回答by Kevin
You should also be aware that the implementation of this varies from phone OS to phone OS - what works on Android may or may not work on iOS, BlackBerry, WindowsPhone, etc.
您还应该知道,此操作的实现因手机操作系统而异 - 在 Android 上运行的内容可能在 iOS、黑莓、WindowsPhone 等上运行,也可能不运行。
You're almost there, you just need to:
你快到了,你只需要:
- Specify
enableHighAccuracy: true
(you have it set tofalse
) - Handle the timeout error case in the error handler. If the error from the high accuracy query is timeout, then try it again with
enableHighAccuracy: false
.
- 指定
enableHighAccuracy: true
(您已将其设置为false
) - 在错误处理程序中处理超时错误情况。如果高精度查询的错误是超时,则使用
enableHighAccuracy: false
.
Have a look at this sample code.
看看这个示例代码。
You should also note that when testing this on a few devices, it returns location derived from WiFi even when enableHighAccuracy: true
.
您还应该注意,在一些设备上进行测试时,即使enableHighAccuracy: true
.
回答by peawormsworth
The code mentioned here: http://jsfiddle.net/CvSW4/did not work for me during error handling.
这里提到的代码:http: //jsfiddle.net/CvSW4/在错误处理期间对我不起作用。
The reason is that the error functions accept a parameter named 'position' but use an object in the functions called 'error'.
原因是错误函数接受名为“position”的参数,但在函数中使用名为“error”的对象。
function errorCallback_highAccuracy(position) { ... }
function errorCallback_lowAccuracy(position) { ... }
The solution to fix this was to switch the error methods to accept the input value as a parameter named 'error' and not 'position', since the error callbacks do not accept a position and throw an error object instead.
解决此问题的解决方案是切换错误方法以接受输入值作为名为“错误”而不是“位置”的参数,因为错误回调不接受位置并抛出错误对象。
function errorCallback_highAccuracy(error) { ... }
function errorCallback_lowAccuracy(error) { ... }
I mention it here, because I could not post on the resulting example page and also, this is the location where I linked through to find the code sample mentioned above.
我在这里提到它,因为我无法在生成的示例页面上发布,而且,这是我通过链接找到上面提到的代码示例的位置。