地图视图切换 #19

Merged
p5iufsk8w merged 2 commits from dmz_branch into main 6 months ago

@ -100,7 +100,12 @@ export default {
version: '2.0',
plugins: [
'AMap.Scale',
<<<<<<< HEAD
'AMap.ToolBar',
'AMap.ControlBar',
=======
'AMap.ToolBar',
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
'AMap.InfoWindow',
'AMap.Marker',
'AMap.Polyline',
@ -114,7 +119,11 @@ export default {
'AMap.TileLayer.Satellite',
'AMap.TileLayer.Traffic',
'AMap.TileLayer.RoadNet',
<<<<<<< HEAD
'AMap.Buildings',
=======
'AMap.BuildingLayer',
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
'AMap.DistrictLayer',
'AMap.Weather',
'AMap.Driving'
@ -127,8 +136,13 @@ export default {
mapInstance.value = new AMap.Map('sharedMap', {
zoom: 11,
center: [116.397428, 39.90923],
<<<<<<< HEAD
viewMode: viewMode.value,
pitch: viewMode.value === '3D' ? 45 : 0,
=======
viewMode: '3D',
pitch: 0,
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
rotation: 0,
mapStyle: 'amap://styles/normal',
features: ['bg', 'road', 'building', 'point'],
@ -158,9 +172,26 @@ export default {
right: '10px'
}
})
<<<<<<< HEAD
// 3D3D
const controlBar = new AMap.ControlBar({
position: {
top: '80px',
right: '10px'
},
showZoomBar: false,
showControlButton: true
})
mapInstance.value.addControl(toolbar)
mapInstance.value.addControl(scale)
mapInstance.value.addControl(controlBar)
=======
mapInstance.value.addControl(toolbar)
mapInstance.value.addControl(scale)
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
//
mapInstance.value.on('click', (e) => {
@ -186,7 +217,11 @@ export default {
});
//
<<<<<<< HEAD
layerInstances.value.buildings = new window.AMap.Buildings({
=======
layerInstances.value.buildings = new window.AMap.BuildingLayer({
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
zIndex: 11,
heightFactor: viewMode.value === '3D' ? 1 : 0.6
});
@ -321,6 +356,58 @@ export default {
const changeMapStyle = (style) => {
if (!mapInstance.value) return
<<<<<<< HEAD
try {
currentMapStyle.value = style
switch (style) {
case 'satellite':
// 使
if (!layerInstances.value.satellite) {
layerInstances.value.satellite = new window.AMap.TileLayer.Satellite({
zIndex: 9
})
}
//
mapInstance.value.add(layerInstances.value.satellite)
//
updateOverlayLayers()
break
case 'dark':
//
if (layerInstances.value.satellite) {
mapInstance.value.remove(layerInstances.value.satellite)
}
//
mapInstance.value.setMapStyle('amap://styles/dark')
//
updateOverlayLayers()
break
default:
//
if (layerInstances.value.satellite) {
mapInstance.value.remove(layerInstances.value.satellite)
}
//
mapInstance.value.setMapStyle('amap://styles/normal')
//
updateOverlayLayers()
}
console.log(`已切换到${style}地图样式`)
} catch (error) {
console.error('切换地图样式失败:', error)
ElMessage.error(`切换地图样式失败: ${error.message}`)
}
}
=======
switch (style) {
case 'satellite':
// 使
@ -367,11 +454,25 @@ export default {
return layers
}
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
//
const changeViewMode = (mode) => {
if (!mapInstance.value) return
try {
<<<<<<< HEAD
// - 使API
if (mode === '3D') {
// 3D
mapInstance.value.setViewMode('3D');
mapInstance.value.setPitch(45); // (3D)
console.log('已切换到3D视图模式');
} else {
// 2D
mapInstance.value.setViewMode('2D');
mapInstance.value.setPitch(0); //
console.log('已切换到2D视图模式');
=======
//
mapInstance.value.setViewMode(mode)
@ -385,6 +486,7 @@ export default {
}, 100)
} else {
mapInstance.value.setPitch(0) //
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
}
//
@ -392,7 +494,11 @@ export default {
if (layerInstances.value.buildings) {
mapInstance.value.remove(layerInstances.value.buildings)
}
<<<<<<< HEAD
layerInstances.value.buildings = new window.AMap.Buildings({
=======
layerInstances.value.buildings = new window.AMap.BuildingLayer({
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
zIndex: 10,
// 3D
heightFactor: mode === '3D' ? 1 : 0.6
@ -404,8 +510,11 @@ export default {
if (currentMapStyle.value === 'satellite') {
changeMapStyle('satellite')
}
<<<<<<< HEAD
=======
console.log(`已切换到${mode}视图模式`)
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
} catch (error) {
console.error('切换视图模式失败:', error)
ElMessage.error(`切换到${mode}视图失败: ${error.message}`)
@ -416,6 +525,198 @@ export default {
const updateOverlayLayers = () => {
if (!mapInstance.value || !window.AMap) return
<<<<<<< HEAD
try {
console.log('更新图层状态:', overlayLayers.value)
//
if (overlayLayers.value.includes('traffic')) {
if (!layerInstances.value.traffic) {
layerInstances.value.traffic = new window.AMap.TileLayer.Traffic({
zIndex: 10,
opacity: 0.8
})
}
mapInstance.value.add(layerInstances.value.traffic)
console.log('已添加交通流量图层')
} else if (layerInstances.value.traffic) {
mapInstance.value.remove(layerInstances.value.traffic)
console.log('已移除交通流量图层')
}
//
if (overlayLayers.value.includes('buildings')) {
if (!layerInstances.value.buildings) {
layerInstances.value.buildings = new window.AMap.Buildings({
zIndex: 11,
heightFactor: viewMode.value === '3D' ? 1 : 0.6
})
}
mapInstance.value.add(layerInstances.value.buildings)
console.log('已添加建筑物图层')
} else if (layerInstances.value.buildings) {
mapInstance.value.remove(layerInstances.value.buildings)
console.log('已移除建筑物图层')
}
//
if (overlayLayers.value.includes('terrain')) {
if (!layerInstances.value.terrain) {
// 线
layerInstances.value.terrain = new window.AMap.TileLayer({
zIndex: 8,
getTileUrl: function(x, y, z) {
return 'https://webst04.is.autonavi.com/appmaptile?style=6&x=' + x + '&y=' + y + '&z=' + z
}
})
}
mapInstance.value.add(layerInstances.value.terrain)
console.log('已添加地形图层')
} else if (layerInstances.value.terrain) {
mapInstance.value.remove(layerInstances.value.terrain)
console.log('已移除地形图层')
}
//
if (overlayLayers.value.includes('weather')) {
//
showWeatherInfo()
} else {
//
hideWeatherInfo()
}
//
if (overlayLayers.value.includes('threats')) {
//
console.log('威胁区域图层暂未实现')
}
} catch (error) {
console.error('更新图层失败:', error)
ElMessage.error(`更新图层失败: ${error.message}`)
}
}
//
const showWeatherInfo = () => {
if (!mapInstance.value || !window.AMap) return
try {
//
if (layerInstances.value.weatherMarkers && layerInstances.value.weatherMarkers.length > 0) {
layerInstances.value.weatherMarkers.forEach(marker => {
mapInstance.value.remove(marker)
})
}
//
layerInstances.value.weatherMarkers = []
//
if (!layerInstances.value.weatherService) {
layerInstances.value.weatherService = new window.AMap.Weather()
}
//
const cities = [
{ name: '北京', position: [116.397428, 39.90923] },
{ name: '上海', position: [121.473667, 31.230525] },
{ name: '广州', position: [113.264385, 23.129112] },
{ name: '深圳', position: [114.057868, 22.543099] },
{ name: '成都', position: [104.065735, 30.659462] }
]
//
cities.forEach(city => {
layerInstances.value.weatherService.getLive(city.name, (err, data) => {
if (!err && data) {
//
const weatherInfo = `
<div style="padding: 8px; text-align: center;">
<h4 style="margin: 0 0 5px 0;">${city.name}天气</h4>
<p style="margin: 3px 0;">天气: ${data.weather}</p>
<p style="margin: 3px 0;">温度: ${data.temperature}°C</p>
<p style="margin: 3px 0;">风向: ${data.windDirection}</p>
<p style="margin: 3px 0;">风力: ${data.windPower}</p>
<p style="margin: 3px 0;">湿度: ${data.humidity}%</p>
</div>
`
//
const weatherIcon = getWeatherIcon(data.weather)
//
const marker = new window.AMap.Marker({
position: city.position,
content: `<div class="weather-marker">${weatherIcon}</div>`,
offset: new window.AMap.Pixel(-15, -15),
zIndex: 120,
title: `${city.name}天气: ${data.weather}, ${data.temperature}°C`
})
//
const infoWindow = new window.AMap.InfoWindow({
content: weatherInfo,
offset: new window.AMap.Pixel(0, -30)
})
//
marker.on('click', () => {
infoWindow.open(mapInstance.value, city.position)
})
//
mapInstance.value.add(marker)
//
layerInstances.value.weatherMarkers.push(marker)
}
})
})
console.log('已添加天气信息图层')
} catch (error) {
console.error('天气图层添加失败:', error)
}
}
//
const hideWeatherInfo = () => {
if (!mapInstance.value) return
//
if (layerInstances.value.weatherMarkers && layerInstances.value.weatherMarkers.length > 0) {
layerInstances.value.weatherMarkers.forEach(marker => {
mapInstance.value.remove(marker)
})
layerInstances.value.weatherMarkers = []
console.log('已移除天气信息图层')
}
}
//
const getWeatherIcon = (weather) => {
//
const icons = {
'晴': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#FFB800"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>',
'多云': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#82B1FF"><path d="M19 18H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 7.69 9.5 6 12 6c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>',
'阴': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#78909C"><path d="M19 18H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 7.69 9.5 6 12 6c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>',
'雨': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#4FC3F7"><path d="M19 16H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 5.69 9.5 4 12 4c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/><path d="M8 19v3M12 19v3M16 19v3" stroke="#4FC3F7" stroke-width="2"/></svg>',
'雪': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#E1F5FE"><path d="M19 16H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 5.69 9.5 4 12 4c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/><circle cx="8" cy="20" r="1"/><circle cx="12" cy="20" r="1"/><circle cx="16" cy="20" r="1"/></svg>'
}
//
for (const key in icons) {
if (weather.includes(key)) {
return icons[key]
}
}
//
return '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#64B5F6"><circle cx="12" cy="12" r="10" fill-opacity="0.7"/><text x="12" y="16" text-anchor="middle" fill="white" font-size="10">天气</text></svg>'
}
=======
//
if (overlayLayers.value.includes('traffic')) {
if (!layerInstances.value.traffic) {
@ -510,6 +811,7 @@ export default {
}
}
>>>>>>> 999104f0d0174d3a3870682fb7163d95bc623970
//
provide('mapInstance', mapInstance)

@ -552,6 +552,10 @@ export default {
currentSatelliteLayer.value = satelliteLayer
console.log('卫星图层添加成功')
//
updateOverlayLayers()
ElMessage.success('已切换到卫星地图')
return
}
@ -577,6 +581,10 @@ export default {
currentSatelliteLayer.value = satelliteLayer
console.log('备用卫星图层加载成功')
//
updateOverlayLayers()
ElMessage.success('已切换到卫星地图')
} catch (tileError) {
console.warn('备用卫星图层加载失败:', tileError)
@ -610,6 +618,9 @@ export default {
}
})
//
updateOverlayLayers()
const styleNames = {
normal: '标准',
dark: '暗色'
@ -629,6 +640,9 @@ export default {
currentSatelliteLayer.value = null
}
map.value.setMapStyle('amap://styles/normal')
// 退
updateOverlayLayers()
} catch (fallbackError) {
console.warn('回退样式也失败:', fallbackError)
}
@ -650,6 +664,9 @@ export default {
map.value.setRotation(0)
}
//
updateOverlayLayers()
ElMessage.success(`已切换到${viewMode.value}视图模式`)
} catch (error) {
console.warn('视图模式切换失败:', error)
@ -710,13 +727,36 @@ export default {
clearThreatZones()
}
//
if (overlayLayers.value.includes('terrain')) {
if (!layerInstances.value.terrain) {
layerInstances.value.terrain = new AMap.TileLayer({
zIndex: 8,
getTileUrl: function(x, y, z) {
return 'https://webst04.is.autonavi.com/appmaptile?style=6&x=' + x + '&y=' + y + '&z=' + z
}
})
console.log('创建地形图层')
}
map.value.add(layerInstances.value.terrain)
console.log('添加地形图层')
} else if (layerInstances.value.terrain) {
map.value.remove(layerInstances.value.terrain)
console.log('移除地形图层')
}
//
if (overlayLayers.value.includes('weather')) {
showWeatherInfo()
} else {
hideWeatherInfo()
}
ElMessage.success('图层已更新')
} catch (error) {
console.error('图层更新失败:', error)
ElMessage.warning('部分图层更新失败')
}
ElMessage.success('图层已更新')
}
const loadThreatZones = () => {
@ -828,6 +868,125 @@ export default {
ElMessage.success('已清除所有目标点')
}
//
const showWeatherInfo = () => {
if (!map.value || !window.AMap) return
try {
//
if (layerInstances.value.weatherMarkers && layerInstances.value.weatherMarkers.length > 0) {
layerInstances.value.weatherMarkers.forEach(marker => {
map.value.remove(marker)
})
}
//
layerInstances.value.weatherMarkers = []
//
if (!layerInstances.value.weatherService) {
layerInstances.value.weatherService = new window.AMap.Weather()
}
//
const cities = [
{ name: '北京', position: [116.397428, 39.90923] },
{ name: '上海', position: [121.473667, 31.230525] },
{ name: '广州', position: [113.264385, 23.129112] },
{ name: '深圳', position: [114.057868, 22.543099] },
{ name: '成都', position: [104.065735, 30.659462] }
]
//
cities.forEach(city => {
layerInstances.value.weatherService.getLive(city.name, (err, data) => {
if (!err && data) {
//
const weatherInfo = `
<div style="padding: 8px; text-align: center;">
<h4 style="margin: 0 0 5px 0;">${city.name}天气</h4>
<p style="margin: 3px 0;">天气: ${data.weather}</p>
<p style="margin: 3px 0;">温度: ${data.temperature}°C</p>
<p style="margin: 3px 0;">风向: ${data.windDirection}</p>
<p style="margin: 3px 0;">风力: ${data.windPower}</p>
<p style="margin: 3px 0;">湿度: ${data.humidity}%</p>
</div>
`
//
const weatherIcon = getWeatherIcon(data.weather)
//
const marker = new window.AMap.Marker({
position: city.position,
content: `<div class="weather-marker">${weatherIcon}</div>`,
offset: new window.AMap.Pixel(-15, -15),
zIndex: 120,
title: `${city.name}天气: ${data.weather}, ${data.temperature}°C`
})
//
const infoWindow = new window.AMap.InfoWindow({
content: weatherInfo,
offset: new window.AMap.Pixel(0, -30)
})
//
marker.on('click', () => {
infoWindow.open(map.value, city.position)
})
//
map.value.add(marker)
//
layerInstances.value.weatherMarkers.push(marker)
}
})
})
console.log('已添加天气信息图层')
} catch (error) {
console.error('天气图层添加失败:', error)
}
}
//
const hideWeatherInfo = () => {
if (!map.value) return
//
if (layerInstances.value.weatherMarkers && layerInstances.value.weatherMarkers.length > 0) {
layerInstances.value.weatherMarkers.forEach(marker => {
map.value.remove(marker)
})
layerInstances.value.weatherMarkers = []
console.log('已移除天气信息图层')
}
}
//
const getWeatherIcon = (weather) => {
//
const icons = {
'晴': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#FFB800"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>',
'多云': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#82B1FF"><path d="M19 18H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 7.69 9.5 6 12 6c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>',
'阴': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#78909C"><path d="M19 18H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 7.69 9.5 6 12 6c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>',
'雨': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#4FC3F7"><path d="M19 16H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 5.69 9.5 4 12 4c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/><path d="M8 19v3M12 19v3M16 19v3" stroke="#4FC3F7" stroke-width="2"/></svg>',
'雪': '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#E1F5FE"><path d="M19 16H6a4 4 0 0 1-4-4c0-2.21 1.79-4 4-4h.71C7.37 5.69 9.5 4 12 4c3.04 0 5.5 2.46 5.5 5.5v.5H19a3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/><circle cx="8" cy="20" r="1"/><circle cx="12" cy="20" r="1"/><circle cx="16" cy="20" r="1"/></svg>'
}
//
for (const key in icons) {
if (weather.includes(key)) {
return icons[key]
}
}
//
return '<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 24 24" fill="#64B5F6"><circle cx="12" cy="12" r="10" fill-opacity="0.7"/><text x="12" y="16" text-anchor="middle" fill="white" font-size="10">天气</text></svg>'
}
onMounted(() => {
initMap()
refreshDroneList()

Loading…
Cancel
Save