【发布时间】:2017-01-03 22:09:48
【问题描述】:
我正在使用 google geolocation 的 getCurrentPosition() 函数来获取用户的当前位置。
它在 Firefox 上对我来说很好,但在 chrome 上却不行。
我的代码如下::
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p id="demo">Click the button to get your position.</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showtemp);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&key=AIzaSyDOgvRydLLNrztjgagobYS_sROK1u3r4M4&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}
function showtemp(temp) {
alert("test");
}
function showError(error) {
$.get("http://ipinfo.io", function (response) {
var array = (response.loc).split(',');
console.log(array[0]);
var latlon = array[0] + "," + array[1];
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}, "jsonp");
}
</script>
</body>
</html>
请帮我解决这个问题。
它给了我错误 ::“ getCurrentPosition() 和 watchPosition() 在不安全的来源上被弃用,并且支持将在未来被删除。您应该考虑将您的应用程序切换到安全的来源,例如 HTTPS。”
提前致谢
【问题讨论】:
-
它在 chrome 中不起作用,而不是在控制台中显示错误。用 chrome 的控制台错误更新你的问题??
-
getcurrentposition() 已被弃用,并且没有替代它。阅读这个答案:- getcurrentposition-and-watchposition-are-deprecated-on-insecure-origins
-
那么有什么办法可以使用最新的 chrome Version = 52 吗?
-
注意:
I am using the google geolocation's getCurrentPosition() function- 这与谷歌无关 - 这是一个浏览器功能
标签: javascript php geolocation