問題描述
我正在開發一個使用地理位置坐標的應用程序.我使用 HTML5 地理定位功能來找出網站訪問者的位置,但問題在于 Chrome 不支持不安全來源的 GeoLocation.
I am developing an application which uses geolocation coordinates. I used the HTML5 geolocation feature to find out the location of the site visitor, but the issue is with Chrome which doesnt support GeoLocation on insecure origins.
function showPosition(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
document.getElementById("result").innerHTML = positionInfo;
});
} else{
alert("Sorry, your browser does not support HTML5 geolocation.");
}
}
這里的代碼 getCurrentPosition
無法使用,因為我的網站沒有連接 SSL.
Here is the code getCurrentPosition
is not working in as my site is not SSL connected .
Chrome 警告: getCurrentPosition() 和 watchPosition() 不再適用于不安全的來源.要使用此功能,您應該考慮將您的應用程序切換到安全來源,例如 HTTPS.
Warning by Chrome : getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS.
還有其他方法可以在 chrome 上獲取坐標/LatLong 值嗎?[僅在不安全的連接上]
Is there any other way to get coordinates/LatLong values on chrome ? [on Insecure connection only]
EDIT :它在我的機器上使用 localhost:80 工作,但在 http 上的測試 url 中不工作
EDIT : It is working in my machine using localhost:80 but not working in test url which is on http
推薦答案
var apiGeolocationSuccess = function(position) {
alert("API geolocation success!
lat = " + position.coords.latitude + "
lng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error!
"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!
lat = " + position.coords.latitude + "
lng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !
Timeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !
Position unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};
tryGeolocation();
這篇關于沒有 SSL 連接的地理位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!