【问题标题】:Flutter Google Map how to check if user is in marker rectangle regionFlutter Google Map如何检查用户是否在标记矩形区域中
【发布时间】:2022-10-06 16:30:35
【问题描述】:

我正在使用谷歌地图,并希望能够使用用户的当前位置检测用户是否在标记的矩形中(放置在地图上)。我如何获取孔矩形坐标并检查我的用户是否在特定的矩形中,我正在将它用于考勤项目,就像我在 Android 应用程序中使用颤振一样

【问题讨论】:

标签: flutter google-maps


【解决方案1】:

这是解决方案 使用这个包

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,
            ));
          }
        }

希望这将回答您的问题..它可以帮助您将此答案标记为已标记以帮助其他人

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 2014-04-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 2017-10-10
    相关资源
    最近更新 更多