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>
相关推荐
Front思1 分钟前
shopify前端开发
前端
风骏时光牛马4 分钟前
Julia常见问题汇总与代码示例
前端
ZC跨境爬虫9 分钟前
跟着 MDN 学JavaScript day_10:数组——数据的有序集合
android·java·开发语言·前端·javascript
广州华水科技11 分钟前
如何利用单北斗变形监测实现大坝安全监测?
前端
hxy060113 分钟前
Flutter showModalBottomSheet等弹窗宽度问题
前端·flutter
Wireless_wifi614 分钟前
IPQ9574 + WiFi 7: Building the Foundation for Scalable Edge AI Deployments
前端·人工智能·edge
晓131317 分钟前
【Cocos Creator 2.x】篇——第五章 游戏常用关键技术
前端·javascript·vue.js·游戏引擎
英俊潇洒美少年27 分钟前
前端全量资源预加载优化指南(React内置API + Vue实现 + prerender/prefetch深度对比)
前端·react.js·前端框架
道友可好35 分钟前
3 个人,100 万行代码,一行都没人写:OpenAI 的 Harness Engineering 实验
前端·人工智能·后端
winfredzhang1 小时前
用 Node.js + SQLite + 原生前端写一个本地情绪急救 Web App:情绪降落伞 Mood Parachute
前端·sqlite·node.js·express·情绪管理