fix: 修正地图中心计算逻辑,修正用户定位
This commit is contained in:
parent
983efb2d6b
commit
d8456d165b
@ -277,11 +277,11 @@ var DeviceMarkers = (function() {
|
|||||||
|
|
||||||
vectorSource.addFeature(myLocationFeature);
|
vectorSource.addFeature(myLocationFeature);
|
||||||
|
|
||||||
map.getView().animate({
|
// map.getView().animate({
|
||||||
center: coordinates,
|
// center: coordinates,
|
||||||
zoom: 15,
|
// zoom: 15,
|
||||||
duration: 1000
|
// duration: 1000
|
||||||
});
|
// });
|
||||||
},
|
},
|
||||||
function(error) {
|
function(error) {
|
||||||
console.error('获取位置失败:', 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() {
|
function startLocationUpdates() {
|
||||||
if (navigator.geolocation) {
|
if (navigator.geolocation) {
|
||||||
@ -350,6 +394,7 @@ var DeviceMarkers = (function() {
|
|||||||
findDeviceById: findDeviceById,
|
findDeviceById: findDeviceById,
|
||||||
locateDevice: locateDevice,
|
locateDevice: locateDevice,
|
||||||
getMyLocation: getMyLocation,
|
getMyLocation: getMyLocation,
|
||||||
|
locateMyLocation: locateMyLocation,
|
||||||
startLocationUpdates: startLocationUpdates,
|
startLocationUpdates: startLocationUpdates,
|
||||||
stopLocationUpdates: stopLocationUpdates,
|
stopLocationUpdates: stopLocationUpdates,
|
||||||
updateMyLocationForMapType: updateMyLocationForMapType,
|
updateMyLocationForMapType: updateMyLocationForMapType,
|
||||||
|
|||||||
@ -91,6 +91,10 @@ var DeviceOverview = (function() {
|
|||||||
return MapCore.switchMapType(mapType);
|
return MapCore.switchMapType(mapType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function locateMyLocation() {
|
||||||
|
return DeviceMarkers.locateMyLocation();
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: init,
|
init: init,
|
||||||
|
|
||||||
@ -102,6 +106,7 @@ var DeviceOverview = (function() {
|
|||||||
queryDevices: queryDevices,
|
queryDevices: queryDevices,
|
||||||
locateDeviceOnMap: locateDeviceOnMap,
|
locateDeviceOnMap: locateDeviceOnMap,
|
||||||
locateDeviceDirectly: locateDeviceDirectly,
|
locateDeviceDirectly: locateDeviceDirectly,
|
||||||
|
locateMyLocation: locateMyLocation,
|
||||||
|
|
||||||
toggleMapFunctionsMenu: toggleMapFunctionsMenu,
|
toggleMapFunctionsMenu: toggleMapFunctionsMenu,
|
||||||
toggleDeviceId: toggleDeviceId,
|
toggleDeviceId: toggleDeviceId,
|
||||||
@ -135,3 +140,4 @@ window.showNextForecast = DeviceOverview.showNextForecast;
|
|||||||
window.queryDevices = DeviceOverview.queryDevices;
|
window.queryDevices = DeviceOverview.queryDevices;
|
||||||
window.locateDeviceOnMap = DeviceOverview.locateDeviceOnMap;
|
window.locateDeviceOnMap = DeviceOverview.locateDeviceOnMap;
|
||||||
window.locateDeviceDirectly = DeviceOverview.locateDeviceDirectly;
|
window.locateDeviceDirectly = DeviceOverview.locateDeviceDirectly;
|
||||||
|
window.locateMyLocation = DeviceOverview.locateMyLocation;
|
||||||
@ -88,9 +88,8 @@ var MapCore = (function() {
|
|||||||
|
|
||||||
if (deviceList && deviceList.length > 0) {
|
if (deviceList && deviceList.length > 0) {
|
||||||
setCenterFromDevices(deviceList);
|
setCenterFromDevices(deviceList);
|
||||||
}
|
|
||||||
|
|
||||||
DeviceMarkers.addDeviceMarkers(deviceList);
|
DeviceMarkers.addDeviceMarkers(deviceList);
|
||||||
|
}
|
||||||
|
|
||||||
DeviceMarkers.getMyLocation();
|
DeviceMarkers.getMyLocation();
|
||||||
DeviceMarkers.startLocationUpdates();
|
DeviceMarkers.startLocationUpdates();
|
||||||
@ -212,21 +211,13 @@ var MapCore = (function() {
|
|||||||
function setCenterFromDevices(deviceList) {
|
function setCenterFromDevices(deviceList) {
|
||||||
if (!deviceList || deviceList.length === 0) return;
|
if (!deviceList || deviceList.length === 0) return;
|
||||||
|
|
||||||
var minLat = deviceList[0].latitude;
|
var sumLat = 0, sumLon = 0;
|
||||||
var maxLat = deviceList[0].latitude;
|
for (var i = 0; i < deviceList.length; i++) {
|
||||||
var minLon = deviceList[0].longitude;
|
sumLat += deviceList[i].latitude;
|
||||||
var maxLon = deviceList[0].longitude;
|
sumLon += deviceList[i].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 centerLat = sumLat / deviceList.length;
|
||||||
var centerLat = (minLat + maxLat) / 2;
|
var centerLon = sumLon / deviceList.length;
|
||||||
var centerLon = (minLon + maxLon) / 2;
|
|
||||||
|
|
||||||
map.getView().setCenter(ol.proj.fromLonLat([centerLon, centerLat]));
|
map.getView().setCenter(ol.proj.fromLonLat([centerLon, centerLat]));
|
||||||
}
|
}
|
||||||
@ -237,15 +228,22 @@ var MapCore = (function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 保存当前视图状态
|
||||||
|
var currentCenter = map.getView().getCenter();
|
||||||
|
var currentZoom = map.getView().getZoom();
|
||||||
|
|
||||||
map.removeLayer(currentBaseLayer);
|
map.removeLayer(currentBaseLayer);
|
||||||
currentBaseLayer = MapLayers.getLayer(mapType);
|
currentBaseLayer = MapLayers.getLayer(mapType);
|
||||||
map.getLayers().insertAt(0, currentBaseLayer);
|
map.getLayers().insertAt(0, currentBaseLayer);
|
||||||
|
|
||||||
DeviceMarkers.updateMyLocationForMapType(mapType);
|
DeviceMarkers.updateMyLocationForMapType(mapType);
|
||||||
|
|
||||||
// 重新获取设备列表并添加标记
|
|
||||||
var deviceList = window.deviceList || [];
|
var deviceList = window.deviceList || [];
|
||||||
DeviceMarkers.addDeviceMarkers(deviceList);
|
DeviceMarkers.addDeviceMarkers(deviceList);
|
||||||
|
|
||||||
|
// 恢复之前的视图状态
|
||||||
|
map.getView().setCenter(currentCenter);
|
||||||
|
map.getView().setZoom(currentZoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleDeviceId() {
|
function toggleDeviceId() {
|
||||||
|
|||||||
@ -935,13 +935,16 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="dropdown-divider"></div>
|
<div class="dropdown-divider"></div>
|
||||||
<div class="dropdown-group">
|
<div class="dropdown-group">
|
||||||
<div class="dropdown-group-title">测量工具</div>
|
<div class="dropdown-group-title">定位工具</div>
|
||||||
<div class="dropdown-item" onclick="toggleMeasureDistance()">
|
<div class="dropdown-item" onclick="toggleMeasureDistance()">
|
||||||
测距
|
测距
|
||||||
</div>
|
</div>
|
||||||
<div class="dropdown-item" onclick="clearMeasure()">
|
<div class="dropdown-item" onclick="clearMeasure()">
|
||||||
清除测距
|
清除测距
|
||||||
</div>
|
</div>
|
||||||
|
<div class="dropdown-item" onclick="locateMyLocation()">
|
||||||
|
当前位置
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="dropdown-divider"></div>
|
<div class="dropdown-divider"></div>
|
||||||
<div class="dropdown-group" th:if="${role=='SUPER_ADMIN'}">
|
<div class="dropdown-group" th:if="${role=='SUPER_ADMIN'}">
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user