路及规划-集成(打点绘制)后端接口

This commit is contained in:
2026-02-26 10:09:59 +08:00
parent 0acc78f656
commit 56d8fe6d6b
5 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
class DeviceAddPathPointModel {
final double latitude;
final double longitude;
DeviceAddPathPointModel({required this.latitude, required this.longitude});
Map<String, dynamic> toJson() => {
'latitude': latitude,
'longitude': longitude,
};
factory DeviceAddPathPointModel.fromJson(Map<String, dynamic> json) => DeviceAddPathPointModel(
latitude: json['latitude'],
longitude: json['longitude'],
);
}

View File

@@ -0,0 +1,30 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../../domain/repositories/path_repository.dart';
import '../models/device_add_path_point_model.dart';
class PathRepositoryImpl implements PathRepository {
final String baseUrl = 'https://your-backend-api.com'; // 后端接口地址
// 生成路径
@override
Future<List<DeviceAddPathPointModel>> generatePath(List<DeviceAddPathPointModel> coordinates) async {
final url = Uri.parse('$baseUrl/api/path');
final headers = {'Content-Type': 'application/json'};
final body = jsonEncode({
'coordinates': coordinates.map((p) => p.toJson()).toList(),
});
try {
final response = await http.post(url, headers: headers, body: body);
if (response.statusCode == 200) {
final List<dynamic> jsonData = jsonDecode(response.body)['path'];
return jsonData.map((item) => DeviceAddPathPointModel.fromJson(item)).toList();
} else {
throw Exception('Failed to load path: ${response.statusCode}');
}
} catch (e) {
throw Exception('Network error: $e');
}
}
}