[英]Get coordinates from Hardware GPS
我发现了很多有关GPS坐标的问题,但是没有一个问题可以确认使用移动硬件GPS而不是geoGPS之类的Web GPS。
我的实际方法:
我正在使用navigator.geolocation.getCurrentPosition()
, Lat/Long
来自网络,这是代码:
function getGPS(funcCallBack)
{
if (navigator.geolocation)
{
var timeoutVal = getCookie("GPSTimeout");
navigator.geolocation.getCurrentPosition(sucess
,error
,{enableHighAccuracy: true
,timeout: timeoutVal
,maximumAge: 0}
);
}
else{
alert('GPS is turned off, or was not possible to find it. Now, doing the login without localization.');
window.gpsLat = 0;
window.gpsLng = 0;
window.gpsAcc = 0;
funcCallBack();
}
function sucess(position) //sucess
{
window.gpsLat = position.coords.latitude;
window.gpsLng = position.coords.longitude;
window.gpsAcc = position.coords.accuracy;
funcCallBack();
}
function error() //error
{
window.gpsLat = 0;
window.gpsLng = 0;
window.gpsAcc = 0;
funcCallBack();
}
}
我的问题:
有时,当我登录时,没有获得GPS坐标(它们为0),有时却获得了2,000精度以上的坐标(这是不精确的)。
顺便说一句,我正在数据互联网服务上测试GPS,当我确实使用Wi-Fi连接时,它的准确度低于100。
细节:
也许您在抱怨:
timeoutVal
:它是其中包含数字5000
的cookie。 funcCallBack
:它是继续登录操作的函数。 window.gpsLat
:这是一个全局变量,其中包含从geoLocation
的Latitude
值。 window.gpsLng
:这是一个全局变量,其中包含从geoLocation
的Longitude
值。 window.gpsAcc
:这是一个全局变量,其中包含从geoLocation
的Accuracy
值。 我想要什么?
我想要用JavaScript或PHP解决方案,它可以从移动硬件设备,本机GPS而不是地理位置获取坐标,并且当本机GPS关闭时,请用户将其打开。
您应该使用javascript而不是PHP来获取位置。 PHP仅能够执行IP查找,这是确定位置的最不准确的方法。
navigator.geolocation.getCurrentPosition()
工作方式是使用当前可用的最准确的数据。 对于移动设备,如果启用,它将首先使用GPS,然后使用Wi-Fi。
如果启用了本地GPS,则javascript将访问该数据而不是Wi-Fi数据,但是如果GPS数据不可用,则无法阻止对Wi-Fi数据进行检查。
最好的解决方案是检查准确性字段,如果该字段不在您满意的范围内,请用户启用GPS。
另外,如果您要构建混合应用程序,则大多数框架(PhoneGap等)都具有可直接查询设备GPS的API。 使用PhoneGap检查是否启用了GPS
地理位置API并未提供直接的方法来检查GPS是否打开,但您可以捕获地理位置错误,并且基于错误类型可以从中得出结论。
例如POSITION_UNAVAILABLE(2),如果网络中断或无法联系定位卫星。 但是不确定要处理一些条件的捷径!
我将建议使用watchPostion {我同意其用于观察并连续定位的意思}您可以保持打开状态,如果GPS抛出错误,则可以提示自定义提醒,以使用户打开GPS设备/ wifi /互联网。成功回调,您可以清除手表。
var watch =null;
function success(position)
{
var lat = position.coords.latitude;
var lon= position.coords.longitude;
if (watch != null )
/*Need to take care .. as maybe there is no gps and user
want it off so keep attempt 3 times or some kind a way out otherwise it will
infinite loop */
{
navigator.geolocation.clearWatch(watch);
watch = null;
}
}
function getLatLon()
{
var geolocOK = ("geolocation" in navigator);
if ( geolocOK )
{
var option = {enableHighAccuracy:true, maximumAge: 0,timeout:10000 };
watch = navigator.geolocation.watchPosition(success, fails, option);
}
else {
//disable the current location?
}
}
function fails()
{
alert("please turn on the GPS !");
}
getLatLon();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.