如何在 Rails 7 中使用 Leaflet?

发布于 2025-01-14 06:44:31 字数 2045 浏览 3 评论 0原文

我想做的事情: 我想在 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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      }).addTo(this.map);
    });
  }

  disconnect(){
    this.map.remove()
  }

}

我得到了什么: 我到处都有随机的瓷砖,如下所示: 输入图片这里的描述

我可以点击图块并移动和缩放,然后它们会闪烁,仍然很乱,甚至覆盖 Rails html 文本,如下所示: 输入图片此处描述

我尝试过的:

  1. 使用 importmap 安装 leaflet-css
  2. 并使用 '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:
enter image description here

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:
enter image description here

What I've tried:

  1. Installing leaflet-css with importmap
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2025-01-21 06:44:32

好的,我自己找到了答案:Leaflet 缺少 css 文件。

当使用 importmap 安装 leaflet-css 时(正如我上面提到的),以下行会自动添加到 config/importmap.rb

pin "leaflet-css", to: "https://ga.jspm.io/npm:[email protected]/dist/leaflet.css.min.js"

然后我添加在 map_controller.js 文件顶部添加以下行:

import "leaflet-css"

并将以下内容添加到 div 标记中(样式标记仅用于此处测试。我将使用tailwind):

 class="leaflet-container" style="min-height: 800px;" 

然后它按预期工作:
输入图像描述这里

最终简单且合乎逻辑。我有预感我会喜欢 Rails 7!

Ok found the answer myself: Leaflet was missing its css files.

When installing leaflet-css with importmap (as I mentioned above) the following line is automatically added to config/importmap.rb :

pin "leaflet-css", to: "https://ga.jspm.io/npm:[email protected]/dist/leaflet.css.min.js"

I then added the following line in the top of the map_controller.js file:

import "leaflet-css"

and added the following to the div tag (The style tag is just for testing here. I'll be using tailwind):

 class="leaflet-container" style="min-height: 800px;" 

And then it works as expected:
enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文