这是解决方案
使用这个包
maps_toolkit: ^2.0.0
首先让你的 googleMap 类像这样
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,
));
}
}
希望这将回答您的问题..它可以帮助您将此答案标记为已标记以帮助其他人