React Native 集成 ArcGIS 地图

ArcGIS官方提供了 JavaScript SDK,也提供了 ArcGIS-Runtime-SDK-iOS,但是并没有提供 React Native的版本,所以这里使用了 react-native-arcgis-mapview 库,这个库比较老,支持的 ArcGIS-Runtime-SDK-iOS 版本是100.4,但是在使用的时候发现,在使用pod install安装的时候总是会下载失败,所以后面手动将 ArcGIS-Runtime-SDK-iOS 的版本改为 100.14.1。

创建工程

  • 初始化工程(需要科学上网)
shell 复制代码
npx react-native init MyReactNativeApp
  • 安装依赖
shell 复制代码
cd MyReactNativeApp/ios
pod install

安装react-native-arcgis-mapview

  • 使用 npm 安装 react-native-arcgis-mapview 库
shell 复制代码
cd MyReactNativeApp
npm install react-native-arcgis-mapview
  • 修改 ios/Podfile 文件,在最后添加
shell 复制代码
pod 'RNArcGISMapView', :path => "../node_modules/react-native-arcgis-mapview/ios"
  • 编辑 node_modules/react-native-arcgis-mapview/ios/RNArcGISMapView.podspec,修改 dependency 版本为 100.14.1。
shell 复制代码
s.dependency 'ArcGIS-Runtime-SDK-iOS', '100.4'
=>
s.dependency 'ArcGIS-Runtime-SDK-iOS', '100.14.1'
  • 使用 pod install 安装 ArcGIS-Runtime-SDK-iOS
shell 复制代码
cd MyReactNativeApp/ios
pod install

使用 react-native-arcgis-mapview

  • 在 App.js 中使用 react-native-arcgis-mapview 库,完整代码如下
JavaScript 复制代码
import React, {useRef, useState}from 'react'
import {View, Text, Button, StyleSheet } from 'react-native'
import ArcGISMapView, { setLicenseKey } from 'react-native-arcgis-mapview'

function App() {

  const key = '<key>'
  
  setLicenseKey(key)

  const mapView = useRef(null)

  const basemap = 'https://www.arcgis.com/home/item.html?id=6b6b9cea06964cb38d8a654964c347ab'

  return (
    <View style={styles.container}>
      <ArcGISMapView
          style={styles.map} 
          initialMapCenter={[{latitude: 32.788, longitude: -79.940, scale: 10000.0}]}
          basemapUrl={basemap}
          ref={mapView}
      />
      <Button title="Test" onPress={() => {
          console.log(mapView.current.props.basemapUrl)
      }} />
    </View>
  );
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  map: {
    flex: 1,
  },
})

export default App

验证

  • 运行项目,验证是否成功。
shell 复制代码
npm start
相关推荐
非科班Java出身GISer18 小时前
ArcGIS Maps SDK for JavaScript 5.0 组件(Component)地图初始化示例
arcgis·arcgis js 组件化·arcgis js5.0初始化·arcgis js组件式·arcgis js组件初始化·component初始化·arcgis js 5.0
啦啦球晃晃19 小时前
ArcGIS统计乡镇耕地面积
arcgis
研究点啥好呢1 天前
Ruflo v3.6:多智能体编排平台的全面突破
arcgis
沐言人生2 天前
ReactNative 源码分析5——ReactActivity之启动RN应用
android·react native
DeepFlow 零侵扰全栈可观测2 天前
运动战:AI 时代 IT 运维的决胜之道——DeepFlow 业务全链路可观测性的落地实践
运维·网络·人工智能·arcgis·云计算
沐言人生3 天前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
沐言人生4 天前
ReactNative 源码分析3——ReactActivity之初始化RN应用
android·react native
一个扣子4 天前
Hermes 未来路线图:2025 年起的新特性与 React Native New Architecture 协同
react native·未来发展·路线图·hermes·字节码diffing·性能增强
沐言人生4 天前
React Native 源码分析1——HybridData 机制深度分析
android·react native
空中海4 天前
01 React Native 基础、核心组件与布局体系
javascript·react native·react.js