gpt4 book ai didi

html - 从谷歌地图和 HTML 5 GeoLocation 获取国家代码

转载 作者:行者123 更新时间:2023-11-28 00:20:59 33 4
gpt4 key购买 nike

我正在尝试使用 HTML 5 GeoLocation 获取经度和纬度,然后使用 Google Maps API 获取该经度/纬度的国家/地区代码。谁能告诉我我的代码哪里出错了,我目前在 main.js 中收到 Javascript 错误“this.lat is not a function”:

    <html>

<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=drnhdhddfhgfghfg" type="text/javascript"></script>

<script type="text/javascript">

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(function(position) {

var lat = position.coords.latitude;
var lng = position.coords.longitude;

var latlng = new google.maps.LatLng(lat, lng);

$.post('http://maps.googleapis.com/maps/api/geocode/json', { latlng: latlng, sensor: false }, function (results) {
alert(results);
});
});




}
else {
alert("Geolocation services are not supported by your browser.");
}

</script>

</head>

<body>



</body>

</html>

最佳答案

试试这个:

<html>

<head>
<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
<script type="text/javascript">

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(function(position) {

var lat = position.coords.latitude;
var lng = position.coords.longitude;

var latlng = new google.maps.LatLng(lat, lng);

var geocoder = new google.maps.Geocoder();

geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
alert(results[7].formatted_address);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
});
} else {
alert("Geolocation services are not supported by your browser.");
}

</script>
</head>
<body>
</body>
</html>

关于html - 从谷歌地图和 HTML 5 GeoLocation 获取国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8941923/

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