gpt4 book ai didi

javascript - 如何获取用户的位置?

转载 作者:行者123 更新时间:2023-12-04 17:54:17 24 4
gpt4 key购买 nike

我能够在我的网站上获取用户的 IP 地址。现在我需要他的国家、城市、地区使用我得到的他的 IP。我正在尝试使用以下代码的 Google Geolocation API,

    <div id="yourinfo"></div>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script>
<script type="text/javascript">
if(google.loader.ClientLocation)
{
visitor_lat = google.loader.ClientLocation.latitude;
visitor_lon = google.loader.ClientLocation.longitude;
visitor_city = google.loader.ClientLocation.address.city;
visitor_region = google.loader.ClientLocation.address.region;
visitor_country = google.loader.ClientLocation.address.country;
visitor_countrycode = google.loader.ClientLocation.address.country_code;
document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>';
}
else
{
document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>';
}
</script>

真的,这段代码对我不起作用。它正在执行 else 部分并显示 Whoops!。如何获取用户的地理位置。任何其他想法让我知道。

我已经尝试了以下代码,效果很好,但在免费版本中需要 6 秒的延迟。后来我才知道谷歌免费提供这项服务。所以我试试看。

http://services.ipaddresslabs.com/iplocation/locateip?key=SAK58RWP983694534K4Z&ip=".$ClientIP

提前致谢

最佳答案

如果您愿意使用 HTML5,您应该访问这个 tutorial on geolocation .

关于javascript - 如何获取用户的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12967753/

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