2016-10-26 38 views
4

工作這是我的javascript代碼:navigator.geolocation.getCurrentPosition/watchPosition沒有在安卓6.0

function getLocation() { 
    //navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:30000, enableHighAccuracy:true}); 
    var mobile =jQuery.browser.mobile; 
    var deviceAgent = navigator.userAgent.toLowerCase(); 
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/); 
    if(mobile){ 
     watchLocation(function(coords) { 
     var latlon = coords.latitude + ',' + coords.longitude; 
     //some stuff 
     }, function() { 
     alert("error"); 
     }); 
    } else { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(showPosition); 
     } else { 
      alert("error"); 
     } 
    } 
} 

function watchLocation(successCallback, errorCallback) { 
    successCallback = successCallback || function(){}; 
    errorCallback = errorCallback || function(){}; 
    // Try HTML5-spec geolocation. 
    var geolocation = navigator.geolocation; 
    if (geolocation) { 
     // We have a real geolocation service. 
     try { 
      function handleSuccess(position) { 
      alert("position:"+position.coords); 
      successCallback(position.coords); 
      } 
      geolocation.watchPosition(handleSuccess, errorCallback, { 
      enableHighAccuracy: true, 
      maximumAge: 5000 // 5 sec. 
      }); 
     } catch (err) { 
      errorCallback(); 
     } 
    } else { 
     errorCallback(); 
    } 
} 

我曾經嘗試都getCurrentPositionwatchPosition

當控制到geolocation.watchPosition行時它達到errorCalback()方法。

我在Motorola G 2nd GenAndroid 6Google chrome browseropera mini測試。

更新1:當我把錯誤的警報回撥功能我得到error:1; message:Only Secure origins are allowed(see:link)。

navigator.geolocation.getCurrentPosition(showPosition, function(e) 
    { alert(e); //alerts error:1; message:Only Secure origins are allowed(see: ) 
     console.error(e); 
    }) 

更新2:g4s8我能夠findout錯誤是因爲不安全的URL的幫助。即只訪問http,而不是https。但隨後我也繞過了瀏覽器通過點擊高級按鈕。但它會提示爲Do you want to allow location,我不希望..有沒有任何方式訪問的位置沒有提示它?

+1

您是否嘗試將第二個參數(錯誤回調)添加到'getCurrentPosition'方法? 'navigator.geolocation.getCurrentPosition(showPosition,function(e){console.error(e);})' – g4s8

+0

yes ...你可以看到第二行被註釋了......'errorCoor'就是回調函數.. 。 –

+0

你可以打印錯誤到控制檯並向此問題添加錯誤消息嗎?它可能包含有用的信息。例如'PositionError {code:1,消息:「用戶拒絕地理定位」}' – g4s8

回答

5

您的頁面應通過https提供以訪問地理位置API。

Geolocation API Removed from Unsecured Origins

Starting with Chrome 50, Chrome no longer supports obtaining the user's location using the HTML5 Geolocation API from pages delivered by non-secure connections

...

It is an important issue as it will directly impact any site that requires use of the geolocation API and is not served over https

爲了解決這個問題爲你的頁面通過HTTPS或localhost


Thank you...Is there any way to bypass it??

您可以嘗試使用一些地理定位服務,例如 geoip2Geolocation request


how to use them? can you show an example?? from those two can i access user location without knowing them?

GeoIP2通過IP地址檢測您的位置。你可以用JS LIB獲得國家(geoip2.country()),市(geoip2.city):

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script> 

這裏https://dev.maxmind.com/geoip/geoip2/javascript/你可以找到完整的文檔。

谷歌地圖的地理位置是谷歌服務,所以你首先需要get api key。然後,你可以用JSON參數發送POST請求https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY並得到響應:

{ 
    "location": { 
    "lat": 51.0, 
    "lng": -0.1 
    }, 
    "accuracy": 1200.4 
} 

,其中位置是用戶的估計緯度和經度,以度 和精度估計位置的精度,以米爲單位。

完整JSON參數defenition您可以在 「請求正文」 這裏段https://developers.google.com/maps/documentation/geolocation/intro#overview

您也可以找到有用的那些答案發現:getCurrentPosition() and watchPosition() are deprecated on insecure origins


using IP it provides only country and city..??

是的,僅此。

will it provide physical location like how getCurrent Position provides??

不,您不能獲取物理位置,因爲只能通過凝膠位置API訪問,因此只能在不安全的上下文中進行限制。

你也有多一個選項。您只能在https服務器上託管一個頁面(即訪問地理位置API),並且可以通過此頁面將用戶位置重定向到http站點以獲取參數。

/* https page */ 
navigator.geolocation.getCurrentPosition(function (result) { 
    window.location.href = "http://your.site.com/http-page?lat=" + result.latitude + "&long=" + result.longitude; 
}); 
+0

謝謝......有什麼辦法可以繞過它? –

+0

如何使用它們?你能舉一個例子嗎?從這兩個我可以訪問用戶的位置而不知道他們? –

+0

g4s8使用IP它只提供國家和城市.. ??它會提供物理位置,例如getCurrent Position如何提供? –