You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.1 KiB
Dart
160 lines
5.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_map/flutter_map.dart';
|
|
import 'package:flutter_map_geojson2/flutter_map_geojson2.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:test_sa/helper/utils.dart';
|
|
import 'package:test_sa/new_views/app_style/app_color.dart';
|
|
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
|
|
|
|
import 'asset_tracking_provider.dart';
|
|
import 'models/asset_tracking_model.dart';
|
|
import 'models/floor_map_model.dart';
|
|
|
|
class MapTrackingPage extends StatefulWidget {
|
|
final AssetTrackingModel? assetTrackingData;
|
|
final bool inAppView;
|
|
|
|
MapTrackingPage({Key? key, this.inAppView = false, this.assetTrackingData}) : super(key: key);
|
|
|
|
@override
|
|
_MapTrackingPageState createState() {
|
|
return _MapTrackingPageState();
|
|
}
|
|
}
|
|
|
|
class _MapTrackingPageState extends State<MapTrackingPage> {
|
|
final MapController mapController = MapController();
|
|
late AssetTrackingProvider assetTrackingProvider;
|
|
|
|
FloorMapModel? floorMapModel;
|
|
Map<String, dynamic>? _geojson;
|
|
List<Marker> markers = [];
|
|
LatLngBounds? _computedBounds;
|
|
|
|
// Fallback shown only until the real floor plan bounds are computed.
|
|
static const LatLng _fallbackCenter = LatLng(24.81200245073082, 46.624273601310186);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
assetTrackingProvider = Provider.of<AssetTrackingProvider>(context, listen: false);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (widget.assetTrackingData != null) {
|
|
print('widget.assetTrackingData!.location!.floor!.id!: ${widget.assetTrackingData!.toJson()}');
|
|
Utils.showLoading(context);
|
|
assetTrackingProvider.getFloorMap(widget.assetTrackingData!.location!.floor!.id!).then((floorMap) {
|
|
Utils.hideLoading(context);
|
|
_onFloorMapLoaded(floorMap);
|
|
}).catchError((error) {
|
|
print('Failed to load floor map: $error');
|
|
Utils.hideLoading(context);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
void _onFloorMapLoaded(FloorMapModel? loadedFloorMap) {
|
|
floorMapModel = loadedFloorMap;
|
|
|
|
if (floorMapModel?.geojsonData == null) {
|
|
setState(() {});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
final decoded = floorMapModel!.geojsonData!;
|
|
final bounds = _computeBoundsFromGeoJson(decoded);
|
|
|
|
setState(() {
|
|
_geojson = decoded;
|
|
_computedBounds = bounds;
|
|
markers = [
|
|
Marker(
|
|
point: LatLng(widget.assetTrackingData!.location!.latitude!, widget.assetTrackingData!.location!.longitude!),
|
|
child: const Icon(Icons.location_pin, color: AppColor.red30),
|
|
),
|
|
];
|
|
});
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
mapController.fitCamera(
|
|
CameraFit.bounds(bounds: bounds, padding: const EdgeInsets.all(24)),
|
|
);
|
|
});
|
|
} catch (e) {
|
|
print('Failed to parse/compute bounds for floor geojson: $e');
|
|
setState(() {}); // still show the map with whatever we have
|
|
}
|
|
}
|
|
|
|
LatLngBounds _computeBoundsFromGeoJson(Map<String, dynamic> geojson) {
|
|
final features = (geojson['features'] as List<dynamic>? ?? []);
|
|
|
|
double? minLng, minLat, maxLng, maxLat;
|
|
|
|
void visit(dynamic node) {
|
|
if (node is List && node.isNotEmpty && node.first is num) {
|
|
final lng = (node[0] as num).toDouble();
|
|
final lat = (node[1] as num).toDouble();
|
|
minLng = (minLng == null || lng < minLng!) ? lng : minLng;
|
|
maxLng = (maxLng == null || lng > maxLng!) ? lng : maxLng;
|
|
minLat = (minLat == null || lat < minLat!) ? lat : minLat;
|
|
maxLat = (maxLat == null || lat > maxLat!) ? lat : maxLat;
|
|
} else if (node is List) {
|
|
for (final child in node) {
|
|
visit(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
for (final feature in features) {
|
|
final geometry = feature['geometry'] as Map<String, dynamic>?;
|
|
final coordinates = geometry?['coordinates'];
|
|
if (coordinates == null) continue;
|
|
visit(coordinates);
|
|
}
|
|
|
|
if (minLng == null || minLat == null || maxLng == null || maxLat == null) {
|
|
throw StateError('No coordinates found in floor geojson — cannot compute bounds.');
|
|
}
|
|
|
|
return LatLngBounds(
|
|
LatLng(minLat!, minLng!),
|
|
LatLng(maxLat!, maxLng!),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Widget mapWidget = FlutterMap(
|
|
mapController: mapController,
|
|
options: MapOptions(initialCenter: _fallbackCenter, initialZoom: 16.0),
|
|
children: [
|
|
TileLayer(
|
|
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
userAgentPackageName: 'com.hmg.atoms',
|
|
),
|
|
if (_geojson != null) ...[
|
|
GeoJsonLayer.memory(
|
|
_geojson!,
|
|
styleDefaults: GeoJsonStyleDefaults.leaflet,
|
|
),
|
|
MarkerLayer(markers: markers),
|
|
]
|
|
],
|
|
);
|
|
|
|
return widget.inAppView
|
|
? mapWidget
|
|
: Scaffold(
|
|
appBar: const DefaultAppBar(title: "Live Asset Tracking"),
|
|
body: Stack(
|
|
children: [mapWidget],
|
|
),
|
|
);
|
|
}
|
|
}
|