Browse Source

Pytyhon 使用百度地图API 进行 IP普通定位和地图显示

hongwenjun 3 năm trước cách đây
mục cha
commit
34b9d10037
5 tập tin đã thay đổi với 184 bổ sung49 xóa
  1. 5 1
      README.md
  2. 32 5
      app.py
  3. 52 0
      php/maps.php
  4. 57 43
      templates/hello.html
  5. 38 0
      templates/maps.html

+ 5 - 1
README.md

@@ -4,7 +4,7 @@
 
 ### 本WebAPI支持IP城市定位和域名查IP定位,同时支持命令行查询
 
-![](https://262235.xyz/usr/uploads/2021/08/3947416904.png)
+![](https://262235.xyz/usr/uploads/2021/08/650223153.webp)
 
 ## 安装部署简易命令
 ```
@@ -24,3 +24,7 @@ docker run -d -p 80:5000 --restart=always --name ip hongwenjun/ip
 ### 搭建WebAPI参考文章
 [Python网络开发简单的IP城市定位WebAPI](https://262235.xyz/index.php/archives/342/)
 
+
+[Pytyhon 使用百度地图API 进行 IP普通定位和地图显示](https://www.262235.xyz/index.php/archives/375/)
+
+演示网址: https://www.262235.xyz/ip/maps/

+ 32 - 5
app.py

@@ -1,4 +1,4 @@
-import ipdb, ipaddress
+import ipdb, ipaddress, requests, json
 from flask import Flask, request, render_template, jsonify
 from socket import gethostbyname
 
@@ -9,6 +9,10 @@ def iplocated(ip):
     city = db.find(ip, "CN")
     return ip + " @" + city[0] + city[1] + city[2] + city[3] + "\n"
 
+def getcity(ip):
+    city = db.find(ip, "CN")
+    return  city[2] + '市'
+
 def getip():
     ip = request.remote_addr
     try:
@@ -27,15 +31,33 @@ def is_Mozilla():
     else:
         return False
 
+def ip2bdgps(ip):
+    url = 'https://api.map.baidu.com/location/ip?ak=iUGOVyqGFo4QOP6QhTuZPF1P6FV5GB7p&coor=bd09ll&ip=' + ip
+    r = requests.get(url)
+    json_str = r.text
+    data = json.loads(json_str)
+
+    if data['status'] != 0 :
+        return  (116.39564504, 39.92998578 , data['status'])    # 查不到返回 北京 x,y
+    else:
+        x = data['content']['point']['x']
+        y = data['content']['point']['y']
+    return  (x, y, data['status'], data)
+
 @app.route("/")
 def hello():
     ip = getip()
     if is_Mozilla():
-        return ip + render_template('hello.html') + "\n\n" + request.headers["User-Agent"]
+        return ip + render_template('hello.html', ip=ip) + "\n\n" + request.headers["User-Agent"]
     else:
         return ip
 
[email protected]("/ip")
[email protected]("/ip/maps/")
+def maps():
+    ip = getip()
+    bdgps = ip2bdgps(ip)
+    return  render_template('maps.html',  bdgps=bdgps)
+
 @app.route("/ip/")
 @app.route("/ip/<ipaddr>")
 def show_ip(ipaddr=None):
@@ -44,7 +66,7 @@ def show_ip(ipaddr=None):
         ip = getip()
         ipaddr = iplocated(ip)
         if is_Mozilla():
-            return  render_template('hello.html',  ip=ip, ipaddr=ipaddr)
+            return  render_template('hello.html',  ip=ip, ipaddr=ipaddr, city=getcity(ip))
         else:
             return ip
     else:
@@ -63,8 +85,13 @@ def show_ip(ipaddr=None):
 
     return ipaddr
 
+
+
+
 if __name__ == '__main__':
-	app.run(host='0.0.0.0', debug=True)
+        app.run(host='0.0.0.0', debug=True)
 
 # export FLASK_ENV=development   # 调试模式: 修改代码不用重启服务
 # flask run --host=0.0.0.0       # 监听所有公开的 IP
+
+

+ 52 - 0
php/maps.php

@@ -0,0 +1,52 @@
+<?php
+$city = strval($_GET["city"]);
+?>
+
+<!DOCTYPE html>  
+<html>
+<head>  
+<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
+<title>百度地图-城市定位</title>  
+<style type="text/css">  
+html{height:100%}  
+body{height:100%;margin:0px;padding:0px}  
+#container{height:100%}  
+</style>  
+<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF">
+</script>
+</head>  
+ 
+<body>  
+<div id="container"></div> 
+<script type="text/javascript"> 
+var map = new BMap.Map("container");
+// 创建地图实例  
+var point = new BMap.Point(116.404, 39.915);
+// 创建点坐标  
+map.centerAndZoom(point, 15);
+// 初始化地图,设置中心点坐标和地图级别  
+map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
+map.addControl(new BMap.NavigationControl()); // 添加标准地图控件
+
+
+// 创建地址解析器实例     
+var myGeo = new BMap.Geocoder();      
+// 将地址解析结果显示在地图上,并调整地图视野    
+myGeo.getPoint( "<?php echo $city ?>" , function(point){      
+    if (point) {      
+        map.centerAndZoom(point, 15);      
+        map.addOverlay(new BMap.Marker(point));
+
+		map.addControl(new BMap.NavigationControl());    
+		map.addControl(new BMap.ScaleControl());    
+		map.addControl(new BMap.OverviewMapControl());    
+		map.addControl(new BMap.MapTypeControl());   
+    }      
+ }, 
+ "<?php echo $city ?>");
+</script>  
+
+</body>  
+</html>
+

+ 57 - 43
templates/hello.html

@@ -1,44 +1,58 @@
 <!DOCTYPE html><head><meta charset="utf-8">
-<title>Hello,亲: 本WebAPI支持IP城市定位和域名查IP定位</title>
-</head>
-<html><body style="text-align:center;">
-{% if ip %}
-<h1>Hello, 来自{{ ip }} 的亲:</h1>
-<h3>{{ ipaddr }}</h3> <br><br><br>
-
-
-{% else %}
-  <h1>Hello World!</h1>
-{%  endif  %}
-
-<div>IP/域名: <input id="name" type="text">
-<input type="button" id="btn" value="提交"></div>
-
-<script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
-<script>
-    $("#btn").click(function () {
-        jump_url()
-    })
-    function jump_url(params) {
-        var name = $("#name").val();
-        const url = window.location.href;
-        window.location.href = '/ip/' + name
-    }
-</script>
-<br><br><br><br><br><br>
-
-本WebAPI支持IP城市定位和域名查IP定位,同时支持命令行查询<br>
-<br>github源码:<a href="https://github.com/hongwenjun/ip">https://github.com/hongwenjun/ip</a>
-<br>
-<br>docker镜像:<a href="https://hub.docker.com/r/hongwenjun/ip">https://hub.docker.com/r/hongwenjun/ip</a>
-<h3>搭建WebAPI参考文章</h3>
-<a href="https://262235.xyz/index.php/archives/342/">Python网络开发简单的IP城市定位WebAPI</a>
-<p>
-
-<h3>命令行 curl 使用:</h3>
-<pre>
-curl https://262235.xyz/ip/            # 命令行 URL 要带 /
-
-curl https://262235.xyz/ip/8.8.8.8    # 查询自定义 IP 定位
-
-curl https://262235.xyz/ip/github.com   # 按域名查 IP 定位
+    <title>Hello,亲: 本WebAPI支持IP城市定位和域名查IP定位</title>
+    </head>
+    <html><body style="text-align:center;">
+    {% if ip %}
+    <h2>Hello, 来自 {{ city }} 的亲:</h2>
+    <a href="https://262235.xyz/ip/maps/"> <img style="margin:5px" 
+        src="https://api.map.baidu.com/staticimage/v2?ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF&mcode=666666&center=110,35&width=500&height=300&zoom=4 "/>
+        </a>
+    <h3>{{ ipaddr }}: <a href="https://262235.xyz/maps.php?city={{ city }}">查看地图</a></h3> 
+    {% else %}
+      <h1>Hello World!</h1>
+    {%  endif  %}
+
+    <div>IP/域名: <input id="name" type="text">
+    <input type="button" id="btn" value="提交">
+    <input type="button" id="json" value="获得json"></div>
+
+    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
+    <script>
+        $("#btn").click(function () {
+            jump_url()
+        })
+        function jump_url(params) {
+            var name = $("#name").val();
+            const url = window.location.href;
+            window.location.href = '/ip/' + name
+        }
+
+        $("#json").click(function () {
+            getjson()
+        })
+        function getjson(params) {
+            var ip = $("#name").val();
+            const url = 'https://api.map.baidu.com/location/ip?ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF&ip=';
+            window.location.href = url + ip + '&coor=bd09ll'
+        }
+
+    </script>
+    <br><br><br><br><br><br>
+
+    本WebAPI支持IP城市定位和域名查IP定位,同时支持命令行查询<br>
+    <br>github源码:<a href="https://github.com/hongwenjun/ip">https://github.com/hongwenjun/ip</a>
+    <br>
+    <br>docker镜像:<a href="https://hub.docker.com/r/hongwenjun/ip">https://hub.docker.com/r/hongwenjun/ip</a>
+    <h3>搭建WebAPI参考文章</h3>
+    <a href="https://262235.xyz/index.php/archives/342/">Python网络开发简单的IP城市定位WebAPI</a>
+    <p>
+
+    <h3>命令行 curl 使用:</h3>
+    <pre>
+    curl https://262235.xyz/ip/            # 命令行 URL 要带 /
+
+    curl https://262235.xyz/ip/8.8.8.8    # 查询自定义 IP 定位
+
+    curl https://262235.xyz/ip/github.com   # 按域名查 IP 定位
+
+

+ 38 - 0
templates/maps.html

@@ -0,0 +1,38 @@
+<!DOCTYPE html>  
+<html>
+<head>  
+<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
+<title>服务端IP普通定位</title>  
+<style type="text/css">  
+html{height:100%}  
+body{height:100%;margin:0px;padding:0px}  
+#container{height:100%}  
+</style>  
+<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF">
+</script>
+</head>  
+ 
+<body>  
+<div id="container"></div> 
+<script type="text/javascript"> 
+var map = new BMap.Map("container");
+// 创建地图实例
+{% if bdgps %}
+var point = new BMap.Point({{ bdgps[0] }} ,{{ bdgps[1] }});
+{% else %}
+var point = new BMap.Point(116.404, 39.915);
+{%  endif  %}
+// 创建点坐标  
+map.centerAndZoom(point, 15);
+// 初始化地图,设置中心点坐标和地图级别  
+map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
+map.addControl(new BMap.NavigationControl());    
+map.addControl(new BMap.ScaleControl());    
+map.addControl(new BMap.OverviewMapControl());    
+map.addControl(new BMap.MapTypeControl());    
+</script>
+{{ bdgps[3] }}
+</body>  
+</html>
+