远程遥控相关
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
|
||||
import 'base_map_config.dart';
|
||||
import 'resilient_tile_provider.dart';
|
||||
|
||||
const String _kUserAgent = 'com.maibu.maibu_satabot_v2';
|
||||
|
||||
/// App 统一底图:数据源跟随 App 语言
|
||||
/// - 中文 → ESRI 卫星影像(境内稳定,WGS84 直用)
|
||||
/// - 其他语言 → Google 混合图(卫星 + 英文地名)
|
||||
/// - Google 不可达(境内网络)→ 自动回退 ESRI,不出现空白地图
|
||||
class AppBaseMapLayer extends StatelessWidget {
|
||||
const AppBaseMapLayer({super.key, this.maxZoom = 21, this.forceConfig});
|
||||
|
||||
final double maxZoom;
|
||||
|
||||
/// 强制指定源(离线包/调试用),传了就忽略语言
|
||||
final BaseMapConfig? forceConfig;
|
||||
|
||||
/// 已打印过的「语言 + 实际底图源」组合,避免每帧刷屏
|
||||
static String? _loggedSignature;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final locale = Localizations.localeOf(context);
|
||||
final cfg = forceConfig ?? BaseMapConfig.of(locale);
|
||||
if (cfg.provider == BaseMapProvider.esri) {
|
||||
_logSelection(locale, cfg, fromFallback: false);
|
||||
return _build(cfg);
|
||||
}
|
||||
|
||||
return FutureBuilder<bool>(
|
||||
future: GoogleTileAvailability.reachable(),
|
||||
builder: (context, snapshot) {
|
||||
final googleOk = snapshot.data == true;
|
||||
_logSelection(
|
||||
locale,
|
||||
googleOk ? cfg : BaseMapConfig.esri,
|
||||
fromFallback: !googleOk,
|
||||
);
|
||||
// 探测中 / 不可达 → 先用 ESRI 顶住
|
||||
if (!googleOk) return _build(BaseMapConfig.esri);
|
||||
return _build(cfg);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _logSelection(
|
||||
Locale locale,
|
||||
BaseMapConfig cfg, {
|
||||
required bool fromFallback,
|
||||
}) {
|
||||
final sig = '${locale.languageCode}/${cfg.provider.name}/$fromFallback';
|
||||
if (_loggedSignature == sig) return;
|
||||
_loggedSignature = sig;
|
||||
debugPrint(
|
||||
'🗺️ [BaseMap] 语言=${locale.languageCode} → 底图=${cfg.provider.name}'
|
||||
'${fromFallback ? '(Google 不可达,已回退)' : ''}',
|
||||
);
|
||||
}
|
||||
|
||||
Widget _build(BaseMapConfig cfg) {
|
||||
// key 随源变化:语言切换时强制重建瓦片层,避免复用旧源瓦片
|
||||
final key = ValueKey('base_map_${cfg.provider.name}');
|
||||
if (cfg.provider == BaseMapProvider.google) {
|
||||
return TileLayer(
|
||||
key: key,
|
||||
tileProvider: GoogleTileProvider(lyr: cfg.googleLyr!),
|
||||
userAgentPackageName: _kUserAgent,
|
||||
minZoom: 0,
|
||||
maxNativeZoom: cfg.maxNativeZoom,
|
||||
maxZoom: maxZoom,
|
||||
);
|
||||
}
|
||||
return TileLayer(
|
||||
key: key,
|
||||
urlTemplate: cfg.urlTemplate,
|
||||
userAgentPackageName: _kUserAgent,
|
||||
minZoom: 0,
|
||||
maxNativeZoom: cfg.maxNativeZoom,
|
||||
maxZoom: maxZoom,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Google 栅格瓦片:与 flutter_map 的 XYZ 编号一致,无需坐标转换
|
||||
class GoogleTileProvider extends TileProvider {
|
||||
GoogleTileProvider({required this.lyr, this.hl = 'en'});
|
||||
|
||||
final String lyr;
|
||||
final String hl;
|
||||
|
||||
@override
|
||||
ImageProvider<Object> getImage(
|
||||
TileCoordinates coordinates,
|
||||
TileLayer options,
|
||||
) {
|
||||
final sub = coordinates.x % 4;
|
||||
final url = 'https://mt$sub.google.com/vt/lyrs=$lyr&hl=$hl'
|
||||
'&x=${coordinates.x}&y=${coordinates.y}&z=${coordinates.z}';
|
||||
return ResilientNetworkImage(url);
|
||||
}
|
||||
}
|
||||
102
lib/features/home/presentation/widgets/map/base_map_config.dart
Normal file
102
lib/features/home/presentation/widgets/map/base_map_config.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
/// 底图来源:中文用户用 ESRI,非中文用户用 Google
|
||||
enum BaseMapProvider { esri, google }
|
||||
|
||||
class BaseMapConfig {
|
||||
const BaseMapConfig({
|
||||
required this.provider,
|
||||
required this.maxNativeZoom,
|
||||
this.urlTemplate,
|
||||
this.googleLyr,
|
||||
});
|
||||
|
||||
final BaseMapProvider provider;
|
||||
|
||||
/// 原生最大层级:超出后由 flutter_map 用低级别瓦片拉伸,避免整片空白
|
||||
final int maxNativeZoom;
|
||||
|
||||
/// ESRI XYZ 模板(WGS84,与设备 GPS 同一坐标系)
|
||||
final String? urlTemplate;
|
||||
|
||||
/// Google 瓦片图层:s=纯卫星 / h=地名 / y=卫星+地名
|
||||
final String? googleLyr;
|
||||
|
||||
static const BaseMapConfig esri = BaseMapConfig(
|
||||
provider: BaseMapProvider.esri,
|
||||
maxNativeZoom: 19,
|
||||
urlTemplate:
|
||||
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
|
||||
);
|
||||
|
||||
/// lyrs=y 让境外用户一张图同时拿到卫星影像 + 英文地名(hl=en)
|
||||
static const BaseMapConfig google = BaseMapConfig(
|
||||
provider: BaseMapProvider.google,
|
||||
maxNativeZoom: 20,
|
||||
googleLyr: 'y',
|
||||
);
|
||||
|
||||
/// 规则:中文 → ESRI;其余语言 → Google
|
||||
static BaseMapConfig of(Locale locale) =>
|
||||
locale.languageCode == 'zh' ? esri : google;
|
||||
}
|
||||
|
||||
/// Google 瓦片可达性探测:失败后允许定期重试,避免一次探测失败就永久降级
|
||||
class GoogleTileAvailability {
|
||||
/// 失败后的重试冷却时长:开/关 VPN、切换网络后无需重启 App
|
||||
static const Duration retryAfter = Duration(seconds: 30);
|
||||
|
||||
static Future<bool>? _cached;
|
||||
static bool? _lastResult;
|
||||
static DateTime? _lastFailAt;
|
||||
|
||||
static Future<bool> reachable() {
|
||||
final cached = _cached;
|
||||
if (cached != null) {
|
||||
final failedAt = _lastFailAt;
|
||||
final shouldRetry = _lastResult == false &&
|
||||
failedAt != null &&
|
||||
DateTime.now().difference(failedAt) >= retryAfter;
|
||||
// 继续复用同一个 Future 对象,避免 FutureBuilder 因 future 变化而反复重建
|
||||
if (!shouldRetry) return cached;
|
||||
}
|
||||
final future = _probe();
|
||||
_cached = future;
|
||||
return future;
|
||||
}
|
||||
|
||||
/// 手动重试(例如用户点「刷新地图」、App 从后台恢复)
|
||||
static void reset() {
|
||||
_cached = null;
|
||||
_lastResult = null;
|
||||
_lastFailAt = null;
|
||||
}
|
||||
|
||||
static Future<bool> _probe() async {
|
||||
final sw = Stopwatch()..start();
|
||||
bool ok;
|
||||
try {
|
||||
final res = await http
|
||||
.get(Uri.parse('https://mt0.google.com/vt/lyrs=s&hl=en&x=1&y=0&z=1'))
|
||||
.timeout(const Duration(seconds: 3));
|
||||
ok = res.statusCode == 200 && res.bodyBytes.isNotEmpty;
|
||||
debugPrint(
|
||||
'🗺️ [BaseMap] Google 探测: http=${res.statusCode} '
|
||||
'bytes=${res.bodyBytes.length} ${sw.elapsedMilliseconds}ms '
|
||||
'→ ${ok ? '可用,英文底图用 Google' : '不可用,回退 ESRI'}',
|
||||
);
|
||||
} catch (e) {
|
||||
ok = false;
|
||||
debugPrint(
|
||||
'🗺️ [BaseMap] Google 探测异常: ${e.runtimeType} '
|
||||
'${sw.elapsedMilliseconds}ms → 回退 ESRI',
|
||||
);
|
||||
}
|
||||
_lastResult = ok;
|
||||
_lastFailAt = ok ? null : DateTime.now();
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user