没有SSL连接的地理位置 [英] Geolocation Without SSL connection
问题描述
我正在开发一个使用地理位置坐标的应用程序。
我使用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]
编辑:它在我的机器上使用localhost:80但在http上的测试网址中无效
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!\n\nlat = " + position.coords.latitude + "\nlng = " + 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! \n\n"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
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 !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};
tryGeolocation();
这篇关于没有SSL连接的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!