如何在ReactJs项目中使用Esri Arcgis地图?

9

我正在尝试使用Esri地图。要在我的项目中包含地图,我找到了以下内容:

require([
    "esri/map",
    "esri/dijit/Search",
    "esri/dijit/LocateButton",
    "esri/geometry/Point",
    "esri/symbols/SimpleFillSymbol",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol",

但是没有esri文件夹或npm包。因此,我在这里感到困惑。如何在项目中导入esri?

3个回答

5
使用esri-loader加载所需的esri模块。这是一个渲染底图的组件。
import React, { Component } from 'react';
import { loadModules } from 'esri-loader';

const options = {
  url: 'https://js.arcgis.com/4.6/'
};

const styles =  {
  container: {
    height: '100vh',
    width: '100vw'
  },
  mapDiv: {
    padding: 0,
    margin: 0,
    height: '100%',
    width: '100%'
  },
}

class BaseMap extends Component {

  constructor(props) {
    super(props);
    this.state = {
      status: 'loading'
    }
  }

  componentDidMount() {
    loadModules(['esri/Map', 'esri/views/MapView'], options)
      .then(([Map, MapView]) => {
        const map = new Map({ basemap: "streets" });
        const view = new MapView({
          container: "viewDiv",
          map,
          zoom: 15,
          center: [78.4867, 17.3850]
        });
        view.then(() => {
          this.setState({
            map,
            view,
            status: 'loaded'
          });
        });
      })

  }

  renderMap() {
    if(this.state.status === 'loading') {
      return <div>loading</div>;
    }
  }

  render() {

    return(
          <div style={styles.container}>
            <div id='viewDiv' style={ styles.mapDiv } >
              {this.renderMap()}
            </div>
          </div>
    )
  }
}

export default BaseMap;

这将呈现一个基本地图,但不是响应式的。如果我删除围绕视图div的div或者如果我将外部div(围绕viewDiv)的高度和宽度设置为相对值({ height: '100%', width: '100%'}),则地图不会呈现。不知道为什么。欢迎提出任何使其响应式的建议。


你能帮我展示在地图上每个标记位置对应的弹出窗口或信息窗口吗? - Deep Kakkar

3

除了上面提到的方法外,还有一种方法是在esri-react-router-example中演示的。该应用程序使用一个叫做esri-loader的库来在仅需要的组件/路由中延迟加载ArcGIS API。例如:

首先,安装esri-loader库:

npm install esri-loader --save

然后在任何React模块中导入esri-loader函数:

import * as esriLoader from 'esri-loader'

然后使用懒加载加载 ArcGIS API:
componentDidMount () {
  if (!esriLoader.isLoaded()) {
    // lazy load the arcgis api
    const options = {
      // use a specific version instead of latest 4.x
      url: '//js.arcgis.com/3.18compact/'
    }
    esriLoader.bootstrap((err) => {
      if (err) {
        console.error(err)
      }
      // now that the arcgis api has loaded, we can create the map
      this._createMap()
    }, options)
  } else {
    // arcgis api is already loaded, just create the map
    this._createMap()
  }
},

然后加载需要创建地图的ArcGIS API的(Dojo)模块:

_createMap () {
  // get item id from route params or use default
  const itemId = this.props.params.itemId || '8e42e164d4174da09f61fe0d3f206641'
  // require the map class
  esriLoader.dojoRequire(['esri/arcgis/utils'], (arcgisUtils) => {
    // create a map at a DOM node in this component
    arcgisUtils.createMap(itemId, this.refs.map)
    .then((response) => {
      // hide the loading indicator
      // and show the map title
      // NOTE: this will trigger a rerender
      this.setState({
        mapLoaded: true,
        item: response.itemInfo.item
      })
    })
  })
}

使用esri-loader相比上述方法的好处是,您不必使用Dojo加载器和工具链来加载和构建整个应用程序。您可以使用自己选择的React工具链(如webpack等)。
这篇博客文章解释了这种方法的工作原理,并将其与其他(类似)应用程序中使用的方法进行比较,如esri-redux

请注意,当使用esri-loader时,您不需要惰性加载ArcGIS API。相反,您可以通过在index.html中使用脚本标记来加载ArcGIS API,例如<script src="//js.arcgis.com/3.18compact/"></script>。在这种情况下,componentDidMount()的上述代码将简单地为this._createMap() - Tom Wayson
你能否帮助我在 https://dev59.com/L6r2oIgBc1ULPQZFKUZB 上点击标记位置时显示弹出窗口或信息窗口? - Deep Kakkar

-1

你不需要像导入ReactJS一样导入esri api。由于React文件最终会编译成js文件,因此您需要按原样编写esri部分,并混合ReactJS部分以处理dom节点,这是ReactJS的主要目的。

以下链接中的示例在此处。

define([  
   'react',  
   'esri/toolbars/draw',  
   'esri/geometry/geometryEngine',  
   'dojo/topic',  
   'dojo/on',  
   'helpers/NumFormatter'  
 ], function(  
   React,  
   Draw, geomEngine,  
   topic, on,  
   format  
 ) {  
  var fixed = format(3);  
  var DrawToolWidget = React.createClass({  
    getInitialState: function() {  
       return {  
         startPoint: null,  
         btnText: 'Draw Line',  
         distance: 0,  
         x: 0,  
         y: 0  
       };  
     },  
     componentDidMount: function() {  
      this.draw = new Draw(this.props.map);  
      this.handler = this.draw.on('draw-end', this.onDrawEnd);  
      this.subscriber = topic.subscribe(  
        'map-mouse-move', this.mapCoordsUpdate  
       );  
     },  
     componentWillUnMount: function() {  
       this.handler.remove();  
       this.subscriber.remove();  
     },  
     onDrawEnd: function(e) {  
       this.draw.deactivate();  
       this.setState({  
       startPoint: null,  
         btnText: 'Draw Line'  
       });  
    },  
    mapCoordsUpdate: function(data) {  
      this.setState(data);  
      // not sure I like this conditional check  
      if (this.state.startPoint) {  
        this.updateDistance(data);  
      }  
    },  
    updateDistance: function(endPoint) {  
      var distance = geomEngine.distance(this.state.startPoint, endPoint);  
      this.setState({ distance: distance });  
    },  
    drawLine: function() {  
      this.setState({ btnText: 'Drawing...' });  
      this.draw.activate(Draw.POLYLINE);  
      on.once(this.props.map, 'click', function(e) {  
         this.setState({ startPoint: e.mapPoint });  
         // soo hacky, but Draw.LINE interaction is odd to use  
        on.once(this.props.map, 'click', function() {  
          this.onDrawEnd();  
        }.bind(this));  
      }.bind(this))  
    },  
    render: function() {  
      return (  
        <div className='well'>  
          <button className='btn btn-primary' onClick={this.drawLine}>  
            {this.state.btnText}  
          </button>  
          <hr />  
          <p>  
             <label>Distance: {fixed(this.state.distance)}</label>  
          </p>  
        </div>  
       );  
     }  
  });  
  return DrawToolWidget;  
});  

以下是您可以找到详细信息的链接。

http://odoe.net/blog/esrijs-reactjs/

https://geonet.esri.com/people/odoe/blog/2015/04/01/esrijs-with-reactjs-updated


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接