如何使用传单图表在React.js中显示数组中的标记 [英] How to display markers from array in reactjs using leaflet charts

查看:86
本文介绍了如何使用传单图表在React.js中显示数组中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个索引的数组-ID,LAT,LNG. 我想获得LAT&从我的数组中读取LNG,并在标记上设置值. 对于第一个索引,我要显示一个PopUp.

我将单张图表用于reactjs.

代码:

import './App.css';
import React from 'react'
import { Map as LeafletMap, TileLayer, Marker, Popup } from 'react-leaflet';


class Map extends React.Component {
  constructor() {
    super()
    this.state = {
      coords: [
[1, 41.19197, 25.33719],
[2, 41.26352, 25.1471],
[3, 41.26365, 25.24215],
[4, 41.26369, 25.33719],
[5, 41.26365, 25.43224],
[6, 41.26352, 25.52728],
[7, 41.2633, 25.62233],
[8, 41.263, 25.71737],
[9, 41.30828, 22.95892],
[10, 41.31041, 23.054],
      ],
      zoom: 7
    }
  }

  render() {


    const position = [this.state.coords];
    return (
      <LeafletMap center={[42.733883, 25.485830]} zoom={this.state.zoom}>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='https://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />

        <Marker position={position}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
      </LeafletMap>
    );
  }
}


export default Map

错误是:TypeError:latlng为空

如何在标记上设置第一个索引和第二个索引,并在弹出窗口上设置零索引?

解决方案

使用map遍历对象数组,并使用map索引捕获第一项.您无需将索引存储在coords数组内:

this.state = {
      coords: [
        { lat: 41.19197, lng: 25.33719 },
        { lat: 41.26352, lng: 25.1471 },
        { lat: 41.26365, lng: 25.24215 },
        { lat: 41.26369, lng: 25.33719 },
        { lat: 41.26365, lng: 25.43224 },
        { lat: 41.26352, lng: 25.52728 },
        { lat: 41.2633, lng: 25.62233 },
        { lat: 41.263, lng: 25.71737 },
        { lat: 41.3082, lng: 22.95892 },
        { lat: 41.31041, lng: 23.054 }
      ],
      zoom: 7
    };   

 ....

在渲染功能中:

  {coords.map(({ lat, lng }, index) => (
      <Marker position={[lat, lng]} icon={customMarker} key={index}>
          <Popup>
            {index + 1} is for popup with lat: {lat} and lon {lng}
          </Popup>
      </Marker>
    ))}

演示

I have one array with three indexes - ID , LAT, LNG. I want to get LAT & LNG from my array and set the values on my marker. For the first index I want to display a PopUp.

I use leaflet charts for reactjs.

The code:

import './App.css';
import React from 'react'
import { Map as LeafletMap, TileLayer, Marker, Popup } from 'react-leaflet';


class Map extends React.Component {
  constructor() {
    super()
    this.state = {
      coords: [
[1, 41.19197, 25.33719],
[2, 41.26352, 25.1471],
[3, 41.26365, 25.24215],
[4, 41.26369, 25.33719],
[5, 41.26365, 25.43224],
[6, 41.26352, 25.52728],
[7, 41.2633, 25.62233],
[8, 41.263, 25.71737],
[9, 41.30828, 22.95892],
[10, 41.31041, 23.054],
      ],
      zoom: 7
    }
  }

  render() {


    const position = [this.state.coords];
    return (
      <LeafletMap center={[42.733883, 25.485830]} zoom={this.state.zoom}>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='https://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />

        <Marker position={position}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
      </LeafletMap>
    );
  }
}


export default Map

The error is: TypeError: latlng is null

How can I set the first and the second indexes on my marker and zero index on popup ?

解决方案

Iterate over the array of objects using map and catch the first item using map index. You do not need to store the index inside the coords array:

this.state = {
      coords: [
        { lat: 41.19197, lng: 25.33719 },
        { lat: 41.26352, lng: 25.1471 },
        { lat: 41.26365, lng: 25.24215 },
        { lat: 41.26369, lng: 25.33719 },
        { lat: 41.26365, lng: 25.43224 },
        { lat: 41.26352, lng: 25.52728 },
        { lat: 41.2633, lng: 25.62233 },
        { lat: 41.263, lng: 25.71737 },
        { lat: 41.3082, lng: 22.95892 },
        { lat: 41.31041, lng: 23.054 }
      ],
      zoom: 7
    };   

 ....

In the render function:

  {coords.map(({ lat, lng }, index) => (
      <Marker position={[lat, lng]} icon={customMarker} key={index}>
          <Popup>
            {index + 1} is for popup with lat: {lat} and lon {lng}
          </Popup>
      </Marker>
    ))}

Demo

这篇关于如何使用传单图表在React.js中显示数组中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆