在 React 中使用 ArcGIS JavaScript SDK 构建地图应用

3 篇文章 0 订阅
2 篇文章 0 订阅

创建React工程

$ npx install -g create-react-app
$ create-react-app my-react-arcgis-app
$ cd my-react-arcgis-app
$ npm start

安装ArcGIS库

$ npm install @arcgis/core

创建ArcGIS地图组件

import React, { useEffect, useRef } from 'react';
import Map from '@arcgis/core/Map';
import MapView from '@arcgis/core/views/MapView';
import FeatureLayer from '@arcgis/core/layers/FeatureLayer';

export default function MyMapViewer({}) {

    const mapRef = useRef();

    useEffect(() => {
        const map = new Map({
            basemap: 'streets-navigation-vector'
        });

        const view = new MapView({
            container: mapRef.current,
            map: map,
            center: [-79.940, 32.788],
            zoom: 16
        });

        const featureLayer = new FeatureLayer({
            url: 'https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/Charleston_Buildings_SLR_2080/FeatureServer/85',
            popupTemplate: {
                title: "{BuildingFID}",
                outFields: ["*"],
                content: "{BuildingFID}"
            },
        });

        map.add(featureLayer);

        return () => {
            if (view) {
                view.destroy()
            }
        };
    }, []);

    const mapStyle = {
        width: '100%',
        height: '100%',
        position: 'absolute',
        margin: 0,
        padding: 0,
    };

    return (
        <div className="map-container" ref={mapRef} style={mapStyle} />
    );
};

使用ArcGIS地图组件

修改App.js,内容如下:

import MyMapViewer from './MyMapViewer';
import './App.css';

function App() {

  return (
    <div className="app">
      <h1>My ArcGIS App</h1>
      <MyMapViewer />
    </div>
  );
}

export default App;

修改App.css,在文件夹最后添加ArcGIS的css

@import 'https://js.arcgis.com/4.29/@arcgis/core/assets/esri/themes/light/main.css';
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值