search now shows multiple results. added mapcontroller to move the map around
This commit is contained in:
parent
6de35f6a0b
commit
6cfa321386
@ -13,6 +13,7 @@ import 'models.dart';
|
|||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'api.dart' as api;
|
import 'api.dart' as api;
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:developer';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -52,14 +53,35 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||||
List<Favorite> _favorites = [];
|
List<Favorite> _favorites = [];
|
||||||
LatLng? _selectedPoint;
|
LatLng? _selectedPoint;
|
||||||
LatLng _currentPosition = LatLng(55.9397, 9.5156);
|
LatLng _currentPosition = LatLng(55.656707, 10.563214);
|
||||||
|
LatLng? _userPosition;
|
||||||
double _zoom = 7.0;
|
double _zoom = 7.0;
|
||||||
|
MapController _mapController = MapController();
|
||||||
|
List<SearchResults> _searchResults = [];
|
||||||
final TextEditingController searchBarInput =TextEditingController(text: '');
|
final TextEditingController searchBarInput =TextEditingController(text: '');
|
||||||
final LocationSettings locationSettings = const LocationSettings(
|
final LocationSettings locationSettings = const LocationSettings(
|
||||||
accuracy: LocationAccuracy.high,
|
accuracy: LocationAccuracy.high,
|
||||||
distanceFilter: 100,
|
distanceFilter: 100,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
|
||||||
|
api.isLoggedIn(context).then((isLoggedIn) {
|
||||||
|
if (!isLoggedIn || !mounted) return;
|
||||||
|
|
||||||
|
_fetchFavorites();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
searchBarInput.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
void _onTap(TapPosition _, LatLng point) async {
|
void _onTap(TapPosition _, LatLng point) async {
|
||||||
setState(() => _selectedPoint = point);
|
setState(() => _selectedPoint = point);
|
||||||
|
|
||||||
@ -186,65 +208,59 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void didChangeDependencies() {
|
|
||||||
super.didChangeDependencies();
|
|
||||||
|
|
||||||
api.isLoggedIn(context).then((isLoggedIn) {
|
|
||||||
if (!isLoggedIn || !mounted) return;
|
|
||||||
|
|
||||||
_fetchFavorites();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> GetOpenStreetMapArea() async {
|
Future<void> GetOpenStreetMapArea() async {
|
||||||
final dynamic location;
|
final dynamic location;
|
||||||
LatLng point;
|
|
||||||
|
|
||||||
if (searchBarInput.text != '') {
|
if (searchBarInput.text != '') {
|
||||||
|
|
||||||
|
_searchResults.clear();
|
||||||
|
|
||||||
final response = await http.get(
|
final response = await http.get(
|
||||||
Uri.parse('https://nominatim.openstreetmap.org/search.php?q=${searchBarInput.text}&format=jsonv2'),
|
Uri.parse('https://nominatim.openstreetmap.org/search.php?q=Attractions+in+${searchBarInput.text}&format=jsonv2'),
|
||||||
headers: {'User-Agent': 'SkanTravels/1.0'},
|
headers: {'User-Agent': 'SkanTravels/1.0'},
|
||||||
);
|
);
|
||||||
|
|
||||||
location = jsonDecode(response.body);
|
location = jsonDecode(response.body);
|
||||||
|
|
||||||
if (location is List && location.isNotEmpty) {
|
if (location is List && location.isNotEmpty) {
|
||||||
final firstResult = location[0]; // Pick the first entry
|
for (var i = 0; i < location.length; i++) {
|
||||||
|
if (location[i]['lat'] != null && location[i]['lon'] != null && location[i]['name'] != null && location[i]['display_name'] != null) {
|
||||||
|
|
||||||
if (firstResult['lat'] != null && firstResult['lon'] != null && firstResult['name'] != null && firstResult['display_name'] != null) {
|
double lat = double.parse(location[i]['lat']);
|
||||||
double lat = double.parse(firstResult['lat']);
|
double lon = double.parse(location[i]['lon']);
|
||||||
double lon = double.parse(firstResult['lon']);
|
String description = location[i]['display_name'];
|
||||||
point = LatLng(lat, lon);
|
String name = location[i]['name'];
|
||||||
setState(() => _selectedPoint = point);
|
|
||||||
await _showLocation(point, firstResult['name'], firstResult['display_name']);
|
if (name == "") {
|
||||||
}
|
name = description.split(",")[0];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
setState(() {
|
||||||
|
_searchResults.add(SearchResults(LatLng(lat, lon), name, description));
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
searchBarInput.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
}
|
||||||
|
_mapController.move(_searchResults[0].location, 9);
|
||||||
@override
|
}
|
||||||
void initState() {
|
}
|
||||||
super.initState();
|
|
||||||
_getCurrentLocation(); // Call the async method inside initState
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _getCurrentLocation() async {
|
Future<void> _getCurrentLocation() async {
|
||||||
await Geolocator.checkPermission();
|
LocationPermission permission = await Geolocator.checkPermission();
|
||||||
|
if(permission == LocationPermission.always )
|
||||||
await Geolocator.requestPermission();
|
await Geolocator.requestPermission();
|
||||||
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
|
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
|
||||||
|
|
||||||
|
_mapController.move(LatLng(position.latitude, position.longitude), 10);
|
||||||
setState(() {
|
setState(() {
|
||||||
_currentPosition = LatLng(position.latitude, position.longitude);
|
_userPosition = LatLng(position.latitude, position.longitude);
|
||||||
_zoom = 5.0;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return SideMenu(
|
return SideMenu(
|
||||||
@ -254,6 +270,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
body: Stack(
|
body: Stack(
|
||||||
children: [
|
children: [
|
||||||
FlutterMap(
|
FlutterMap(
|
||||||
|
mapController: _mapController,
|
||||||
options: MapOptions(
|
options: MapOptions(
|
||||||
initialCenter: _currentPosition,
|
initialCenter: _currentPosition,
|
||||||
initialZoom: _zoom,
|
initialZoom: _zoom,
|
||||||
@ -276,14 +293,34 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
if(_userPosition != null)
|
||||||
Marker(
|
Marker(
|
||||||
point: _currentPosition,
|
point: _userPosition!,
|
||||||
width: 20,
|
width: 20,
|
||||||
height: 20,
|
height: 20,
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
child: const Icon(Icons.circle_sharp, size: 15, color: Colors.blueAccent),
|
child: const Icon(Icons.my_location, size: 15, color: Colors.blueAccent),
|
||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
|
..._searchResults.map((point) => MarkerLayer(
|
||||||
|
markers: [
|
||||||
|
Marker(
|
||||||
|
point: point.location,
|
||||||
|
width: 30,
|
||||||
|
height: 50,
|
||||||
|
alignment: Alignment.center,
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.location_pin, size: 30, color: Colors.red),
|
||||||
|
onPressed: () => _showLocation(point.location, point.name, point.description),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)),
|
||||||
..._favorites.map((favorite) => MarkerLayer(
|
..._favorites.map((favorite) => MarkerLayer(
|
||||||
markers: [
|
markers: [
|
||||||
Marker(
|
Marker(
|
||||||
@ -326,6 +363,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
controller: searchBarInput,
|
controller: searchBarInput,
|
||||||
suffixMode: OverlayVisibilityMode.never,
|
suffixMode: OverlayVisibilityMode.never,
|
||||||
backgroundColor: Colors.white.withOpacity(0.9),
|
backgroundColor: Colors.white.withOpacity(0.9),
|
||||||
|
placeholder: 'Search city based',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -349,7 +387,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
right: 2, // Position the button at the bottom-right
|
right: 2,
|
||||||
bottom: 40,
|
bottom: 40,
|
||||||
child:
|
child:
|
||||||
SizedBox(
|
SizedBox(
|
||||||
@ -359,26 +397,22 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
children: [
|
children: [
|
||||||
const Icon(
|
const Icon(
|
||||||
Icons.circle, // Background circle icon
|
Icons.circle,
|
||||||
color: Colors.white, // Set the color of the circle
|
color: Colors.white,
|
||||||
size: 48.0, // Adjust the size of the circle
|
size: 48.0,
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(
|
icon: const Icon(
|
||||||
Icons.my_location, // The main location icon
|
Icons.location_searching,
|
||||||
color: Colors.blue, // Customize the color
|
color: Colors.blue,
|
||||||
size: 36.0, // Customize the size
|
size: 36.0,
|
||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
// Your logic to get the current location goes here
|
|
||||||
_getCurrentLocation();
|
_getCurrentLocation();
|
||||||
},
|
},
|
||||||
tooltip: 'Get Current Location', // Tooltip for the button
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import 'package:latlong2/latlong.dart';
|
||||||
|
|
||||||
class Favorite {
|
class Favorite {
|
||||||
int id;
|
int id;
|
||||||
String userId;
|
String userId;
|
||||||
@ -51,3 +53,11 @@ class User {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SearchResults{
|
||||||
|
LatLng location;
|
||||||
|
String name;
|
||||||
|
String description;
|
||||||
|
|
||||||
|
SearchResults(this.location, this.name, this.description);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user