step 13 : show google map flutter web & current location function
This commit is contained in:
@@ -5,6 +5,8 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../app/route.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
|
||||
import 'test_map_x.dart';
|
||||
// import '../test_map.dart';
|
||||
|
||||
// final routerProvider = Provider((_) => GlobalKey<NavigatorState>());
|
||||
@@ -63,8 +65,9 @@ class MyApp extends StatelessWidget {
|
||||
),
|
||||
|
||||
// home: TestMap(),
|
||||
initialRoute: loginRoute,
|
||||
// home: TestFlutterWebMap(),
|
||||
// initialRoute: loginRoute,
|
||||
// home: TestMapFlutterWeb(),
|
||||
home:TestMapX(),
|
||||
// initialRoute: testFlutterMapRoute,
|
||||
onGenerateRoute: AppRoute.generateRoute,
|
||||
);
|
||||
|
||||
102
lib/test_map_x.dart
Normal file
102
lib/test_map_x.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
class TestMapX extends StatefulWidget {
|
||||
@override
|
||||
_TestMapXState createState() => _TestMapXState();
|
||||
}
|
||||
|
||||
class _TestMapXState extends State<TestMapX> {
|
||||
late GoogleMapController mapController;
|
||||
late LatLng _currentPosition;
|
||||
bool _isLocationSet = false;
|
||||
|
||||
final LatLng _center = const LatLng(
|
||||
-7.566957,
|
||||
110.8080284,
|
||||
); // Koordinat awal peta
|
||||
|
||||
void _onMapCreated(GoogleMapController controller) {
|
||||
mapController = controller;
|
||||
}
|
||||
|
||||
Future<void> _getCurrentLocation() async {
|
||||
try {
|
||||
// Check if location services are enabled
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
// Location services are not enabled
|
||||
return;
|
||||
}
|
||||
|
||||
// Check permission
|
||||
LocationPermission permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission != LocationPermission.whileInUse &&
|
||||
permission != LocationPermission.always) {
|
||||
// Permissions are not granted
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Get current location
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high);
|
||||
|
||||
setState(() {
|
||||
_currentPosition = LatLng(position.latitude, position.longitude);
|
||||
_isLocationSet = true;
|
||||
});
|
||||
|
||||
// Move camera to current location
|
||||
mapController.animateCamera(
|
||||
CameraUpdate.newLatLng(_currentPosition),
|
||||
);
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Google Maps Flutter Web'),
|
||||
backgroundColor: Colors.green[700],
|
||||
),
|
||||
body: Stack(
|
||||
children: <Widget>[
|
||||
GoogleMap(
|
||||
onMapCreated: _onMapCreated,
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: _center,
|
||||
zoom: 11.0,
|
||||
),
|
||||
markers: _isLocationSet
|
||||
? {
|
||||
Marker(
|
||||
markerId: MarkerId('current_location'),
|
||||
position: _currentPosition,
|
||||
infoWindow: InfoWindow(
|
||||
title: 'Your Location ${_currentPosition.latitude.toString()} , ${_currentPosition.longitude.toString()}',
|
||||
),
|
||||
icon: BitmapDescriptor.defaultMarker, // Default marker icon
|
||||
),
|
||||
}
|
||||
: {},
|
||||
),
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
left: 20,
|
||||
child: ElevatedButton(
|
||||
onPressed: _getCurrentLocation,
|
||||
child: Text('Current Location'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user