69 lines
2.0 KiB
Dart
69 lines
2.0 KiB
Dart
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import '../config/environment.dart';
|
|
import '../models/locationiq_model.dart';
|
|
|
|
class LocationIQService {
|
|
static final String baseUrl = Environment.locationIQBaseUrl;
|
|
late LocationIQ locationIQ;
|
|
|
|
Future<void> fetchLocationIQ(double lat, double lon) async {
|
|
// https://eu1.locationiq.com/v1/reverse?key=$locationIQKey&lat=47.93875449671056&lon=13.762706553431048&format=json
|
|
// Simulierte Antwort (für Testzwecke)
|
|
locationIQ = LocationIQ(
|
|
placeId: '12345',
|
|
licence: 'Data © OpenStreetMap contributors',
|
|
osmType: 'node',
|
|
osmId: '67890',
|
|
lat: lat.toString(),
|
|
lon: lon.toString(),
|
|
displayName: 'Test Location',
|
|
address: Address(
|
|
houseNumber: '123',
|
|
road: 'Test Street',
|
|
village: 'Test Village',
|
|
county: 'Test County',
|
|
state: 'Test State',
|
|
postcode: '12345',
|
|
country: 'Test Country',
|
|
countryCode: 'TC',
|
|
),
|
|
boundingbox: [
|
|
'47.93875449671056',
|
|
'47.93875449671056',
|
|
'13.762706553431048',
|
|
'13.762706553431048',
|
|
],
|
|
);
|
|
|
|
// Http Request
|
|
var httpClient = http.Client();
|
|
var url = '$baseUrl&lat=$lat&lon=$lon&format=json';
|
|
print('Fetching LocationIQ data from: $url');
|
|
try {
|
|
var response = await httpClient.get(Uri.parse(url));
|
|
if (response.statusCode == 200) {
|
|
print('LocationIQ API response: ${response.body}');
|
|
locationIQ = LocationIQ.fromJson(
|
|
Map<String, dynamic>.from(
|
|
jsonDecode(response.body) as Map<String, dynamic>,
|
|
),
|
|
);
|
|
print('LocationIQ data parsed successfully: ${locationIQ.displayName}');
|
|
} else {
|
|
print('Failed to fetch LocationIQ data. Status code: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
print('Error fetching LocationIQ data: $e');
|
|
} finally {
|
|
httpClient.close();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|