fix: 修正地图中心计算逻辑,修正用户定位

This commit is contained in:
yarnom 2025-07-07 18:29:04 +08:00
parent 983efb2d6b
commit d8456d165b
4 changed files with 78 additions and 26 deletions

View File

@ -277,11 +277,11 @@ var DeviceMarkers = (function() {
vectorSource.addFeature(myLocationFeature);
map.getView().animate({
center: coordinates,
zoom: 15,
duration: 1000
});
// map.getView().animate({
// center: coordinates,
// zoom: 15,
// duration: 1000
// });
},
function(error) {
console.error('获取位置失败:', error);
@ -290,6 +290,50 @@ var DeviceMarkers = (function() {
}
}
function locateMyLocation() {
if (myLocationFeature) {
var geometry = myLocationFeature.getGeometry();
var coordinates = geometry.getCoordinates();
map.getView().animate({
center: coordinates,
zoom: 15,
duration: 1000
});
return true;
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var currentMapType = getCurrentMapType();
var coordinates = CoordinateUtils.getMapCoordinates(lat, lon, currentMapType);
if (myLocationFeature) {
vectorSource.removeFeature(myLocationFeature);
}
myLocationFeature = new ol.Feature({
geometry: new ol.geom.Point(coordinates),
isMyLocation: true
});
vectorSource.addFeature(myLocationFeature);
map.getView().animate({
center: coordinates,
zoom: 15,
duration: 1000
});
},
);
}
}
return false;
}
function startLocationUpdates() {
if (navigator.geolocation) {
@ -350,6 +394,7 @@ var DeviceMarkers = (function() {
findDeviceById: findDeviceById,
locateDevice: locateDevice,
getMyLocation: getMyLocation,
locateMyLocation: locateMyLocation,
startLocationUpdates: startLocationUpdates,
stopLocationUpdates: stopLocationUpdates,
updateMyLocationForMapType: updateMyLocationForMapType,

View File

@ -91,6 +91,10 @@ var DeviceOverview = (function() {
return MapCore.switchMapType(mapType);
}
function locateMyLocation() {
return DeviceMarkers.locateMyLocation();
}
return {
init: init,
@ -102,6 +106,7 @@ var DeviceOverview = (function() {
queryDevices: queryDevices,
locateDeviceOnMap: locateDeviceOnMap,
locateDeviceDirectly: locateDeviceDirectly,
locateMyLocation: locateMyLocation,
toggleMapFunctionsMenu: toggleMapFunctionsMenu,
toggleDeviceId: toggleDeviceId,
@ -135,3 +140,4 @@ window.showNextForecast = DeviceOverview.showNextForecast;
window.queryDevices = DeviceOverview.queryDevices;
window.locateDeviceOnMap = DeviceOverview.locateDeviceOnMap;
window.locateDeviceDirectly = DeviceOverview.locateDeviceDirectly;
window.locateMyLocation = DeviceOverview.locateMyLocation;

View File

@ -88,9 +88,8 @@ var MapCore = (function() {
if (deviceList && deviceList.length > 0) {
setCenterFromDevices(deviceList);
}
DeviceMarkers.addDeviceMarkers(deviceList);
}
DeviceMarkers.getMyLocation();
DeviceMarkers.startLocationUpdates();
@ -212,21 +211,13 @@ var MapCore = (function() {
function setCenterFromDevices(deviceList) {
if (!deviceList || deviceList.length === 0) return;
var minLat = deviceList[0].latitude;
var maxLat = deviceList[0].latitude;
var minLon = deviceList[0].longitude;
var maxLon = deviceList[0].longitude;
for (var i = 1; i < deviceList.length; i++) {
var device = deviceList[i];
minLat = Math.min(minLat, device.latitude);
maxLat = Math.max(maxLat, device.latitude);
minLon = Math.min(minLon, device.longitude);
maxLon = Math.max(maxLon, device.longitude);
var sumLat = 0, sumLon = 0;
for (var i = 0; i < deviceList.length; i++) {
sumLat += deviceList[i].latitude;
sumLon += deviceList[i].longitude;
}
var centerLat = (minLat + maxLat) / 2;
var centerLon = (minLon + maxLon) / 2;
var centerLat = sumLat / deviceList.length;
var centerLon = sumLon / deviceList.length;
map.getView().setCenter(ol.proj.fromLonLat([centerLon, centerLat]));
}
@ -237,15 +228,22 @@ var MapCore = (function() {
return;
}
// 保存当前视图状态
var currentCenter = map.getView().getCenter();
var currentZoom = map.getView().getZoom();
map.removeLayer(currentBaseLayer);
currentBaseLayer = MapLayers.getLayer(mapType);
map.getLayers().insertAt(0, currentBaseLayer);
DeviceMarkers.updateMyLocationForMapType(mapType);
// 重新获取设备列表并添加标记
var deviceList = window.deviceList || [];
DeviceMarkers.addDeviceMarkers(deviceList);
// 恢复之前的视图状态
map.getView().setCenter(currentCenter);
map.getView().setZoom(currentZoom);
}
function toggleDeviceId() {

View File

@ -935,13 +935,16 @@
</div>
<div class="dropdown-divider"></div>
<div class="dropdown-group">
<div class="dropdown-group-title">测量工具</div>
<div class="dropdown-group-title">定位工具</div>
<div class="dropdown-item" onclick="toggleMeasureDistance()">
测距
</div>
<div class="dropdown-item" onclick="clearMeasure()">
清除测距
</div>
<div class="dropdown-item" onclick="locateMyLocation()">
当前位置
</div>
</div>
<div class="dropdown-divider"></div>
<div class="dropdown-group" th:if="${role=='SUPER_ADMIN'}">