| | |
| | | const switchedCity = Taro.getStorageSync('selectedCity'); |
| | | if (switchedCity?.name) { |
| | | getCityLocation(switchedCity.name).then((loc) => { |
| | | setLatitude(loc.latitude); |
| | | setLongitude(loc.longitude); |
| | | shouldIgnoreRegionChange.current = true; |
| | | skipNextLocationCenterRef.current = true; |
| | | const mapCtx = Taro.createMapContext('map'); |
| | |
| | | // 定位到城市中心 |
| | | if (selectedCity.name && allowMapMove) { |
| | | getCityLocation(selectedCity.name).then(loc => { |
| | | setLatitude(loc.latitude); |
| | | setLongitude(loc.longitude); |
| | | skipNextLocationCenterRef.current = true; |
| | | shouldIgnoreRegionChange.current = true; |
| | | const mapCtx = Taro.createMapContext('map'); |
| | |
| | | // 定位到城市中心 |
| | | if (selectedCity.name && allowMapMove) { |
| | | getCityLocation(selectedCity.name).then(loc => { |
| | | setLatitude(loc.latitude); |
| | | setLongitude(loc.longitude); |
| | | skipNextLocationCenterRef.current = true; |
| | | shouldIgnoreRegionChange.current = true; |
| | | const mapCtx = Taro.createMapContext('map'); |
| | |
| | | const systemSetting = await Promise.resolve((Taro as any).getSystemSetting?.()); |
| | | if (systemSetting?.locationEnabled === false) { |
| | | Taro.showToast({ |
| | | title: '需开启设备GPS/定位服务开关', |
| | | title: '需开启设备定位服务开关', |
| | | icon: 'none' |
| | | }); |
| | | return; |
| | |
| | | if (type === 'begin') { |
| | | setShowToolTip(false); |
| | | } else if (type === 'end') { |
| | | const wasReturningToOrigin = isReturningToOrigin.current; |
| | | if (isReturningToOrigin.current) { |
| | | isReturningToOrigin.current = false; |
| | | } |
| | |
| | | mapCtx.getCenterLocation({ |
| | | success: async (res) => { |
| | | const { latitude: lat, longitude: lng } = res; |
| | | if (causedBy === 'gesture' || causedBy === 'drag') { |
| | | setLatitude(lat); |
| | | setLongitude(lng); |
| | | } |
| | | // 移除 setLatitude 和 setLongitude,避免拖动后地图中心点跳动回旧位置 |
| | | // if (causedBy === 'gesture' || causedBy === 'drag') { |
| | | // setLatitude(lat); |
| | | // setLongitude(lng); |
| | | // } |
| | | |
| | | try { |
| | | // 更新地图数据 |
| | | const { newMarkers, sites } = await updateMapData(lat, lng); |
| | | // 更新地图数据 |
| | | const { newMarkers, sites } = await updateMapData(lat, lng); |
| | | |
| | | const nearestCandidates = sites.length > 0 ? sites : newMarkers; |
| | | const { targetMarker, minDistance } = findNearestMarker(lat, lng, nearestCandidates); |
| | | const nearestCandidates = sites.length > 0 ? sites : newMarkers; |
| | | const { targetMarker, minDistance } = findNearestMarker(lat, lng, nearestCandidates); |
| | | |
| | | // 只有用户手势拖动触发的移动才进行吸附判断 |
| | | if ((causedBy === 'gesture' || causedBy === 'drag') && targetMarker && minDistance < adsorptionDistance) { |
| | | // 使用 moveToLocation 实现吸附效果 |
| | | mapCtx.moveToLocation({ |
| | | latitude: targetMarker.latitude, |
| | | longitude: targetMarker.longitude, |
| | | }); |
| | | } |
| | | // 用户手势拖动或点击回到原点触发的移动,进行吸附判断 |
| | | if ((causedBy === 'gesture' || causedBy === 'drag' || wasReturningToOrigin) && targetMarker && minDistance < adsorptionDistance) { |
| | | // 使用 moveToLocation 实现吸附效果 |
| | | mapCtx.moveToLocation({ |
| | | latitude: targetMarker.latitude, |
| | | longitude: targetMarker.longitude, |
| | | }); |
| | | } |
| | | |
| | | // 根据距离决定是否显示 ToolTip |
| | | setTimeout(() => { |