Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. adding markers to the map reactjs

adding markers to the map reactjs

Scheduled Pinned Locked Moved JavaScript
helpdockerquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcielak
    wrote on last edited by
    #1

    I would like to add a marker to the map using the form. Can anyone help me? Installing the map works. I want to display markers after entering x and y, and read this data from the table. I have 2 inputs, one for X and the other for Y after clicking "add" I add this data to the table and I have 2 objects, etc ... but only one marker from state.items is displayed on the map, although I have more coordinates in the table.I know that the componentDidMount () method is only run once and here is the problem.Please Help

    state={
    items:[{
    "X": 13.6155611,
    "Y": 51.0331258
    },
    ],
    }

    addItem = (e) => {
    e.preventDefault();
    const newItem = {
    "Y": parseFloat(this.state.X),
    "X": parseFloat(this.state.Y),
    };
    this.setState(prevState => ({
    items: [...prevState.items, newItem]
    }));

    }
    componentDidMount() {
    const map = new mapboxgl.Map({
    container: this.mapContainer,
    center: [this.state.lng, this.state.lat],
    zoom: this.state.zoom,
    });
    for (var i = 0; i < this.state.items.length; i++) {
    var obj = this.state.items[i];
    let myLatlng = new mapboxgl.LngLat(obj.X, obj.Y);
    new mapboxgl.Marker()
    .setLngLat(myLatlng)
    .addTo(map);
    }

    }

    render() {
    return (

    this.mapContainer = el} />

    this.setState({
    X: e.target.value
    })}
    />
    this.setState({
    Y: e.target.value
    })}
    />

    )}

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups