React Map - Add and Remove

To add routes at design-time, pass an array of objects to the routes property. A route requires only its locations to be specified. The locations will be connected and make up the route.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Map } from 'devextreme-react/map';
  •  
  • const mapRoutes = [{
  • locations: [
  • { lat: 40.782500, lng: -73.966111 },
  • "40.755833, -73.986389",
  • [ 40.753889, -73.981389 ],
  • "Brooklyn Bridge,New York,NY"
  • ]
  • }];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Map
  • defaultZoom={5}
  • routes={mapRoutes}
  • />
  • );
  • }
  • }
  •  
  • export default App;

View Demo

To add or remove a route at runtime, bind the routes property of the Map to a state property.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Map } from 'devextreme-react/map';
  • import { Button } from 'devextreme-react/button';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.state = {
  • mapRoutes: [{
  • locations: [
  • { lat: 40.782500, lng: -73.966111 },
  • "40.755833, -73.986389",
  • [ 40.753889, -73.981389 ],
  • "Brooklyn Bridge,New York,NY"
  • ]
  • }]
  • };
  •  
  • this.addRoute = this.addRoute.bind(this);
  • this.removeRoute = this.removeRoute.bind(this);
  • }
  •  
  • addRoute() {
  • this.state.mapRoutes.push({
  • locations: [
  • "40.753889, -73.981389",
  • "Brooklyn Bridge,New York,NY"
  • ]
  • });
  • }
  •  
  • removeRoute() {
  • this.state.mapRoutes.pop();
  • }
  •  
  • render() {
  • return (
  • <div>
  • <Map
  • defaultZoom={10}
  • routes={this.state.mapRoutes}
  • />
  • <Button
  • text="Add the Route"
  • onClick={this.addRoute}
  • />
  • <Button
  • text="Remove the Route"
  • onClick={this.removeRoute}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;
See Also