大家好,我是明说网络的小明同学。
地图在日常生活中的使用越来越多,几乎成为了人们日常生活不可或缺的一部分,高德地图几乎成为了小明同学出门必备app之一。那么自然,对于程序员来说,客户对于地图的需求也是越来越多。
那么,程序员如何在项目中快速的搭建一个地图服务呢?今天我们就来介绍一个在python下快速搭建地图服务的方案。
小明同学在最开始接触地图服务的时候,使用的arcgis
arcgis非常的专业,允许你编辑图层,叠加图层,发布地图服务等。
感兴趣的可以到下面的地址看看,如果你对地图的需求非常的高,需要编辑自己的图层,那么建议你使用arcgis,否则可以接着往下看
https://www.esri.com/zh-cn/arcgis/products/arcgis-desktop/overviewwww.esri.com/zh-cn/arcgis/products/arcgis-desktop/overview
但,逐渐的我感觉到arcgis对于我这种仅仅是需要用地图来实现展示功能的小白来说,arcgis太重量级了,包含了太多小白不需要的功能。
直到有一天,我发现了folium,一款完全契合我的需求的python地图展示库。
folium makes it easy to visualize data that’s been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map.
简单来讲,folium可以实现对python数据的在地图上的交互式展示。
下面展示了三行代码实现python上的交互式地图展示
下面这个quickstart较好的展示了folium的功能(该页面也是可交互的哦),有非常多的例如,非常推荐适合入门
https://python-visualization.github.io/folium/quickstart.html#Getting-Startedpython-visualization.github.io/folium/quickstart.html#Getting-Started
在上述quickstart中,folium使用save函数,将地图保存为html文件,实现交互式展示。
但,这也太鸡肋了吧,我一个项目,还要临时生成html文件?不合理!!
不用担心,folium已经提供了解决方案。
Using folium with flaskpython-visualization.github.io/folium/flask.html
我们可以通过以下代码,在flask后台实现交互式地图的路由,是不是很爽!
""" flask_example.py Required packages: - flask - folium Usage: Start the flask server by running: $ python flask_example.py And then head to http://127.0.0.1:5000/ in your browser to see the map displayed"""from flask import Flaskimport foliumapp = Flask(__name__)@app.route('/')def index(): start_coords = (46.9540700, 142.7360300) folium_map = folium.Map(location=start_coords, zoom_start=14) return folium_map._repr_html_()if __name__ == '__main__': app.run(debug=True)
在前端你只需要使用:
其中{{ url_for('/'}}代表你路由的真实链接。这样,就可以展示交互式地图了。
以下为小明同学一个项目中的效果
测量CDN中的IPv6地址2001:7fd::1是否使用了anycast,如图所示,蓝色点为vantage point,蓝色圈为vp到IPv6地址2001:7fd::1的时延的2/3*光速。可以看出IPv6地址2001:7fd::1使用了anycast技术
留言与评论(共有 0 条评论) “” |