update an array of markers regarding the onDragEnd method












0















My problem is the following.
I am trying to add some markers to the map and make them draggable.
i add markers with the onclick method ( the code will be below)
but my problem is i dont know how to update the markers coordinates with the onDragEnd method...
I render the markers with a map because the number of markers is not fixed. it depends on the user's click.



export default class Map extends Component {
constructor(props) {
super(props);

this.state = {
viewport: {
latitude: 35.82004085,
longitude: 139.58855534,
zoom: 17,
bearing: 0,
pitch: 0
},
markers:{
"type": "MarkerColection",
"markersList":
},
events: {},
movingMarker:
};
} // end construstor

_updateViewport = viewport => this.setState({
viewport:{...this.setState.viewport, ...viewport}
});


_logDragEvent(name, event) {
this.setState({
events: {
...this.state.events,
[name]: event.lngLat,
}
})
}

_onMarkerDragStart = (event) => {
this._logDragEvent('onDragStart', event);
};

_onMarkerDrag = (event) => {
this._logDragEvent('onDrag', event);
};

_onMarkerDragEnd = (event) => {
this._logDragEvent('onDragEnd', event);

var newMovingMarker= [
event.lngLat[0],
event.lngLat[1]
];

///////HERE I WOULD LIKE TO UPDATE THE MARKER POSITION///
///////WITH THIS COORDINATES!!!!!////////////////////////
/////////////////////////////////////////////////////////
this.setState({movingMarker: newMovingMarker});
};



_renderMarker = (marker, index) => {
const {movingMarker} = this.state;
return (
<Marker
key={`marker-${index}`}
latitude={marker.coordinates[1]}
longitude={marker.coordinates[0]}
offsetLeft={-10}
offsetTop={-10}
draggable={true}
onDragStart={this._onMarkerDragStart}
onDrag={this._onMarkerDrag}
onDragEnd={this._onMarkerDragEnd}
>
<span class='glyphicon glyphicon-flag'></span>
</Marker>

);
}

onMapClick = event => {


var coord = event.lngLat;
const {markers} = this.state;

var marker = {
"type":"marker",
"coordinates": [
coord[0],
coord[1]
],
"properties": {
"id": String(new Date().getTime())
}
};

markers.markersList.push(marker);

this.setState({markers: markers});

}; // end of onMapClick

render() {
const {viewport, markers} = this.state;



return(
<MapGL
{...viewport}
width="100%"
height="100%"
mapStyle= "mapbox://styles/mapbox/streets-v10"
mapboxApiAccessToken={TOKEN}
onViewportChange={this._updateViewport}
onClick={this.onMapClick}>

{markers.markersList.map(this._renderMarker) }

<div className="nav" style={navStyle}>
<NavigationControl onViewportChange={this._updateViewport} />
</div>
</MapGL>


); // end of return

} // end of render
}


I can get the coordinate after onDragEnd, but i dont know how to correctly update each marker.....



I really need help on it...
Thank you guys










share|improve this question



























    0















    My problem is the following.
    I am trying to add some markers to the map and make them draggable.
    i add markers with the onclick method ( the code will be below)
    but my problem is i dont know how to update the markers coordinates with the onDragEnd method...
    I render the markers with a map because the number of markers is not fixed. it depends on the user's click.



    export default class Map extends Component {
    constructor(props) {
    super(props);

    this.state = {
    viewport: {
    latitude: 35.82004085,
    longitude: 139.58855534,
    zoom: 17,
    bearing: 0,
    pitch: 0
    },
    markers:{
    "type": "MarkerColection",
    "markersList":
    },
    events: {},
    movingMarker:
    };
    } // end construstor

    _updateViewport = viewport => this.setState({
    viewport:{...this.setState.viewport, ...viewport}
    });


    _logDragEvent(name, event) {
    this.setState({
    events: {
    ...this.state.events,
    [name]: event.lngLat,
    }
    })
    }

    _onMarkerDragStart = (event) => {
    this._logDragEvent('onDragStart', event);
    };

    _onMarkerDrag = (event) => {
    this._logDragEvent('onDrag', event);
    };

    _onMarkerDragEnd = (event) => {
    this._logDragEvent('onDragEnd', event);

    var newMovingMarker= [
    event.lngLat[0],
    event.lngLat[1]
    ];

    ///////HERE I WOULD LIKE TO UPDATE THE MARKER POSITION///
    ///////WITH THIS COORDINATES!!!!!////////////////////////
    /////////////////////////////////////////////////////////
    this.setState({movingMarker: newMovingMarker});
    };



    _renderMarker = (marker, index) => {
    const {movingMarker} = this.state;
    return (
    <Marker
    key={`marker-${index}`}
    latitude={marker.coordinates[1]}
    longitude={marker.coordinates[0]}
    offsetLeft={-10}
    offsetTop={-10}
    draggable={true}
    onDragStart={this._onMarkerDragStart}
    onDrag={this._onMarkerDrag}
    onDragEnd={this._onMarkerDragEnd}
    >
    <span class='glyphicon glyphicon-flag'></span>
    </Marker>

    );
    }

    onMapClick = event => {


    var coord = event.lngLat;
    const {markers} = this.state;

    var marker = {
    "type":"marker",
    "coordinates": [
    coord[0],
    coord[1]
    ],
    "properties": {
    "id": String(new Date().getTime())
    }
    };

    markers.markersList.push(marker);

    this.setState({markers: markers});

    }; // end of onMapClick

    render() {
    const {viewport, markers} = this.state;



    return(
    <MapGL
    {...viewport}
    width="100%"
    height="100%"
    mapStyle= "mapbox://styles/mapbox/streets-v10"
    mapboxApiAccessToken={TOKEN}
    onViewportChange={this._updateViewport}
    onClick={this.onMapClick}>

    {markers.markersList.map(this._renderMarker) }

    <div className="nav" style={navStyle}>
    <NavigationControl onViewportChange={this._updateViewport} />
    </div>
    </MapGL>


    ); // end of return

    } // end of render
    }


    I can get the coordinate after onDragEnd, but i dont know how to correctly update each marker.....



    I really need help on it...
    Thank you guys










    share|improve this question

























      0












      0








      0








      My problem is the following.
      I am trying to add some markers to the map and make them draggable.
      i add markers with the onclick method ( the code will be below)
      but my problem is i dont know how to update the markers coordinates with the onDragEnd method...
      I render the markers with a map because the number of markers is not fixed. it depends on the user's click.



      export default class Map extends Component {
      constructor(props) {
      super(props);

      this.state = {
      viewport: {
      latitude: 35.82004085,
      longitude: 139.58855534,
      zoom: 17,
      bearing: 0,
      pitch: 0
      },
      markers:{
      "type": "MarkerColection",
      "markersList":
      },
      events: {},
      movingMarker:
      };
      } // end construstor

      _updateViewport = viewport => this.setState({
      viewport:{...this.setState.viewport, ...viewport}
      });


      _logDragEvent(name, event) {
      this.setState({
      events: {
      ...this.state.events,
      [name]: event.lngLat,
      }
      })
      }

      _onMarkerDragStart = (event) => {
      this._logDragEvent('onDragStart', event);
      };

      _onMarkerDrag = (event) => {
      this._logDragEvent('onDrag', event);
      };

      _onMarkerDragEnd = (event) => {
      this._logDragEvent('onDragEnd', event);

      var newMovingMarker= [
      event.lngLat[0],
      event.lngLat[1]
      ];

      ///////HERE I WOULD LIKE TO UPDATE THE MARKER POSITION///
      ///////WITH THIS COORDINATES!!!!!////////////////////////
      /////////////////////////////////////////////////////////
      this.setState({movingMarker: newMovingMarker});
      };



      _renderMarker = (marker, index) => {
      const {movingMarker} = this.state;
      return (
      <Marker
      key={`marker-${index}`}
      latitude={marker.coordinates[1]}
      longitude={marker.coordinates[0]}
      offsetLeft={-10}
      offsetTop={-10}
      draggable={true}
      onDragStart={this._onMarkerDragStart}
      onDrag={this._onMarkerDrag}
      onDragEnd={this._onMarkerDragEnd}
      >
      <span class='glyphicon glyphicon-flag'></span>
      </Marker>

      );
      }

      onMapClick = event => {


      var coord = event.lngLat;
      const {markers} = this.state;

      var marker = {
      "type":"marker",
      "coordinates": [
      coord[0],
      coord[1]
      ],
      "properties": {
      "id": String(new Date().getTime())
      }
      };

      markers.markersList.push(marker);

      this.setState({markers: markers});

      }; // end of onMapClick

      render() {
      const {viewport, markers} = this.state;



      return(
      <MapGL
      {...viewport}
      width="100%"
      height="100%"
      mapStyle= "mapbox://styles/mapbox/streets-v10"
      mapboxApiAccessToken={TOKEN}
      onViewportChange={this._updateViewport}
      onClick={this.onMapClick}>

      {markers.markersList.map(this._renderMarker) }

      <div className="nav" style={navStyle}>
      <NavigationControl onViewportChange={this._updateViewport} />
      </div>
      </MapGL>


      ); // end of return

      } // end of render
      }


      I can get the coordinate after onDragEnd, but i dont know how to correctly update each marker.....



      I really need help on it...
      Thank you guys










      share|improve this question














      My problem is the following.
      I am trying to add some markers to the map and make them draggable.
      i add markers with the onclick method ( the code will be below)
      but my problem is i dont know how to update the markers coordinates with the onDragEnd method...
      I render the markers with a map because the number of markers is not fixed. it depends on the user's click.



      export default class Map extends Component {
      constructor(props) {
      super(props);

      this.state = {
      viewport: {
      latitude: 35.82004085,
      longitude: 139.58855534,
      zoom: 17,
      bearing: 0,
      pitch: 0
      },
      markers:{
      "type": "MarkerColection",
      "markersList":
      },
      events: {},
      movingMarker:
      };
      } // end construstor

      _updateViewport = viewport => this.setState({
      viewport:{...this.setState.viewport, ...viewport}
      });


      _logDragEvent(name, event) {
      this.setState({
      events: {
      ...this.state.events,
      [name]: event.lngLat,
      }
      })
      }

      _onMarkerDragStart = (event) => {
      this._logDragEvent('onDragStart', event);
      };

      _onMarkerDrag = (event) => {
      this._logDragEvent('onDrag', event);
      };

      _onMarkerDragEnd = (event) => {
      this._logDragEvent('onDragEnd', event);

      var newMovingMarker= [
      event.lngLat[0],
      event.lngLat[1]
      ];

      ///////HERE I WOULD LIKE TO UPDATE THE MARKER POSITION///
      ///////WITH THIS COORDINATES!!!!!////////////////////////
      /////////////////////////////////////////////////////////
      this.setState({movingMarker: newMovingMarker});
      };



      _renderMarker = (marker, index) => {
      const {movingMarker} = this.state;
      return (
      <Marker
      key={`marker-${index}`}
      latitude={marker.coordinates[1]}
      longitude={marker.coordinates[0]}
      offsetLeft={-10}
      offsetTop={-10}
      draggable={true}
      onDragStart={this._onMarkerDragStart}
      onDrag={this._onMarkerDrag}
      onDragEnd={this._onMarkerDragEnd}
      >
      <span class='glyphicon glyphicon-flag'></span>
      </Marker>

      );
      }

      onMapClick = event => {


      var coord = event.lngLat;
      const {markers} = this.state;

      var marker = {
      "type":"marker",
      "coordinates": [
      coord[0],
      coord[1]
      ],
      "properties": {
      "id": String(new Date().getTime())
      }
      };

      markers.markersList.push(marker);

      this.setState({markers: markers});

      }; // end of onMapClick

      render() {
      const {viewport, markers} = this.state;



      return(
      <MapGL
      {...viewport}
      width="100%"
      height="100%"
      mapStyle= "mapbox://styles/mapbox/streets-v10"
      mapboxApiAccessToken={TOKEN}
      onViewportChange={this._updateViewport}
      onClick={this.onMapClick}>

      {markers.markersList.map(this._renderMarker) }

      <div className="nav" style={navStyle}>
      <NavigationControl onViewportChange={this._updateViewport} />
      </div>
      </MapGL>


      ); // end of return

      } // end of render
      }


      I can get the coordinate after onDragEnd, but i dont know how to correctly update each marker.....



      I really need help on it...
      Thank you guys







      javascript reactjs mapbox-gl-js markers react-map-gl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 7:21









      j.schoj.scho

      73




      73
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294973%2fupdate-an-array-of-markers-regarding-the-ondragend-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294973%2fupdate-an-array-of-markers-regarding-the-ondragend-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma