通过百度API 获取当前城市3种方法讲解

2021-06-22
浏览量:170

方法一:

function showLocation(data) {

  console.log(data.content);
  //alert(data.content.address_detail.city)
}
$.getScript("https://api.map.baidu.com/location/ip?ak=9zNKGguAbdNC6xwD7syftt533eIf7cSn&callback=showLocation")

方法二:

<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=您的ak"></script>

  var myCity = new BMap.LocalCity();
    myCity.get(function(data){
    console.log(data)

  });

方法三:


fetch("https://api.map.baidu.com/location/ip?ip=输入ip&ak=您的ak") // 后台直接请求


再推荐一个可用的搜狐API

<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>

<script type="text/javascript"> 

console.log(returnCitySN) //返回对象Object 

{cip: "122.70.200.146", cid: "110000", cname: "北京市"} 

</script>