add locations by search

This commit is contained in:
Konstantin Kollar
2022-09-05 22:42:04 +02:00
parent 9de8e319c3
commit e93dc0f771
5 changed files with 158 additions and 5 deletions
@@ -0,0 +1,24 @@
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');
}
}
}