方法一:
<html>
<head>
<title>獲取地址</title>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script>
var cityCode=returnCitySN.cid;
document.write('<hr><br><h1> 老鐵位置:'+JSON.stringify(returnCitySN)+'</h1>');
</script>
</head>
<body>
</body>
</html>
效果:
轉載地址:https://blog.csdn.net/HaHa_Sir/article/details/82627997
方法二:
百度地圖獲取
<html>
<head>
<title>獲取地址</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
</head>
<body>
<script>
$.getScript("https://api.map.baidu.com/location/ip?ak=自己的&callback=showLocation");
function showLocation(data) {
console.log(data.content.address_detail);
alert(data.content.address_detail.city);
alert(data.content.address_detail.province);
}
</script>
</body>
</html>
效果: