简体   繁体   English

iPython中的Folium地图未显示地图。 引发错误:“未捕获的ReferenceError:未定义L”

[英]Folium map in iPython not displaying map. Throws error: 'Uncaught ReferenceError: L is not defined'

Attempting to display a basic map in iPython using folium leaflet library. 尝试使用大叶小叶库在iPython中显示基本地图。 Recent install of iPython through Anaconda with Folium installed with Pip. 最近通过Anaconda安装了iPython,并通过Pip安装了Folium。 Confirmed everything is up to date 确认一切都是最新的

ran this code in iPython 在iPython中运行此代码

import folium
map = folium.Map(location=[48, -102], zoom_start=3)
map.create_map('map.html')
map

I receive a blank frame. 我收到一个空白框。 I checked the console on the html. 我在html上检查了控制台。 I receive a number of Failed to load resource: net::ERR_FILE_NOT_FOUND tracing back to an Uncaught ReferenceError: L is not defined . 我收到许多无法加载资源的信息: net::ERR_FILE_NOT_FOUND追溯回Uncaught ReferenceError: L is not defined I checked the html document and found the leaflet reference looks like this: 我检查了html文档,发现传单参考如下:

    src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js">

I assume the issue is with the relative link but I have no found information in the folium docs to resolve this issue. 我认为问题出在相对链接上,但是我在folium文档中没有找到解决此问题的信息。

Thanks ya'll for the help. 谢谢您的帮助。 I look forward to paying it forward. 我期待着向前支付。

I've found this tutorial on Folium in iPython Notebooks quite helpful. 我发现iPython Notebooks中有关Folium的教程很有帮助。 Also, I did a detailed answer on this other Stackoverflow question which seems related. 另外,我对这个似乎相关的其他Stackoverflow问题做了详细的回答。

To display in the iPython notebook, you need to generate the html with the myMap._build_map() method, wrap it in an iframe, and return the iFrame to iPython for display. 要在iPython笔记本中显示,您需要使用myMap._build_map()方法生成html,将其包装在iframe中,然后将iFrame返回给iPython进行显示。

Here's an example for your situation: 这是您情况的一个示例:

import folium  
from IPython.display import HTML
myMap = folium.Map(location=[48, -102], zoom_start=3)
myMap._build_map() 
mapWidth, mapHeight = (400,500) # width and height of the iFrame in pixels
srcdoc = myMap.HTML.replace('"', '"')
embed = HTML('<iframe srcdoc="{}" '
             'style="width: {}px; height: {}px; display:block; width: 50%; margin: 0 auto; '
             'border: none"></iframe>'.format(srcdoc, width, height))
embed

Note that the .create_map() method will save the full map HTML to a file, while you want to keep the HTML code handy for presentation in iPython- this is why we use ._build_map() instead. 请注意, .create_map()方法会将完整的地图HTML保存到文件中,同时您希望保留HTML代码以便在iPython中方便显示-这就是我们改用._build_map()的原因。 The line beginning with embed is where the magic happens- we wrap the HTML content generated by folium in an iframe which can be styled as desired, and then is returned as the cell's output. embed开头的那行是发生魔术的地方-我们将folium生成的HTML内容包装到iframe中,该iframe可以根据需要设置样式,然后作为单元格的输出返回。 IPython internally calls .display() on the results returned to the cell, so you should have a nice, centered map. IPython在返回到单元格的结果上内部调用.display() ,因此您应该有一个漂亮的居中地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM