| | |
| | | code: '', |
| | | }); // 选中的城市 |
| | | |
| | | const ensureSelectedCityReady = async (lng: number, lat: number, skipCityEffect = false) => { |
| | | const ensureSelectedCityReady = async (lng: number, lat: number, skipCityEffect = false, forceRefresh = false) => { |
| | | const cachedCity = Taro.getStorageSync('selectedCity'); |
| | | if (cachedCity?.code) { |
| | | if (!forceRefresh && cachedCity?.code && typeof cachedCity?.id === 'number') { |
| | | return cachedCity; |
| | | } |
| | | try { |
| | |
| | | } |
| | | if (auth === false) { |
| | | setIsLocationAuthDenied(true); |
| | | // 检查是否有选中的城市缓存 |
| | | const selectedCity = Taro.getStorageSync('selectedCity'); |
| | | if (!selectedCity || !selectedCity.code) { |
| | | // 没有缓存,尝试通过 IP 获取位置 |
| | | getLocationByIP().then(res => { |
| | | setLatitude(res.latitude); |
| | | setLongitude(res.longitude); |
| | | const cityName = res.city || res.province || '北京市'; |
| | | const cityCode = convertAdcodeToCityCode(res.adcode, cityName, res.province); |
| | | const cityInfo = { |
| | | name: cityName, |
| | | code: cityCode |
| | | }; |
| | | setSelectedCity(cityInfo); |
| | | Taro.setStorageSync('selectedCity', cityInfo); |
| | | }).catch(() => { |
| | | // IP 获取失败,使用默认值 |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | setSelectedCity({ |
| | | name: '北京市', |
| | | code: '110000', |
| | | }) |
| | | Taro.setStorageSync('selectedCity', { |
| | | name: '北京市', |
| | | code: '110000', |
| | | }); |
| | | }); |
| | | } else { |
| | | // 有缓存,但授权被拒绝,仍然可以尝试通过 IP 更新中心点 |
| | | getLocationByIP().then(res => { |
| | | setLatitude(res.latitude); |
| | | setLongitude(res.longitude); |
| | | }).catch(() => { |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | }); |
| | | } |
| | | setMapScale(3); |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | return; |
| | | } else { |
| | | setIsLocationAuthDenied(false); |
| | | // 检查缓存中是否有 startSite |
| | |
| | | return; |
| | | } |
| | | if (!selectedCity.code) return; |
| | | const allowMapMove = !isLocationAuthDenied; |
| | | getGeofence({ cityCode: selectedCity.code }).then((res: any) => { |
| | | |
| | | if (res && Array.isArray(res) && res.length > 0) { |
| | | const newPolygons = formatGeofencePolygons(res); |
| | | setPolygons(newPolygons); |
| | | if (!Taro.getStorageSync('startSite')) { |
| | | const shouldIncludeOperatingArea = allowMapMove && !Taro.getStorageSync('startSite'); |
| | | if (shouldIncludeOperatingArea) { |
| | | if (skipFenceIncludePointsRef.current) { |
| | | skipFenceIncludePointsRef.current = false; |
| | | } else { |
| | |
| | | setPolygons([]); |
| | | if ((orderData || orderData?.id) && JSON.stringify(orderData) != '{}') return; |
| | | // 定位到城市中心 |
| | | if (selectedCity.name) { |
| | | if (selectedCity.name && allowMapMove) { |
| | | getCityLocation(selectedCity.name).then(loc => { |
| | | setLatitude(loc.latitude); |
| | | setLongitude(loc.longitude); |
| | |
| | | console.error('获取电子围栏失败', err); |
| | | setPolygons([]); |
| | | // 定位到城市中心 |
| | | if (selectedCity.name) { |
| | | if (selectedCity.name && allowMapMove) { |
| | | getCityLocation(selectedCity.name).then(loc => { |
| | | setLatitude(loc.latitude); |
| | | setLongitude(loc.longitude); |
| | |
| | | }); |
| | | } |
| | | }); |
| | | }, [selectedCity.code, selectedCity.name]); |
| | | }, [selectedCity.code, selectedCity.name, isLocationAuthDenied]); |
| | | |
| | | const calculateBoxHeight = () => { |
| | | // 延迟计算,确保 DOM 渲染完成 |
| | |
| | | } |
| | | |
| | | // 获取用户位置信息 |
| | | const getLocationInfo = (updateCenter = true) => { |
| | | const getLocationInfo = (updateCenter = true, forceCityRefresh = false) => { |
| | | console.log('getLocation获取用户位置信息'); |
| | | Taro.getLocation({ type: 'gcj02' }) |
| | | .then(async res => { |
| | |
| | | } |
| | | |
| | | try { |
| | | const cityInfo = await ensureSelectedCityReady(res.longitude, res.latitude); |
| | | const cityInfo = await ensureSelectedCityReady(res.longitude, res.latitude, false, forceCityRefresh); |
| | | if (cityInfo?.code) { |
| | | const { newMarkers, sites } = await updateMapData(res.latitude, res.longitude, cityInfo.code); |
| | | const { targetMarker, minDistance } = findNearestMarker(res.latitude, res.longitude, sites.length > 0 ? sites : newMarkers); |
| | |
| | | Taro.getSetting({ |
| | | success: (res) => { |
| | | const auth = res.authSetting['scope.userLocation']; |
| | | setIsLocationAuthDenied(auth === false); |
| | | if (auth === false) { |
| | | setIsLocationAuthDenied(true); |
| | | setMapScale(3); |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | return; |
| | | } |
| | | setIsLocationAuthDenied(false); |
| | | getLocationByIP().then(res => { |
| | | setLatitude(res.latitude); |
| | | setLongitude(res.longitude); |
| | | const cityName = res.city || res.province || '北京市'; |
| | | const cityCode = convertAdcodeToCityCode(res.adcode, cityName, res.province); |
| | | const cityInfo = { |
| | | name: cityName, |
| | | code: cityCode |
| | | }; |
| | | setSelectedCity(cityInfo); |
| | | Taro.setStorageSync('selectedCity', cityInfo); |
| | | setCurrentAddress(cityName); |
| | | setCurrentSite({ |
| | | name: cityName, |
| | | latitude: res.latitude, |
| | | longitude: res.longitude |
| | | }); |
| | | }).catch(() => { |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | }); |
| | | }, |
| | | fail: () => { |
| | | setIsLocationAuthDenied(false); |
| | | getLocationByIP().then(res => { |
| | | setLatitude(res.latitude); |
| | | setLongitude(res.longitude); |
| | | const cityName = res.city || res.province || '北京市'; |
| | | const cityCode = convertAdcodeToCityCode(res.adcode, cityName, res.province); |
| | | const cityInfo = { |
| | | name: cityName, |
| | | code: cityCode |
| | | }; |
| | | setSelectedCity(cityInfo); |
| | | Taro.setStorageSync('selectedCity', cityInfo); |
| | | setCurrentAddress(cityName); |
| | | setCurrentSite({ |
| | | name: cityName, |
| | | latitude: res.latitude, |
| | | longitude: res.longitude |
| | | }); |
| | | }).catch(() => { |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | }); |
| | | } |
| | | }); |
| | | getLocationByIP().then(res => { |
| | | setLatitude(res.latitude); |
| | | setLongitude(res.longitude); |
| | | const cityName = res.city || res.province || '北京市'; |
| | | const cityCode = convertAdcodeToCityCode(res.adcode, cityName, res.province); |
| | | const cityInfo = { |
| | | name: cityName, |
| | | code: cityCode |
| | | }; |
| | | setSelectedCity(cityInfo); |
| | | Taro.setStorageSync('selectedCity', cityInfo); |
| | | setCurrentAddress(cityName); |
| | | setCurrentSite({ |
| | | name: cityName, |
| | | latitude: res.latitude, |
| | | longitude: res.longitude |
| | | }); |
| | | }).catch(() => { |
| | | setLatitude(DEFAULT_LATITUDE); |
| | | setLongitude(DEFAULT_LONGITUDE); |
| | | }); |
| | | }); |
| | | } |
| | |
| | | const toOrigin = async () => { |
| | | const ok = await ensureNetwork(); |
| | | if (!ok) return; |
| | | if (isLocationAuthDenied) return; |
| | | shouldIgnoreRegionChange.current = false; |
| | | isReturningToOrigin.current = true; |
| | | setMapScale(14); |
| | |
| | | const type = e?.type || e?.detail?.type; |
| | | // 获取触发源: gesture (手势), update (接口调用) |
| | | const causedBy = e?.causedBy || e?.detail?.causedBy; |
| | | if (isLocationAuthDenied) return; |
| | | |
| | | // 如果是用户手势拖动,则必须触发,且重置忽略标记 |
| | | if (causedBy === 'gesture' || causedBy === 'drag' || causedBy === 'scale') { |
| | |
| | | success: (res) => { |
| | | if (res.authSetting['scope.userLocation']) { |
| | | setIsLocationAuthDenied(false); |
| | | getLocationInfo(); |
| | | setMapScale(14); |
| | | getLocationInfo(true, true); |
| | | } |
| | | } |
| | | }) |
| | |
| | | return; |
| | | } |
| | | if (isToMapStart) { |
| | | Taro.setStorageSync('startSite', currentSite); |
| | | Taro.navigateTo({ |
| | | url: '/robotaxi/pages/select-map-site/index?pageType=' + type, |
| | | }) |
| | |
| | | const handleViewOperatingArea = async () => { |
| | | const ok = await ensureNetwork(); |
| | | if (!ok) return; |
| | | if (isLocationAuthDenied) return; |
| | | if (!selectedCity?.code) { |
| | | Taro.showToast({ |
| | | title: '此城市暂未开通运营区域', |
| | |
| | | <View className="home-container"> |
| | | <Map |
| | | id="map" |
| | | longitude={longitude ?? 104.066541} |
| | | latitude={latitude ?? 30.572269} |
| | | longitude={longitude ?? DEFAULT_LONGITUDE} |
| | | latitude={latitude ?? DEFAULT_LATITUDE} |
| | | scale={mapScale} |
| | | markers={isLocationAuthDenied ? [] : markers} |
| | | polygons={polygons} |