I am using Google Maps, and want to be able to detect if a user is in the rectangle of a Marker (placed on a map), using the user's current location. how i can get the hole rectangle coordinates and check whether the my user are in the specific rectangle are not i am using it for Attendance project by the way i am using flutter for android app
Asked
Active
Viewed 745 times
0
-
you mean is you draw some line on map through coordiante and when some user came in the specfic coordinate then you triger some thing ..like his attendence automatically going on server..? or please correct me if i'm wrong – benten Oct 04 '22 at 07:13
-
yes I want to draw a rectangle around a hospital or school building and when students/Doctor come to that building they can mark his attendance or may be automatically and I ill do some more stuff – Said Hussain Oct 04 '22 at 12:22
-
Other possible duplicates: [Check if a coordinate is inside a polygon in flutter using google_maps_flutter](https://stackoverflow.com/questions/61086855/check-if-a-coordinate-is-inside-a-polygon-in-flutter-using-google-maps-flutter), [Check if the point is within the Google Maps polygon in flutter](https://stackoverflow.com/questions/67462271/check-if-the-point-is-within-the-google-maps-polygon-in-flutter) – Yrll Oct 05 '22 at 23:39
1 Answers
1
here is the solution Use this package
maps_toolkit: ^2.0.0
first make your googleMap Class like this
import 'package:maps_toolkit/maps_toolkit.dart'
class _GoogleMapsWidgetState extends State<GoogleMapsWidget> {
var latitude;
var longitude;
Set<Polygon> _polygons = HashSet<Polygon>();
@override
void initState() {
polygoan();
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
print("init map");
return new Scaffold(
body: Stack(
children: [
GoogleMap(
// circles: circles,
polygons: _polygons,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
new Factory<OneSequenceGestureRecognizer>(() => new
EagerGestureRecognizer(),),
].toSet(),
cameraMove: (position) {
latitude = position.target.latitude;
longitude = position.target.longitude;
},
cameraIdle:() async {
if (latitude != null && longitude != null) {
distanceBetweenPoints = PolygonUtil.containsLocation(LatLng(latitude, longitude),
[
LatLng(24.8671979,66.9685133),
LatLng(24.8509609,66.7337653),
LatLng(24.9009609,66.7337653),
LatLng(24.9209609,66.7337653),
LatLng(24.9734838,66.9055773),
LatLng(25.0585608,67.0920403),
LatLng(24.9809088,67.2108533),
LatLng(25.0461688,67.2740133),
LatLng(25.1631338,67.3294933),
LatLng(25.153956, 67.365219),
LatLng(24.9961038,67.3152423),
//bahria town
LatLng(24.9669878,67.2399053),
LatLng(24.8771229,67.1956623),
LatLng(24.8168969,67.2279713),
LatLng(24.8009849,67.1316243),
LatLng(24.7835109,67.1362163),
LatLng(24.7791219,67.1220993),
LatLng(24.8292699,67.0936673),
LatLng(24.8498649,67.0938363),
LatLng(24.8451169,67.0843003),
LatLng(24.8206559,67.0833513),
LatLng(24.8031969,67.0757973),
LatLng(24.7933619,67.0775643),
LatLng(24.7608529,67.1010113),
LatLng(24.7490799,67.0773103),
LatLng(24.7967999,67.0314263),
],false
);
//now here you can handle the app if he is in the given coordinate or not
if(distanceBetweenPoints == true)
{
// if he is in the region
}
else
{
}
}
},
onMapCreated: (GoogleMapController controller) {
print("after Map");
// controller.setMapStyle(
// );
widget.completer.complete(controller);
},
),
],
));
}
void polygoan() {
_polygons.add(Polygon(
fillColor: Colors.transparent,
polygonId: PolygonId('polygonId'),
points: [
LatLng(24.8671979, 66.9685133),
LatLng(24.8509609, 66.7337653),
LatLng(24.9009609, 66.7337653),
LatLng(24.9209609, 66.7337653),
LatLng(24.9734838, 66.9055773),
LatLng(25.0585608, 67.0920403),
LatLng(24.9809088, 67.2108533),
LatLng(25.0461688, 67.2740133),
LatLng(25.1631338, 67.3294933),
LatLng(25.153956, 67.365219),
LatLng(24.9961038, 67.3152423),
//bahria town
LatLng(24.9669878, 67.2399053),
LatLng(24.8771229, 67.1956623),
LatLng(24.8168969, 67.2279713),
LatLng(24.8009849, 67.1316243),
LatLng(24.7835109, 67.1362163),
LatLng(24.7791219, 67.1220993),
LatLng(24.8292699, 67.0936673),
LatLng(24.8498649, 67.0938363),
LatLng(24.8451169, 67.0843003),
LatLng(24.8206559, 67.0833513),
LatLng(24.8031969, 67.0757973),
LatLng(24.7933619, 67.0775643),
LatLng(24.7608529, 67.1010113),
LatLng(24.7490799, 67.0773103),
LatLng(24.7967999, 67.0314263),
],
strokeWidth: 3,
strokeColor: Colors.redAccent,
));
}
}
hope this will answer your question..it its help you mark this answer as marked to help other

benten
- 696
- 7
- 18
-
-
-
But again this will check for the complete coordinate match. If there slightly difference in coordinates of user's location, It will return false. Let me know If I am wrong. – Jaimin Modi Nov 05 '22 at 10:57
-
It will just check that you're in the given coordinate or not. and just one thing these given coordinate make a boundary wall – benten Nov 06 '22 at 14:34