Nir*_*ddy 14 html5 geolocation
我正在开发一个使用地理位置坐标的应用程序.我使用HTML5地理位置功能来查找网站访问者的位置,但问题在于Chrome不支持不安全来源的GeoLocation.
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.");
}
}
Run Code Online (Sandbox Code Playgroud)
以下是代码getCurrentPosition 无法正常工作,因为我的网站未连接SSL.
Chrome警告: getCurrentPosition()和watchPosition()不再适用于不安全的来源.要使用此功能,您应该考虑将应用程序切换到安全的来源,例如HTTPS.
有没有其他方法可以获取chrome上的坐标/ LatLong值?[仅限不安全连接]
编辑:它使用localhost:80在我的机器上工作但不在http上的测试网址中工作
Man*_*lim 19
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();
Run Code Online (Sandbox Code Playgroud)
Ary*_*itz 15
真的很简单:打开 Chrome,打开这个地址并输入你想要启用的域
chrome://flags/#unsafely-treat-insecure-origin-as-secure
Run Code Online (Sandbox Code Playgroud)
这样它是永久性的,您不需要每次都打开 chrome 浏览器
google-chrome --args --unsafely-treat-insecure-origin-as-secure="http://whatever.test"`
Run Code Online (Sandbox Code Playgroud)
如上所述。
sta*_*her 11
Chrome 现在可以防止不受信任的站点使用 html5 地理定位。受信任的站点包括localhost或任何 https 认证域。在我的情况下,这些都不可能,所以我从命令行用这个参数打开了 chrome:open -a /Applications/Google\ Chrome.app' --args --unsafely-treat-insecure-origin-as-secure="http://yoursite.test"将我的特定域添加为受信任的站点。
小智 5
这是我在2017/05年针对Safari的非SSL解决方案
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:
// dirty hack for safari
if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
tryAPIGeolocation();
} else {
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();
Run Code Online (Sandbox Code Playgroud)
新的部分是“ case error.POSITION_UNAVAILABLE”中的这一部分:
case error.POSITION_UNAVAILABLE:
// dirty hack for safari
if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
tryAPIGeolocation();
} else {
alert("Browser geolocation error !\n\nPosition unavailable.");
}
break;
Run Code Online (Sandbox Code Playgroud)