python通过批量经纬度生成标记地图生成

使用Python读取Excel文件中的经纬度数据,创建定位地图的教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python通过批量经纬度生成标记地图生成

通过读取excel文件中经纬度数据生成定位图

from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType
import pandas as pd


def mapa():
    city = 'xx区县'
    g = Geo(init_opts=opts.InitOpts(width='1850px', height='850px'))
    g.add_schema(maptype=city)
    # 定义坐标对应的名称,添加到坐标库中 add_coordinate(name, lng, lat)
    # 导入excel表举例
    df = pd.read_excel("学校机构经纬度表.xlsx")
    df.head()
    # 导入自定义的地点经纬度
    for i in range(len(df)):
        if isinstance(df.iloc[i]['经度'], float):
            g.add_coordinate(df.iloc[i]['机构名称'], df.iloc[i]['经度'], df.iloc[i]['纬度'])
            print(df.iloc[i]['机构名称'], df.iloc[i]['经度'], df.iloc[i]['纬度'])
            # data_pair= [(df.iloc[i]['机构名称'], int(df.iloc[i]['学校类型编号']))]
    data_pair = [(df.iloc[i]['机构名称'], int(df
### 如何在 Excel 中根据经纬度数据生成地图 要在 Excel 中根据经纬度数据生成地图,可以采用多种方式实现。以下是几种常见的解决方案: #### 方法一:使用百度地图 API 将经纬度数据可视化 通过调用百度地图 API,可以在网页端展示 Excel 文件中的经纬度数据,并将其嵌入到 Excel 或其他应用中作为补充说明[^1]。 具体操作流程如下: - 首先,在 Python 脚本中读取 Excel 数据。 - 使用 `requests` 库向百度地图 API 发送请求,获取对应的地理编码信息。 - 结合 JavaScript 和 HTML 创建交互式的地图页面。 示例代码: ```python import pandas as pd import requests def get_baidu_map_url(lat, lng): api_key = 'your_api_key_here' base_url = f"http://api.map.baidu.com/staticimage/v2?ak={api_key}&center={lng},{lat}&width=500&height=400&zoom=15" return base_url # 读取 Excel 数据 df = pd.read_excel('data_with_coordinates.xlsx') df['map_url'] = df.apply(lambda row: get_baidu_map_url(row['latitude'], row['longitude']), axis=1) print(df[['latitude', 'longitude', 'map_url']]) ``` 此方法适用于需要高精度显示地理位置的应用场景。 --- #### 方法二:借助 MATLAB 导入 SHP 地图数据并绘制点位 如果熟悉 MATLAB 编程环境,则可以通过加载 Shapefile(SHP)格式的地图边界文件来完成任务[^2]。 主要步骤包括: 1. **导入 SHP 数据**:利用 Mapping Toolbox 工具箱打开指定区域的矢量图形资源; 2. **提取 EXCEL 表格内的坐标列表**:运用函数如 `xlsread()` 获取目标位置集合; 3. **叠加标注标记**:最后把计算所得的结果渲染于底图之上形成最终效果。 样例脚本片段: ```matlab % 加载中国行政区划 shapefile 文件 shpFile = shaperead('china_provinces.shp'); figure; geoshow(shpFile,'FaceColor','none'); hold on; % 假设已知某几个城市的 GPS 定位参数存储在一个名为 locations 的矩阵里 locations = xlsread('city_locations.xls'); scatter(locations(:,1), locations(:,2),'r*'); title('Cities Distribution Over China Map'); xlabel('Longitude (°E)'); ylabel('Latitude (°N)') axis equal tight; grid minor; ``` 这种方法适合科研人员或者工程技术人员用来分析大规模空间分布特征的研究课题之中。 --- #### 方法三:Python 自动化处理地名转为经纬度后再绘图 当原始资料仅有文字描述的地名而非确切数值形式的时候,可依赖第三方库比如 GeoPy 来自动解析转换成标准格式再进一步加工呈现给用户查看[^3]。 下面给出一段简单的示范程序用于演示这一过程的工作原理: ```python from geopy.geocoders import Nominatim import folium def convert_address_to_coords(addresses_list): geo_locator = Nominatim(user_agent="geo_app") coords_results = [] for addr in addresses_list: loc_info = geo_locator.geocode(addr) if loc_info is not None: lat_lng_pair = [loc_info.latitude, loc_info.longitude] coords_results.append(lat_lng_pair) return coords_results addresses_df = pd.DataFrame({ "Place Names": ["Beijing", "Shanghai"] }) coordinates_data = convert_address_to_coords(list(addresses_df["Place Names"])) mymap = folium.Map(location=[sum([c[0]for c in coordinates_data])/len(coordinates_data), sum([c[1]for c in coordinates_data])/len(coordinates_data)], zoom_start=6) for coord_set in coordinates_data: marker_icon = folium.Icon(color='blue') folium.Marker(coord_set, icon=marker_icon).add_to(mymap) mymap.save("output_map.html") ``` 上述例子展示了如何批量查询多个城市中心点的具体座标值并将它们逐一放置到一张动态HTML文档里面供浏览者在线查阅详情。 --- #### 方法四:ArcGIS Online 提供更专业的 GIS 解决方案 对于更加复杂的需求来说,可以选择 ArcGIS 平台所提供的服务功能[^4]。它可以轻松连接数据库源并且支持实时更新属性字段内容从而方便二次开发定制专属样式主题等等高级特性。 创建自定义要素图层实例代码如下所示: ```javascript require(["esri/layers/FeatureLayer"], function(FeatureLayer){ var featureLayer = new FeatureLayer({ url : "<path-to-service>" }); }); ``` 以上介绍了四种不同的技术路线可供参考选用依据实际项目背景条件灵活调整实施方案即可达成预期目的。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lele198838683

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值