Showing
1 changed file
with
50 additions
and
5 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-01-22 11:40:12 | 2 | * @Date: 2025-01-22 11:40:12 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-01-24 17:33:54 | 4 | + * @LastEditTime: 2025-02-08 10:05:50 |
| 5 | * @FilePath: /map-demo/src/views/mapCutter.vue | 5 | * @FilePath: /map-demo/src/views/mapCutter.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -37,12 +37,12 @@ | ... | @@ -37,12 +37,12 @@ |
| 37 | @blur="onLBRangeBlur" | 37 | @blur="onLBRangeBlur" |
| 38 | /> | 38 | /> |
| 39 | | 39 | |
| 40 | - <el-input | 40 | + <!-- <el-input |
| 41 | v-model="map_right_top_range" | 41 | v-model="map_right_top_range" |
| 42 | style="width: 240px" | 42 | style="width: 240px" |
| 43 | placeholder="输入右上角的经纬度" | 43 | placeholder="输入右上角的经纬度" |
| 44 | @blur="onRTRangeBlur" | 44 | @blur="onRTRangeBlur" |
| 45 | - /> | 45 | + /> --> |
| 46 | </div> | 46 | </div> |
| 47 | <div v-if="showUpload"> | 47 | <div v-if="showUpload"> |
| 48 | <input type="file" @change="handleImageUpload" /> | 48 | <input type="file" @change="handleImageUpload" /> |
| ... | @@ -170,11 +170,56 @@ function handleImageUpload(event) { | ... | @@ -170,11 +170,56 @@ function handleImageUpload(event) { |
| 170 | reader.readAsDataURL(file); | 170 | reader.readAsDataURL(file); |
| 171 | } | 171 | } |
| 172 | 172 | ||
| 173 | -function addImageToMap(url) { | 173 | +async function addImageToMap(url) { |
| 174 | if (imageLayer.value) { | 174 | if (imageLayer.value) { |
| 175 | map.value.remove(imageLayer.value); | 175 | map.value.remove(imageLayer.value); |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | + // 计算图片右上角经纬度 | ||
| 179 | + const img = new Image(); | ||
| 180 | + img.src = url; | ||
| 181 | + | ||
| 182 | + await new Promise((resolve) => { | ||
| 183 | + img.onload = resolve; | ||
| 184 | + }); | ||
| 185 | + | ||
| 186 | + const imgWidth = img.width; | ||
| 187 | + const imgHeight = img.height; | ||
| 188 | + const aspectRatio = imgWidth / imgHeight; // 图片宽高比 | ||
| 189 | + | ||
| 190 | + // 获取左下角经纬度 | ||
| 191 | + const [lng1, lat1] = map_left_bottom_range.value; | ||
| 192 | + | ||
| 193 | + // 计算地理坐标的范围(保持图片比例) | ||
| 194 | + const mapBounds = map.value.getBounds(); // 获取当前地图可视范围 | ||
| 195 | + const southWest = mapBounds.getSouthWest(); | ||
| 196 | + const northEast = mapBounds.getNorthEast(); | ||
| 197 | + | ||
| 198 | + // 计算地图当前可视区域的宽高 | ||
| 199 | + const mapLngWidth = Math.abs(northEast.lng - southWest.lng); | ||
| 200 | + const mapLatHeight = Math.abs(northEast.lat - southWest.lat); | ||
| 201 | + | ||
| 202 | + // 让地图范围的宽高比匹配图片的宽高比 | ||
| 203 | + let lat2, lng2; | ||
| 204 | + if (mapLngWidth / mapLatHeight > aspectRatio) { | ||
| 205 | + // 地图太宽了,需要基于高度调整宽度 | ||
| 206 | + const latHeight = mapLatHeight * 0.5; // 设定图片占地图的比例(可以调整) | ||
| 207 | + const lngWidth = latHeight * aspectRatio; | ||
| 208 | + | ||
| 209 | + lat2 = lat1 + latHeight; | ||
| 210 | + lng2 = lng1 + lngWidth; | ||
| 211 | + } else { | ||
| 212 | + // 地图太高了,需要基于宽度调整高度 | ||
| 213 | + const lngWidth = mapLngWidth * 0.5; // 设定图片占地图的比例(可以调整) | ||
| 214 | + const latHeight = lngWidth / aspectRatio; | ||
| 215 | + | ||
| 216 | + lat2 = lat1 + latHeight; | ||
| 217 | + lng2 = lng1 + lngWidth; | ||
| 218 | + } | ||
| 219 | + | ||
| 220 | + // 更新右上角坐标 | ||
| 221 | + map_right_top_range.value = [lng2, lat2]; | ||
| 222 | + | ||
| 178 | // TAG: 设置图片范围 | 223 | // TAG: 设置图片范围 |
| 179 | bounds.value = new AMap.Bounds(map_left_bottom_range.value, map_right_top_range.value); // 设置图片范围 左下角 (西南) -> 右上角 (东北) | 224 | bounds.value = new AMap.Bounds(map_left_bottom_range.value, map_right_top_range.value); // 设置图片范围 左下角 (西南) -> 右上角 (东北) |
| 180 | imageLayer.value = new AMap.ImageLayer({ | 225 | imageLayer.value = new AMap.ImageLayer({ |
| ... | @@ -269,7 +314,7 @@ const onRTRangeBlur = () => { | ... | @@ -269,7 +314,7 @@ const onRTRangeBlur = () => { |
| 269 | } | 314 | } |
| 270 | 315 | ||
| 271 | const showUpload = computed(() => { | 316 | const showUpload = computed(() => { |
| 272 | - return map_left_bottom_range.value && map_right_top_range.value ? true : false; | 317 | + return map_left_bottom_range.value ? true : false; |
| 273 | }); | 318 | }); |
| 274 | 319 | ||
| 275 | const onCenterBlur = () => { | 320 | const onCenterBlur = () => { | ... | ... |
-
Please register or login to post a comment