You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中gmaps库在Jupyter Notebook无法渲染地图的报错求助

gmaps热力图在Jupyter Notebook无法显示的问题

问题描述

目标是使用gmaps库生成并显示热力图,但运行代码后Jupyter Notebook无法显示地图,抛出如下错误。

运行代码

import gmaps
import gmaps.datasets
from matplotlib import pyplot as plt
import pandas as pd

gmaps.configure(api_key="AIzaSyCtd6p2bI-ZTdHvBlEXhhpO8xi5uHfiedk")
earthquake_df=gmaps.datasets.load_dataset_as_df("earthquakes")
print(earthquake_df.head())

locations = earthquake_df[['latitude', 'longitude']]
weights = earthquake_df['magnitude']
print(locations)
print(weights)

fig = gmaps.figure()
fig.add_layer(gmaps.heatmap_layer(locations=locations, weights=weights))
fig

错误信息

[Open Browser Console for more detailed log - Double click to close this message]
Model class 'FigureModel' from module 'jupyter-gmaps' is loaded but can not be instantiated
TypeError: Class constructor L cannot be invoked without 'new'
    at new t (http://localhost:8888/nbextensions/jupyter-gmaps/index.js?v=20231220195354:39:179771)
    at u._make_model (http://localhost:8888/nbextensions/jupyter-js-widgets/extension.js?v=20231220195354:2:747368)

解决方案

这个错误源于jupyter-gmaps版本与Leaflet库的兼容性冲突,按以下步骤修复:

  • 卸载当前gmaps版本:pip uninstall -y gmaps
  • 安装兼容的稳定版本(如0.9.0):pip install gmaps==0.9.0
  • 重新启用Jupyter Notebook扩展:jupyter nbextension enable --py gmaps
  • 若使用Jupyter Lab,额外安装Lab扩展:jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-gmaps
  • 重启Jupyter环境后重新运行代码

另外确认你的Google API密钥已正确启用Google Maps JavaScript API权限。

内容的提问来源于stack exchange,提问作者İrem Ertürk 1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:06:12