简体   繁体   English

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

[英]How can I use Esri Arcgis Map in ReactJs Project?

I'm trying to use Esri map. 我正在尝试使用Esri地图。 To include map in my project, here is what I found: 要在我的项目中包含地图,我发现这是:

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

But there isn't any esri folder or npm package. 但是没有任何esri文件夹或npm包。 Therefore, I'm confused here. 因此,我在这里很困惑。 How esri is imported in project? 如何在项目中导入esri?

An alternative method to the above is the one demonstrated in esri-react-router-example . 上面的另一种方法是在esri-react-router-example中演示的方法。 That application uses a library called esri-loader to lazy load the ArcGIS API only in components/routes where it is needed. 该应用程序使用名为esri-loader的库来仅在需要它的组件/路由中延迟加载ArcGIS API。 Example: 例:

First, install the esri-loader libary: 首先,安装esri-loader libary:

npm install esri-loader --save

Then import the esri-loader functions in any react module: 然后在任何react模块中导入esri-loader函数:

import * as esriLoader from 'esri-loader'

Then lazy load the ArcGIS API: 然后延迟加载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()
  }
},

Then load and the ArcGIS API's (Dojo) modules that are needed to create a map: 然后加载创建地图所​​需的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
      })
    })
  })
}

The benefit of using esri-loader over the approach shown above is that you don't have to use the Dojo loader and toolchain to load and build your entire application. 使用esri-loader而不是上面显示的方法的好处是,您不必使用Dojo加载程序和工具链来加载和构建整个应用程序。 You can use the React toolchain of your choice (webpack, etc). 您可以使用您选择的React工具链(webpack等)。

This blog post explains how this approach works and compares it to other (similar) approaches used in applications like esri-redux . 这篇博客文章解释了这种方法的工作原理,并将其与esri-redux等应用程序中使用的其他(类似)方法进行了比较。

Use esri-loader to load the required esri modules. 使用esri-loader加载所需的esri模块。 This is a component rendering basemap. 这是一个组件渲染底图。

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;

This renders a base map but this is not responsive. 这会呈现一个基本地图,但这不是响应。 If I remove the div around the view div or if I give the height and width of the outer div (surrounding viewDiv) as relative ({ height: '100%', width: '100%'}), the map does not render. 如果我删除视图div周围的div或者我将外部div(周围的viewDiv)的高度和宽度设置为relative({height:'100%',width:'100%'}),则地图不会渲染。 No idea why. 不知道为什么。 Any suggestions to make it responsive would be appreciated. 任何建议,使其响应将不胜感激。

You don't need to import esri api like you do for ReactJS. 您不需要像对ReactJS那样导入esri api。 As the react file will finally compile into a js file you need to write the esri parts as it is and mix the ReactJS part for handling the dom node, which is the main purpose of ReactJS. 由于react文件最终会编译成js文件,你需要按原样编写esri部分并混合使用ReactJS部分来处理dom节点,这是ReactJS的主要目的。

A sample from the links below is here 以下链接中的示例如下所示

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;  
});  

Below are the links where you can find information in detail. 以下是您可以在其中详细查找信息的链接。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换Esri ASCII(.asc)以在传单地图上使用? - How to convert Esri ASCII (.asc) to use on a leaflet map? NodeJS:@esri/arcgis-rest-request - 如何调整 Fetch 选项? - NodeJS: @esri/arcgis-rest-request - How to adjust Fetch options? 在 ReactJS 中,如何使用 map() 为每个项目创建下拉菜单? - In ReactJS how can I use map() to create dropdown menus for each item? 如何一起使用passportjs和reactjs? - How can I use passportjs and reactjs together? 如何在ReactJS中使用嵌套表单? - How can i use Nested forms in ReactJS? 如何在ReactJS项目中导入gl-matrix? - How can I import gl-matrix in a ReactJS project? 我总是对 mern 项目(nodejs 和 reactjs)使用“并发”,但现在我无法同时安装 - I always use "concurrently" for mern project (nodejs & reactjs) but now I can't able to install concurrently 如何将 ReactJs 与使用 EJS Express Mongodb 作为堆栈的现有项目一起使用? - How do I use ReactJs with an existing project that uses EJS Express Mongodb as stack? 作为经过身份验证的用户,我可以使用 ArcGIS REST-API“applyEdits”来更新要素图层吗? - Can I use ArcGIS REST-API "applyEdits" to update Feature-Layer as an authenticated user? 如何在Reactjs中进行映射和过滤 - How to Map and Filter in Reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM