如何在 Rails 7 中使用 Leaflet?
我想做的事情: 我想在 Rails 7 应用程序中使用 Leaflet。
我做了什么: 我创建了 Rails 应用程序并生成了一个名为 map
的模型(带有控制器和视图),只有一个标题。然后我添加了一条地图记录。
我向应用程序添加了传单,如下所示:
./bin/importmap pin leaflet
我在 app/views/map/show.html.erb
中添加了一个带有刺激属性的地图 div
,现在看起来像这样
<p style="color: green"><%= notice %></p>
<%= render @map %>
<div data-controller="map" data-target="map.placeholder">
<div><%= link_to "Edit this map", edit_map_path(@map) %> | <%= link_to "Back to maps", maps_path %>
<%= button_to "Destroy this map", @map, method: :delete %> </div>
:然后创建了 app/javascript/controllers/map_controller.js
,如下所示:
// map_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "placeholder" ]
connect(){
import("leaflet").then( L => {
this.map = L.map(this.placeholderTarget,{zoomDelta:0.5,zoomSnap:0.5}).setView([ 50.1960, -6.8712 ], 10);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(this.map);
});
}
disconnect(){
this.map.remove()
}
}
我可以点击图块并移动和缩放,然后它们会闪烁,仍然很乱,甚至覆盖 Rails html 文本,如下所示:
我尝试过的:
- 使用 importmap 安装
leaflet-css
- 并使用
'leaflet-container' 添加类属性
代码>
但似乎一切都没有改变。
任何关于如何让 Leaflet 在 Rails 7 中工作的建议将不胜感激! (我真的很想让它与默认的热线和刺激一起工作)。谢谢。
What I would like to do:
I would like to use Leaflet in a Rails 7 app.
What I did:
I created my rails app and generated a model (with controller and views) called map
with only a title. I then added a single map record.
I added leaflet to the app like this:
./bin/importmap pin leaflet
I added a map div
with stimulus attributes in app/views/map/show.html.erb
which now looks like this:
<p style="color: green"><%= notice %></p>
<%= render @map %>
<div data-controller="map" data-target="map.placeholder">
<div><%= link_to "Edit this map", edit_map_path(@map) %> | <%= link_to "Back to maps", maps_path %>
<%= button_to "Destroy this map", @map, method: :delete %> </div>
I then created app/javascript/controllers/map_controller.js
looking like this:
// map_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "placeholder" ]
connect(){
import("leaflet").then( L => {
this.map = L.map(this.placeholderTarget,{zoomDelta:0.5,zoomSnap:0.5}).setView([ 50.1960, -6.8712 ], 10);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(this.map);
});
}
disconnect(){
this.map.remove()
}
}
What I got:
I got random tiles here and there like this:
I can click on the tiles and move and zoom, and then they flicker, are still messy and even overlay the rails html text like this:
What I've tried:
- Installing
leaflet-css
with importmap - And adding a class attribute with
'leaflet-container'
but nothing seems to change.
Any suggestions for how I could get Leaflet working in Rails 7 would be greatly appreciated! (I would really like to get it to work with the default hotwire and stimulus). Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我自己找到了答案:Leaflet 缺少 css 文件。
当使用
importmap
安装leaflet-css
时(正如我上面提到的),以下行会自动添加到config/importmap.rb
:然后我添加在
map_controller.js
文件顶部添加以下行:并将以下内容添加到
div
标记中(样式标记仅用于此处测试。我将使用tailwind):然后它按预期工作:

最终简单且合乎逻辑。我有预感我会喜欢 Rails 7!
Ok found the answer myself: Leaflet was missing its css files.
When installing
leaflet-css
withimportmap
(as I mentioned above) the following line is automatically added toconfig/importmap.rb
:I then added the following line in the top of the
map_controller.js
file:and added the following to the
div
tag (The style tag is just for testing here. I'll be using tailwind):And then it works as expected:

Easy and logical in the end. I have a feeling I'm going to like Rails 7!