张含笑
2025-10-13 3d08674227b35c1dee6dbd26a3e011bbfaf1dddf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!-- 地图 -->
<template>
  <!-- 地图容器 -->
  <view class="page-container theme-dark">
    <view id="map" class="map" :prop="setSelectMapLayerKey" :location="location" :change:prop="leaflet.initLayer"
      :change:location="leaflet.setView"></view>
 
    <view class="layer-btn" @click="show = true">
      <up-icon customPrefix="xyicon" name="tuceng" size="24" color="#000"></up-icon>
    </view>
 
    <view class="location-btn" @click="setMapLocation">
      <up-icon customPrefix="xyicon" name="dingwei" size="24" color="#000"></up-icon>
    </view>
 
    <u-input placeholder="搜索" border="surround" v-model="value" @change="change" color="#fff">
      <template #prefix>
        <view class="search-left-box">
          <u-icon color="#fff" name="arrow-left"></u-icon> 地址
        </view>
      </template>
 
      <template #suffix>
        <view class="search-right-box">
          <u-icon color="#fff" name="scan"></u-icon>
        </view>
      </template>
    </u-input>
 
    <up-popup v-model:show="show">
      <view class="popup-container">
        <view class="header">
          <view class="title">图层</view>
          <view class="close" @click="show = false">
            <u-icon color="#000" name="close"></u-icon>
          </view>
        </view>
        <view class="content">
          <view class="layer-box" :class="{on: setSelectMapLayerKey === item.key}" v-for="(item, ind) in layers"
            :key="ind" @click="setMapLayer(item)">
            <view class="image">
              <up-image :src="item.src" width="100%" height="100%"></up-image>
            </view>
            <view class="label">{{item.name}}</view>
          </view>
        </view>
      </view>
    </up-popup>
  </view>
</template>
 
<script setup>
  import {
    useMapStore
  } from "@/store/index.js"
 
  const mapStore = useMapStore()
 
  const layers = [{
      src: "/static/images/sl.png",
      name: '标准地图',
      key: 1,
    },
    {
      src: "/static/images/yx.png",
      name: '卫星地图',
      key: 2,
    }
  ]
 
  const show = ref(false)
 
  const value = ref('')
 
  const change = () => {
 
  }
 
  const setSelectMapLayerKey = computed(() => mapStore.getSelectMapLayerKey)
  const setMapLayer = (item) => {
    mapStore.setSelectMapLayerKey(item.key)
  }
 
  const location = ref('')
  const setMapLocation = () => {
    // 获取定位信息
    uni.getLocation({
      type: 'wgs84', // 返回的经纬度是 WGS84 坐标系(适合地图定位)
      isHighAccuracy: true,
      success: function(res) {
        location.value = res
      },
      fail: function(err) {
        console.log('定位失败:', err);
      }
    });
 
  }
 
 
  onShow(() => {
    // #ifdef APP-PLUS
 
    // plus.screen.lockOrientation("landscape-primary");
    // #endif
  });
 
  onHide(() => {
    // #ifdef APP-PLUS
    // plus.screen.lockOrientation("portrait-primary");
    // #endif
  });
</script>
 
<script module="leaflet" lang="renderjs">
  import L from 'leaflet'
 
  var basemapLayer0 = L.tileLayer(
    'http://t1.tianditu.com/vec_c/wmts?layer=vec&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=e110584a27d506da2740edca951683f4', {
      maxZoom: 18,
      minZoom: 1,
      tileSize: 256,
      zoomOffset: 1
    });
  var basemapLayer1 = L.tileLayer(
    'http://t1.tianditu.com/cva_c/wmts?layer=cva&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=e110584a27d506da2740edca951683f4', {
      maxZoom: 18,
      minZoom: 1,
      tileSize: 256,
      zoomOffset: 1
    });
  var basemapLayer2 = L.tileLayer(
    'http://t1.tianditu.com/img_c/wmts?layer=img&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=e110584a27d506da2740edca951683f4', {
      maxZoom: 18,
      minZoom: 1,
      tileSize: 256,
      zoomOffset: 1
    });
  var basemapLayer3 = L.tileLayer(
    'http://t1.tianditu.com/cia_c/wmts?layer=cia&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=e110584a27d506da2740edca951683f4', {
      maxZoom: 18,
      minZoom: 1,
      tileSize: 256,
      zoomOffset: 1
    });
  var basemap0 = L.layerGroup([basemapLayer0, basemapLayer1]);
  var basemap1 = L.layerGroup([basemapLayer2, basemapLayer3]);
 
  let map = null
 
  const layers = [{
      src: "/static/images/sl.png",
      name: '标准地图',
      key: 1,
      map: basemap0
    },
    {
      src: "/static/images/yx.png",
      name: '卫星地图',
      key: 2,
      map: basemap1
    }
  ]
 
  export default {
    data() {
      return {
        setSelectMapLayerKey: 1
      }
    },
 
    computed: {
 
    },
 
    mounted() {
      this.initMap()
    },
 
    methods: {
      initMap() {
        if (map) return
 
        map = L.map('map', {
          preferCanvas: true,
          crs: L.CRS.EPSG4326,
          zoomControl: false,
          attributionControl: false,
          doubleClickZoom: false,
          editable: true //绘制控件
        }).setView([25.992338, 114.823254], 13);
      },
 
      initLayer(value) {
 
        this.initMap()
 
        this.$nextTick(() => {
          map.removeLayer(layers.find(i => i.key === this.setSelectMapLayerKey).map);
 
          map.addLayer(layers.find(i => i.key === value).map);
 
          this.setSelectMapLayerKey = value
        })
      },
 
      setView(res) {
        if (!res) return
 
        this.initMap()
 
        this.$nextTick(() => {
          map.setView([res.latitude, res.longitude], 16, {
            animate: false // 使用动画过渡
          });
        })
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .page-container {
    position: relative;
    width: 100%;
    height: 100%;
 
    .u-input {
      position: absolute;
      left: 16rpx;
      bottom: 16rpx;
      width: calc(100% - 32rpx);
      height: 64rpx;
      z-index: 999;
      box-sizing: border-box;
      background: rgba(0, 0, 0, .4);
 
      .search-left-box {
        display: flex;
        align-items: center;
        color: $u-main-color;
      }
 
      .search-right-box {
        color: $u-main-color;
      }
    }
 
    .location-btn,
    .layer-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      right: 16rpx;
      width: 64rpx;
      height: 64rpx;
      background: #fff;
      z-index: 999;
      border-radius: 8rpx;
    }
 
    .layer-btn {
      bottom: 176rpx;
    }
 
    .location-btn {
      bottom: 96rpx;
    }
  }
 
  /* Leaflet 默认容器要有高度 */
  .map {
    width: 100%;
    height: 100%;
  }
 
  .popup-container {
    padding: 20rpx;
    background: #D3D3D3;
    border-radius: 32rpx 32rpx 0 0;
 
    .header {
      padding: 0 20rpx;
      display: flex;
      justify-content: space-between;
      height: 56rpx;
    }
 
    .content {
      padding: 20rpx;
      display: flex;
      background: #fff;
      border-radius: 32rpx;
 
      .layer-box {
        margin-left: 16rpx;
        width: calc(100% / 3);
 
        &:first-child {
          margin-left: 0;
        }
 
        &.on {
          .image {
            border: 1px solid #0055ff;
          }
 
          .label {
            color: #0055ff;
          }
        }
 
        .image {
          width: 100%;
          height: 96rpx;
          border-radius: 16rpx;
          overflow: hidden;
          box-sizing: border-box;
          border: 1px solid transparent;
        }
 
        .label {
          line-height: 48rpx;
          text-align: center;
          font-size: 16rpx;
        }
      }
    }
  }
</style>