阿巴ing_ 发表于 2023-9-10 09:51:06

腾讯地图获取您的ip定位

[贴吧_吐舌]
kep请去相关网站申请
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>IP定位</title>
</head>
<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&libraries=service&key=6F4BZ-6QXKL-473PF-EJUG7-6Y5MQ-6CBH7"></script>

<body>
    <div id="container"></div>
    <div id="panel">
      <p><input type='text' id='ipInput' placeholder="输入IP地址(默认为请求端的IP)" size='30'/><input type='button' id='locate' value='搜索所在位置' onclick='locate()' /></p>
      <p id="ipLocationResult"></p>
    </div>
</body>


<script type="text/javascript">
var map = new TMap.Map('container', {
zoom: 14,
center: new TMap.LatLng(40.0402718, 116.2735831),
});
var ipLocation = new TMap.service.IPLocation(); // 新建一个IP定位类
var markers = new TMap.MultiMarker({
map: map,
geometries: [],
});
function locate() {
var ipInput = document.getElementById('ipInput').value;
var params = ipInput ? { ip: ipInput } : {};
ipLocation
    .locate(params)
    .then((result2) => {
      // 未给定ip地址则默认使用请求端的ip
      var { result } = result2;
      markers.updateGeometries([
      {
          id: 'main',
          position: result.location, // 将所得位置绘制在地图上
      },
      ]);
      map.setCenter(result.location);
      document.getElementById(
      'ipLocationResult'
      ).innerText = `您的IP/您输入的IP所在位置:${result.ad_info.nation},${result.ad_info.province}`;
    })
    .catch((error) => {
      document.getElementById(
      'ipLocationResult'
      ).innerText = `错误:${error.status},${error.message}`;
    });
}
locate();
</script>
</html>

这是例子

结果

阿巴ing_ 发表于 2023-9-10 09:52:33

本帖最后由 阿巴ing_ 于 2023-9-10 09:55 编辑

lbs.qq.com
相关网站

我也不知道怎么只显示位置文字别问我
页: [1]
查看完整版本: 腾讯地图获取您的ip定位