簡體   English   中英

從硬件GPS獲取坐標

[英]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。

細節:

也許您在抱怨:

  1. timeoutVal :它是其中包含數字5000的cookie。
  2. funcCallBack :它是繼續登錄操作的函數。
  3. window.gpsLat :這是一個全局變量,其中包含從geoLocationLatitude值。
  4. window.gpsLng :這是一個全局變量,其中包含從geoLocationLongitude值。
  5. window.gpsAcc :這是一個全局變量,其中包含從geoLocationAccuracy值。

我想要什么?

我想要用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM