Search

Google Maps Flutter & Geolocator

Permission Setup

Check Permission

Future<String> checkPermission() async { // <Generic> for the type of return // Location Service on or off final isLocationEnabled = await Geolocator.isLocationServiceEnabled(); // If Location Service is off if (!isLocationEnabled) { return "Activate your Location Service."; } } // Permission Status [denied, deniedForever, whileInUse, always, unableToDetermine] LocationPermission checkedPermission = await Geolocator.checkPermission(); // if denied 1st time if (checkedPermission == LocationPermission.denied) { checkedPermission == await Geolocator.requestPermission(); // denied 2nd time if (checkedPermission == LocationPermission.denied) { return "Activate your Location Service."; } } // denied forever if (checkedPermission == LocationPermission.deniedForever) { return "Activate Location Service Manually."; } // successful return return "Location Service has been activated"; }
Dart
복사

FutureBuilder

FutureBuilder<String>( // <generic> for the type of snapshot.data // parameter can be anything with Future<???> type / function want to check for update future: checkPermission(), // snapshot takes the information returned by future builder: (BuildContext context, AsyncSnapshot snapshot) { // if checkPermission() is in progress if (snapshot.connectionState == ConnectionState.waiting) { return Center( child: CircularProgressIndicator(), ); } // if checkPermission() is successfully done if (snapshot.data == "Location Service has been activated") { return // any widgets ); } // if checkPermission() has problem return Center( child: Text("${snapshot.data}"), ); }, ),
Dart
복사

Basic

static final LatLng targetLatLng = LatLng( 000.0000000, // Latitude 000.0000000, // Longtitude ); static final CameraPosition initialPosition = CameraPosition( target: targetLatLng, // LatLng zoom: 15, // camera zoom ); // widget GoogleMap( mapType: MapType.normal, initialCameraPosition: initialPosition, // define position and zoom myLocationEnabled: true, // recieve location current location info myLocationButtonEnabled: false, // remove location button circles: Set.from([circle]), // must be same with the circleId from the Circle widget markers: Set.from([marker]), // must be same with the markerId from the Marker widget ),
Dart
복사

Circle Widget

static final double distance = 100; static final Circle circle = Circle( circleId: CircleId('circle'), // unique Id for Circle widget (remove if there's duplicate) center: targetLatLng, // center position of circle fillColor: Colors.blue.withOpacity(0.5), radius: distance, // units in meters strokeColor: Colors.blue, // color for the outer border of the circle strokeWidth: 1, // units in pixels );
Dart
복사

Marker Widget

static final Marker marker = Marker( markerId: MarkerId('marker'), position: targetLatLng, );
Dart
복사

StreamBuilder

StreamBuilder<Position>( // <Generic> sets the position type of the snapshot.data stream: Geolocator.getPositionStream(), // recieves position continuosly as current position moves builder: (context, snapshot) { bool isWithingRange = false; if (snapshot.hasData) { final currentPosition = snapshot.data!; final targetPosition = targetLatLng; // get the distance between currentPosition and targetPosition final range = Geolocator.distanceBetween( currentPosition.latitude, currentPosistion.longtitude, targetPosition.latitude, targetPosition.longtitude, ); // check if current position is inside the range of a circle if(range < distance) { isWithinDistance = true; } } // snapshot.data return Column( children: [ _CustomGoogleMap( circle: isWithinRange ? withinDistanceCircle : notWithinDistanceCircle, ), // _CustomGoogleMap ], // children ), // Column } // builder ); // StreamBuilder
Dart
복사