使用 Vite 快速创建 React 项目 - SuperMap iClient JavaScript / Leaflet

SuperMap iClient JavaScript 是基于现代 Web 技术栈全新构建的统一 JavaScript 客户端,适用于 SuperMap 云 GIS 和在线 GIS 平台系列产品。接下来,让我们一起看看如何使用 Vite 快速创建一个 React 项目!

  • Node.js:v22.19.0
  • React:19.1.1
  • SuperMap iClient:supermapgis/iclient-leaflet

1、创建项目

bash 复制代码
# 查看 node 版本
node -v
# 创建项目,创建后根据提示启动项目
npm create vite@latest my-react-app -- --template react

2、加载地图

2.1、编写代码

  • App.js:具体加载地图代码
javascript 复制代码
// App.js
import { useEffect, useRef } from 'react';
import L from 'leaflet';
import '@supermapgis/iclient-leaflet';

function App() {
  const mapRef = useRef(null);
  const mapInstanceRef = useRef(null);

  useEffect(() => {
    if (!mapRef.current) return;

    if (mapInstanceRef.current) return;

    var url = "https://iserver.supermap.io/iserver/services/map-world/rest/maps/World";
    
    var map = L.map(mapRef.current, {
      crs: L.CRS.EPSG4326,
      center: [0, 0],
      maxZoom: 18,
      zoom: 1
    });

     L.supermap.tiledMapLayer(url).addTo(map);

    mapInstanceRef.current = map;

    return () => {
      if (mapInstanceRef.current) {
        mapInstanceRef.current.remove();
        mapInstanceRef.current = null;
      }
    };
  }, []);

  return (
    <div ref={mapRef} style={{margin: '0',height:'100%',position: 'absolute',width:'100%'}}></div>
  );
}

export default App;
  • index.html:引入 iclient-leaflet 相关 css 文件
html 复制代码
<!-- index.html -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css" />
<link rel="stylesheet" href="https://iclient.supermap.io/dist/leaflet/iclient-leaflet.min.css" />

2.2、引入 SuperMap iClient for Leaflet

bash 复制代码
# 进入项目目录
cd my-react-app
# 安装 supermap-iclient
npm install @supermapgis/iclient-leaflet
# 启动
npm run dev
相关推荐
用户6883362059702 小时前
Progressive Web App (PWA)
前端
沢田纲吉2 小时前
《LLVM IR 学习手记(二):变量表达式编译器的实现与深入解析》
前端·编程语言·llvm
小徐_23332 小时前
VitePress 博客变身 APP,支持离线访问,只需这一招。
前端·vitepress·pwa
猪哥帅过吴彦祖2 小时前
第 5 篇:WebGL 从 2D 到 3D - 坐标系、透视与相机
前端·javascript·webgl
折七2 小时前
expo sdk53+ 集成极光推送消息推送 ios swift
前端·javascript·ios
猪哥帅过吴彦祖2 小时前
Flutter 系列教程:布局基础 (上) - `Container`, `Row`, `Column`, `Flex`
前端·flutter·ios
lifejump2 小时前
DVWA | XSS 跨站脚本注入
前端·xss
gplitems1232 小时前
Tripfery - Travel & Tour Booking WordPress Theme Tested
前端
流星稍逝2 小时前
前端&后端解决跨域的方法
前端·后端