【vue3+ArcGIS5】web开发中的地图功能从入门到实战四:更改地图的图层样式

在上一篇文章的基础上,实现更改地图的样式,这里做一个下拉选择来切换从官网加载的各种样式,换肤功能

实现效果

实现步骤

  1. 安装一个下拉选择组件
json 复制代码
"vue3-select-component": "^0.16.2"
  1. 注册为全局组件
js 复制代码
//main.js
import VueSelect from "vue3-select-component";
import "vue3-select-component/styles";
...
app.component('VSelect', VueSelect)
...
  1. 在地图组件中加载样式资源
js 复制代码
// 更改地图样式
    async function changeMapStyle() {
        const basemapStylesEndpoint =
      "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self"; 
      const result = await esriRequest(basemapStylesEndpoint, {
        responseType: "json",
      })

      const json = result.data;
      console.log("🚀 ~ changeMapStyle ~ result:", result)
      json.styles.forEach((style) => {
        // if the style is complete and not deprecated, add it to the select item
        if (style.complete && !style.deprecated) {
          options.value.push({
            label: style.name,
            value: style.path,
          })
        }
  });
  1. 在地图初始化完成后加载这个下拉组件
js 复制代码
onMounted(() => {
    initMap()
    changeMapStyle()
})
  1. 组件布局
html 复制代码
<template>
    <h2>ArcGIS Maps SDK for JavaScript Tutorials: Display a map666</h2>
    <div id="container">
        <div ref="mapDiv" class="map-div">
        </div>
        <div class="select">
            <v-select
                :placeholder="placeholder"
                :options="options"
                v-model="selectedValue"
            >
            </v-select>
        </div>
    </div>
</template>
  1. 定位在地图的右上角
css 复制代码
<style>
   #container {
      height: 100%;
      margin: 0;
      position: relative;
    }
    .map-div {
        height: 100%;
    }
    .select{
        position: absolute;
        top: 10px;
        right: 10px;
        z-index: 999;
        width: 400px;
        height: 400px;
        z-index:33;
    }
</style>
  1. 监听下拉组件的切换事件,从新加载整个地图
js 复制代码
watch(selectedValue, (newValue) => {
  if (newValue) {
    baseMap.value = newValue;
    initMap();
  }
})

完整实现代码

js 复制代码
<script setup>
    import Map from "@arcgis/core/Map";
    import MapView from '@arcgis/core/views/MapView'
    import { onMounted, ref, watch } from "vue";
    import esriRequest from "@arcgis/core/request";
    const mapDiv = ref(null)
    const baseMap = ref('arcgis/topographic')
    let view;
    const initMap = () => {
        const map = new Map({
            basemap: baseMap.value,
        });

        view = new MapView({
            center: [-118.805, 34.020],
            zoom: 13,
            container: mapDiv.value,
            map: map
        });


        view.on('click', (event) => {
            console.log('点击事件:', event);
        });


        view.when(
            () => {
            console.log('地图加载成功')
            },
            (error) => {
            console.error('地图加载失败:', error)
            }
        )
    }

    const placeholder = '请选择一个选项'
    const selectedValue = ref('')

    const options = ref([])

    
    
    onMounted(() => {
        initMap()
        changeMapStyle()
    })


    // 更改地图样式
    async function changeMapStyle() {
        const basemapStylesEndpoint =
      "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self"; 
      const result = await esriRequest(basemapStylesEndpoint, {
        responseType: "json",
      })

      const json = result.data;
      console.log("🚀 ~ changeMapStyle ~ result:", result)
      json.styles.forEach((style) => {

        // if the style is complete and not deprecated, add it to the combobox
        if (style.complete && !style.deprecated) {
          options.value.push({
            label: style.name,
            value: style.path,
          })
        }
      });

      watch(selectedValue, (newValue) => {
        if (newValue) {
          baseMap.value = newValue;
          initMap();
        }
      })
    }
</script>

<template>
    <h2>ArcGIS Maps SDK for JavaScript Tutorials: Display a map666</h2>
    <div id="container">
        <div ref="mapDiv" class="map-div">
        </div>
        <div class="select">
            
            <v-select
                :placeholder="placeholder"
                :options="options"
                v-model="selectedValue"
            >
            </v-select>
        </div>
    </div>
</template>

<style>
   #container {
      height: 100%;
      margin: 0;
      position: relative;
    }
    .map-div {
        height: 100%;
    }
    .select{
        position: absolute;
        top: 10px;
        right: 10px;
        z-index: 999;
        width: 400px;
        height: 400px;
        z-index:33;
    }

</style>
相关推荐
钛态9 小时前
Vite 中的 CSS 工程化:从 CSS Modules 到 UnoCSS 的渐进式迁移
前端·vue·react·web
南城以南溫暖如初1471 天前
树洞交友系统架构设计与匿名聊天实战指南
java·spring boot·mysql·系统架构·vue·mybatis·交友
_xaboy2 天前
低代码可视化表单设计器 FcDesigner 远程请求教程:GET POST 变量鉴权
低代码·开源·vue·表单·fcdesigner
宠友信息5 天前
IM系统开发技术路线分析,即时通讯源码助力快速搭建聊天应用
java·spring boot·redis·websocket·mysql·uni-app·vue
南城以南溫暖如初1476 天前
从传统健身房到无人值守:24小时自助健身系统的技术实现路径
java·spring boot·mysql·架构·vue
新中地GIS开发老师6 天前
WebGIS开发学生作品 | 基于无人机影像的烟草检测智能体与数字化管理平台
vue·web·mapbox·webgis
南城以南溫暖如初1476 天前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
aXin_ya7 天前
ElementUI (01):Vue2 项目引入 ElementUI使用
elementui·vue
常宇佳8 天前
vue3 @代指src路径设置
前端·typescript·vue
北城笑笑8 天前
Server 18 ,Nginx + Vite 前端部署排错实战:从 `/api/gpt/chat` 404 到 9012 后端服务的完整定位过程
linux·运维·前端·nginx·ubuntu·vue