add locations by search
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user