gpt4 book ai didi

javascript - 谷歌地图地理位置 getcurrentposition()

转载 作者:行者123 更新时间:2023-11-28 05:38:09 26 4
gpt4 key购买 nike

我正在使用谷歌地理定位的 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。”

提前致谢

最佳答案

getcurrentposition() 已被弃用并且没有替代品。阅读这个答案:- getcurrentposition-and-watchposition-are-deprecated-on-insec‌​ure-origins

点击这个谷歌更新的 API 的示例链接,它是工作示例。 : https://developers.google.com/maps/documentation/javascript/examples/map-geolocation

将鼠标悬停在代码块的右上角可复制代码或在 JSFiddle 中打开它。

使用此功能:

// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.

<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});

// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};

infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}

</script>

关于javascript - 谷歌地图地理位置 getcurrentposition(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39179800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com