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 { 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 _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: [ 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'), ), ), ], ), ); } }