import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; class TestMap extends StatefulWidget { const TestMap({super.key}); @override State createState() => _TestMapState(); } class _TestMapState extends State { /// Determine the current position of the device. /// /// When the location services are not enabled or permissions /// are denied the `Future` will return an error. Future> determinePosition() async { bool serviceEnabled; LocationPermission permission; // Test if location services are enabled. serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // Location services are not enabled don't continue // accessing the position and request users of the // App to enable the location services. return Future.error('Location services are disabled.'); } permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Permissions are denied, next time you could try // requesting permissions again (this is also where // Android's shouldShowRequestPermissionRationale // returned true. According to Android guidelines // your App should show an explanatory UI now. // return Future.error('Location permissions are denied'); return { "message": "Ijinkan Lokasi di HP", "error": true, }; } } if (permission == LocationPermission.deniedForever) { // Permissions are denied forever, handle appropriately. // return Future.error( // 'Location permissions are permanently denied, we cannot request permissions.'); return { "message": "Ijinkan Lokasi di HP", "error": true, }; } // When we reach here, permissions are granted and we can // continue accessing the position of the device. Position position = await Geolocator.getCurrentPosition(); return { "position": position, "message": "Berhasil mendapatkan posisi", "error": false }; } String latitude = ""; String longitude = ""; String addressFromCoordinat = ""; String jarak = ""; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Test Map'), ), body: SafeArea( child: Padding( padding: const EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ (latitude.isNotEmpty && longitude.isNotEmpty) ? Text('Coordinat Akhir: $latitude , $longitude') : SizedBox.shrink(), (addressFromCoordinat.isNotEmpty) ? Text('Address : $addressFromCoordinat') : SizedBox.shrink(), (jarak.isNotEmpty) ? Text('Jarak : $jarak') : SizedBox.shrink(), SizedBox( height: 10, ), Text('SFA Klodran : -7.5350973, 110.7921524'), SizedBox( height: 10, ), ElevatedButton( child: Text('Determine Position'), onPressed: () async { print('ABSENSI'); Map posx = await determinePosition(); print('${posx}'); // if (posx['error'] == false) { // Position position = posx['position']; // List placemarks = await placemarkFromCoordinates( // position.latitude, position.longitude); // print('placemark : ${placemarks}'); // double jarakx = await Geolocator.distanceBetween(-7.5350973, // 110.7921524, position.latitude, position.longitude); // setState(() { // jarak = jarakx.toString() + " meter"; // longitude = position.longitude.toString(); // latitude = position.latitude.toString(); // addressFromCoordinat = placemarks[0].street.toString() + // " , " + // placemarks[0].subLocality.toString() + // " , " + // placemarks[0].locality.toString() + // " , " + // placemarks[0].postalCode.toString() + // " , " + // placemarks[0].country.toString(); // }); // } else { // setState(() { // longitude = ""; // addressFromCoordinat = ""; // jarak = ""; // latitude = posx['message'].toString(); // }); // } }, ), SizedBox( height: 20, ), ElevatedButton( onPressed: () { setState(() { latitude = ""; longitude = ""; addressFromCoordinat = ""; jarak = ""; }); }, child: Text('Clear'), ), ], ), ), ), ); } }