25 lines
782 B
Dart
25 lines
782 B
Dart
import 'dart:convert';
|
|
|
|
import 'package:wetter/models/location_model.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class OpenStreetMapAPI {
|
|
static Future<List<LocationModel>> getLocationsFromSearch(
|
|
String search) async {
|
|
final response = await http.get(
|
|
Uri.parse(
|
|
'https://nominatim.openstreetmap.org/search.php?q=$search&format=jsonv2'),
|
|
headers: {"Accept": "application/json"});
|
|
if (response.statusCode == 200) {
|
|
List<LocationModel> locations = [];
|
|
List<dynamic> results = jsonDecode(response.body);
|
|
for (Map<String, dynamic> result in results) {
|
|
locations.add(LocationModel.fromJson(result));
|
|
}
|
|
return locations;
|
|
} else {
|
|
throw Exception('Failed to load weather data');
|
|
}
|
|
}
|
|
}
|