Openlayers调用ArcGis地图服务之一 —— 地图切片(/tile)

1.1 Openlayers调用ArcGis地图服务之地图切片(/tile)

地图服务一般分为预先缓存的地图切片(瓦片地图)、动态地图、要素查询、要素查找、要素识别,下面使用ArcGis官方服务作为示例直接调用(如果使用自己的私有服务,可能先要获取token)

各个库版本如下:

javascript 复制代码
    "ol": "^10.8.0",
    "proj4": "^2.20.8",
    "vue3-openlayers": "^12.2.2"

目录

  • [1.1 地图切片【地图服务的切片接口(/tile)】](#1.1 地图切片【地图服务的切片接口(/tile)】)
    • [1.1.1 介绍](#1.1.1 介绍)
    • [1.1.2 判断](#1.1.2 判断)
    • [1.1.3 调用](#1.1.3 调用)
      • [1.1.3.1 在线预览](#1.1.3.1 在线预览)
      • [1.1.3.2 Openlayers调用](#1.1.3.2 Openlayers调用)
      • [1.1.3.3 Vue3-Openlayers调用](#1.1.3.3 Vue3-Openlayers调用)

1.1 地图切片【地图服务的切片接口(/tile)】

1.1.1 介绍

地图切片是指将一幅完整的地图,按照预先设定的比例尺层级(Zoom Levels)和固定的图片尺寸(如 256×256 像素),切割成若干行、列排列的图片文件(jpg,jpeg,png,webp等),并存储在服务器端。客户端在地图上移动或缩放时,仅加载当前视野范围内所需的瓦片图片进行拼接显示。

核心特征

  • 预生成:瓦片在服务发布前或首次访问时已生成好,无需实时渲染。

  • 静态:瓦片内容相对固定,不随每次请求而改变。

  • 高性能:客户端直接请求现成图片,服务器压力小,响应快,适合大规模并发访问。

  • 不实时:如果地图数据更新,需要重新生成瓦片才能体现变化。

  • 典型应用:互联网底图(如 Google 地图、高德地图、 的瓦片服务)。

1.1.2 判断

判断一个ArcGis地图服务是否可以使用切片形式调用,可以查看地图服务的基本信息,比如ArcGis官方服务1

注意 :图中的Single Fused Map Cache: trueTile Info两个字段就可以说明该地图服务已经被预先切分缓存

1.1.3 调用

1.1.3.1 在线预览

点击红框内的ArcGIS JavaScript,即可在线预览

可以看到在线预览使用切片调用

1.1.3.2 Openlayers调用

可以看到很多类似

https://sampleserver6.arcgisonline.com/arcgis/rest/services/MtBaldy_BaseMap/MapServer/tile/6/364/331这样的请求,每一个请求返回一个png图片

javascript 复制代码
<template>
  <div class="map-page">
    <h1>OpenLayers 原生地图</h1>
    <div id="ol-map" ref="mapContainer" class="map-container"></div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import TileGrid from "ol/tilegrid/TileGrid";
import { XYZ } from "ol/source";

const mapContainer = ref<HTMLDivElement>();
let map: Map | null = null;

// 从 MapServer 获取的 resolutions
const resolutions = [
  4233.341800016934, 2116.670900008467, 1058.3354500042335, 529.1677250021168,
  264.5838625010584, 132.2919312505292, 66.1459656252646, 33.0729828126323,
  16.53649140631615, 8.268245703158074, 4.134122851579037, 2.0670614257895186,
  1.0335307128947593,
];

// 创建 TileGrid
const tileGrid = new TileGrid({
  origin: [-5120900, 9998100],
  resolutions: resolutions,
  tileSize: [256, 256],
});

onMounted(() => {
  // 创建 ArcGIS MapServer 切片图层
  const arcgisLayer = new TileLayer({
    source: new XYZ({
      url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/MtBaldy_BaseMap/MapServer/tile/{z}/{y}/{x}",
      attributions: "ArcGIS",
      projection: "EPSG:32611",
      tileGrid: tileGrid,
    }),
  });

  // 创建地图
  map = new Map({
    target: mapContainer.value!,
    layers: [arcgisLayer],
    view: new View({
      projection: "EPSG:32611",
      center: [457000, 3796000], // UTM zone 11N 坐标 (大约 MtBaldy 区域)
      zoom: 5,
      resolutions: resolutions,
    }),
  });
});

onUnmounted(() => {
  if (map) {
    map.setTarget(undefined);
    map = null;
  }
});
</script>

<style scoped>
.map-page {
  padding: 20px;
}

h1 {
  margin-bottom: 20px;
  color: #333;
}

.map-container {
  width: 100%;
  height: 600px;
  border: 2px solid #ddd;
  border-radius: 8px;
}
</style>
1.1.3.3 Vue3-Openlayers调用
javascript 复制代码
<template>
  <div class="map-page">
    <h1>Vue3-OpenLayers 组件地图</h1>
    <ol-map
      :loadTilesWhileAnimating="true"
      :loadTilesWhileInteracting="true"
      class="map-container"
    >
      <ol-view
        ref="view"
        :center="center"
        :zoom="zoom"
        :projection="projection"
        :resolutions="resolutions"
      />

      <ol-tile-layer>
        <ol-source-xyz
          url="https://sampleserver6.arcgisonline.com/arcgis/rest/services/MtBaldy_BaseMap/MapServer/tile/{z}/{y}/{x}"
          attributions="ArcGIS"
          :projection="projection"
          :tileGrid="tileGrid"
        />
      </ol-tile-layer>
    </ol-map>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import TileGrid from "ol/tilegrid/TileGrid";

// 从 MapServer 获取的 resolutions
const resolutions = [
  4233.341800016934, 2116.670900008467, 1058.3354500042335, 529.1677250021168,
  264.5838625010584, 132.2919312505292, 66.1459656252646, 33.0729828126323,
  16.53649140631615, 8.268245703158074, 4.134122851579037, 2.0670614257895186,
  1.0335307128947593,
];

// 创建 TileGrid
const tileGrid = new TileGrid({
  origin: [-5120900, 9998100],
  resolutions: resolutions,
  tileSize: [256, 256],
});

const center = ref([457000, 3796000]); // UTM zone 11N 坐标 (大约 MtBaldy 区域)
const zoom = ref(5);
const projection = ref("EPSG:32611");
</script>

<style scoped>
.map-page {
  padding: 20px;
}

h1 {
  margin-bottom: 20px;
  color: #333;
}

.map-container {
  width: 100%;
  height: 600px;
  border: 2px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
}
</style>
相关推荐
tedcloud1231 天前
UI-TARS-desktop部署教程:构建AI桌面自动化系统
服务器·前端·人工智能·ui·自动化·github
UXbot1 天前
AI原型设计工具如何支持团队协作与快速迭代
前端·交互·个人开发·ai编程·原型模式
ZC跨境爬虫1 天前
跟着MDN学HTML_day_48:(Node接口)
前端·javascript·ui·html·音视频
PieroPc1 天前
CAMWATCH — 局域网摄像头监控系统 Fastapi + html
前端·python·html·fastapi·监控
巴巴博一1 天前
2026 最新:Trae / Cursor 一键接入 taste-skill 完整教程(让 AI 前端告别“AI 味”)
前端·ai·ai编程
kyriewen1 天前
半夜三点线上崩了,AI替我背了锅——用AI排错,五分钟定位三年老bug
前端·javascript·ai编程
kyriewen1 天前
我让 AI 当了 24 小时全年无休的“毒舌考官”
前端·ci/cd·ai编程
hexu_blog1 天前
vue+java实现图片批量压缩
java·前端·vue.js
IT_陈寒1 天前
为什么你应该学习JavaScript?
前端·人工智能·后端
lifejump1 天前
Empire(帝国)CMS 7.5 XSS注入
前端·安全·xss