import 'dart:math' as math; import 'package:flutter/widgets.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; /// GeoServer 基地址(公司端) const String kGeoserverBase = 'http://1.95.137.212:7900/geoserver'; /// 首期苏州区域图层标识 const String kSuzhouLayerName = 'workSpace:suzhou'; /// 苏州图层实际覆盖范围(取自 GetCapabilities,约 250m × 190m 的小地块) final LatLngBounds kSuzhouBounds = LatLngBounds( const LatLng(31.082450793459877, 120.83906484264836), const LatLng(31.084141983135456, 120.84164596446476), ); /// 场站中心点(真实地块中心) const LatLng kSuzhouCenter = LatLng(31.083296, 120.840355); /// 叠加层取瓦片方式开关: /// - false(当前):WMS GetMap 兜底,不依赖 GWC 缓存 /// - true:WMTS GetTile(公司补配 GWC EPSG:3857 gridset 并 Seed 后切换) const bool kUseWmtsTile = false; /// WMS 取图提供器: /// 实测 GeoServer 对 workSpace:suzhou 只有原生 EPSG:4326 能出图, /// 请求 EPSG:3857(WMS 1.1.1 / 1.3.0 均验证过)返回纯白图。 /// 因此这里按瓦片 XYZ 反算经纬度 bbox,以 EPSG:4326 请求 WMS。 class GeoWmsProvider extends TileProvider { GeoWmsProvider({this.maxLevel = 21}); final int maxLevel; /// Web 墨卡托地球周长(米) static const double _full = 40075016.685578488; /// Web 墨卡托 Y(米)→ 纬度(度) static double _mercatorYToLat(double y) { final t = y / 6378137.0; final sinh = (math.exp(t) - math.exp(-t)) / 2; // dart:math 无 sinh,手动实现 return math.atan(sinh) * 180 / math.pi; } @override ImageProvider getImage( TileCoordinates coordinates, TileLayer options, ) { if (coordinates.z > maxLevel) { return MemoryImage(TileProvider.transparentImage); } final span = _full / math.pow(2, coordinates.z); final westM = -_full / 2 + coordinates.x * span; final eastM = westM + span; final northM = _full / 2 - coordinates.y * span; final southM = northM - span; final westLng = westM / _full * 360; final eastLng = eastM / _full * 360; final southLat = _mercatorYToLat(southM); final northLat = _mercatorYToLat(northM); // 与图层实际范围不相交的瓦片直接返回透明图,不发无谓请求 final intersects = westLng < kSuzhouBounds.east && eastLng > kSuzhouBounds.west && southLat < kSuzhouBounds.north && northLat > kSuzhouBounds.south; if (!intersects) { return MemoryImage(TileProvider.transparentImage); } final url = Uri.parse('$kGeoserverBase/wms').replace( queryParameters: { 'SERVICE': 'WMS', 'REQUEST': 'GetMap', 'VERSION': '1.1.1', 'LAYERS': kSuzhouLayerName, 'BBOX': '${westLng.toStringAsFixed(6)},${southLat.toStringAsFixed(6)},' '${eastLng.toStringAsFixed(6)},${northLat.toStringAsFixed(6)}', 'WIDTH': '256', 'HEIGHT': '256', 'FORMAT': 'image/png', 'TRANSPARENT': 'true', 'SRS': 'EPSG:4326', 'STYLES': '', }, ); return NetworkImage(url.toString()); } } /// GeoServer 苏州无人机高清影像叠加层(WGS84 基准,覆盖在 ESRI 底图上) /// /// 地块很小(约 250m),zoom 16 以上才有意义,默认 minZoom = 16 class GeoSuzhouOverlay extends StatelessWidget { const GeoSuzhouOverlay({super.key, this.minZoom = 16, this.maxZoom = 21}); /// 小于该级别不请求瓦片(地块太小,低层级看不见) final double minZoom; /// 大于该级别不请求瓦片 final double maxZoom; @override Widget build(BuildContext context) { if (kUseWmtsTile) { // === WMTS 版:公司把 EPSG:3857 gridset 加入 GWC 并 Seed 后生效 === return TileLayer( urlTemplate: '$kGeoserverBase/gwc/service/wmts' '?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0' '&LAYER=$kSuzhouLayerName&TILEMATRIXSET=EPSG:3857' '&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png', userAgentPackageName: 'com.example.app', minZoom: minZoom, maxZoom: maxZoom, maxNativeZoom: maxZoom.toInt(), tileBounds: kSuzhouBounds, ); } // === WMS 版(当前兜底):按原生 EPSG:4326 请求,GeoServer 实时渲染 === return TileLayer( tileProvider: GeoWmsProvider(maxLevel: maxZoom.toInt()), userAgentPackageName: 'com.example.app', minZoom: minZoom, maxZoom: maxZoom, maxNativeZoom: maxZoom.toInt(), tileBounds: kSuzhouBounds, ); } }