如何在Next.js中使用从数据库获取的坐标向Leaflet地图添加标记?

huangapple go评论111阅读模式
英文:

How to add Markes to a Leaflet-map with coordinates from database in Next.js?

问题

我必须在我的数据库中显示所有地点的标记。目前,我只能显示一个标记。

我已经映射了数据库中的数组,但我不知道如何将这两者连接起来。我会非常感激任何帮助。

到目前为止,这是我的代码:

<div>
  <MapContainer
    center={position}
    zoom={13}
    scrollWheelZoom={true}
    style={{ height: 500 }}
    // whenCreated={setMap}
    animate={true}
  >
    <TileLayer
      attribution='&copy; <a href="http://www.openstreetmap.org/copyright"'
      url="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    <Marker position={position}>
      <Popup>Hi there!</Popup>
    </Marker>
  </MapContainer>

  {places.map((place) => (
    <div key={place.id}>
      {place.longCoord}, {place.latCoord}
    </div>
  )}
</div>

请注意,这是你的代码的翻译部分。

英文:

I have to display markers for all the places in my database. At the moment I can display just one marker.

I maped over the array from database but I do not know how to connecte these two. I would really appreciate any help.
This is my code so far:

&lt;div&gt;
      &lt;MapContainer
        center={position}
        zoom={13}
        scrollWheelZoom={true}
        style={{ height: 500 }}
        // whenCreated={setMap}
        animate={true}
      &gt;
        &lt;TileLayer
          attribution=&#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&#39;
          url=&quot;https://tile.openstreetmap.org/{z}/{x}/{y}.png&quot;
        /&gt;
        &lt;Marker position={position}&gt;
          &lt;Popup&gt;Hi there!&lt;/Popup&gt;
        &lt;/Marker&gt;
      &lt;/MapContainer&gt;

      {places.map((place) =&gt; (
        &lt;div key={place.id}&gt;
          {place.longCoord}, {place.latCoord}
        &lt;/div&gt;
      ))}
    &lt;/div&gt;
  );

答案1

得分: 2

只需在MapContainer中使用.map,如下所示:

<MapContainer
  center={position}
  zoom={13}
  scrollWheelZoom={true}
  style={{ height: 500 }}
  animate={true}
>
  <TileLayer
    attribution='&copy; <a href="http://www.openstreetmap.org/copyright"'
    url="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
  />
  {places.map((item, i) => (
    <Marker key={i} position={{ lat: item.latCoord, lng: item.longCoord}}>
      <Popup>Hi there!</Popup>
    </Marker>
  ))}
</MapContainer>;
英文:

just use .map inside your MapContainer. like below

&lt;MapContainer
center={position}
        zoom={13}
        scrollWheelZoom={true}
        style={{ height: 500 }}
        // whenCreated={setMap}
        animate={true}
&gt;
&lt;TileLayer
          attribution=&#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&#39;
          url=&quot;https://tile.openstreetmap.org/{z}/{x}/{y}.png&quot;
        /&gt;
  {places.map((item, i) =&gt; (
    &lt;Marker key={i} position={{ lat: item.latCoord, lng: item.longCoord}}&gt;
      &lt;Popup&gt;Hi there!&lt;/Popup&gt;
    &lt;/Marker&gt;
  ))}
&lt;/MapContainer&gt;;

huangapple
  • 本文由 发表于 2023年2月27日 04:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定