UID86279性别保密经验 EP铁粒 粒回帖0主题精华在线时间 小时注册时间2021-7-25最后登录1970-1-1
|
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>
复制代码
这是例子
结果 |
|