【问题标题】:How do I restrict the bounds of a Google Map in Flutter?如何在 Flutter 中限制谷歌地图的边界?
【发布时间】:2020-02-25 21:08:12
【问题描述】:

我正在使用 google_maps_flutter 包,我需要将地图的可滚动区域限制为特定区域。我有西南角和东北角,但我不知道怎么做。

我已经尝试了下面的代码,但它不起作用。

uniCampusSW 和 uniCampusNE 都是 LatLngs。

_userLocation == null // If user location has not been found
          ? Center(
              // Display Progress Indicator
              child: CircularProgressIndicator(
                backgroundColor: UniColors.primaryColour[500],
              ),
            )
          : GoogleMap(
              // Show Campus Map
              onMapCreated: _onMapCreated,
              initialCameraPosition: // required parameter that sets the starting camera position. Camera position describes which part of the world you want the map to point at.
                  CameraPosition(
                      target: _userLocation, zoom: defaultZoom, tilt: 0.0),
              scrollGesturesEnabled: true,
              tiltGesturesEnabled: true,
              trafficEnabled: false,
              compassEnabled: true,
              rotateGesturesEnabled: true,
              myLocationEnabled: true,
              mapType: _currentMapType,
              zoomGesturesEnabled: true,
              cameraTargetBounds: new CameraTargetBounds(
                new LatLngBounds(
                  northeast: UniCampusNE,
                  southwest: UniCampusSW,
                ),
              ),
            ),

这是我得到的错误

/flutter (12525): 在构建 TheMap(dirty, state: _TheMapState#a8840): I/flutter (12525): “包:google_maps_flutter/src/location.dart”:断言失败: 第 68 行 pos 16: I/flutter (12525): 'southwest.latitude

谢谢

【问题讨论】:

    标签: google-maps flutter dart google-maps-flutter


    【解决方案1】:

    朋友们好!

    零安全解决方案

    注意错误:'southwest.latitude

    根据文档,链接如下:

    https://pub.dev/documentation/google_maps_flutter_platform_interface/2.1.2/google_maps_flutter_platform_interface/LatLngBounds-class.html

    需要检查这些值是否不是静态的,因为s西南.纬度必须小于或等于东北.纬度,东北.经度必须小于或等于西南.经度。

    LatLng? UniCampusNE;
    LatLng? UniCampusSW;
    
    var nLat, nLon, sLat, sLon;
    
    if (UniCampusSW!.latitude <= UniCampusNE!.latitude) {
    
      sLat = UniCampusSW.latitude;
      nLat = UniCampusNE.latitude;
    
    }else{
    
      sLat = UniCampusNE.latitude;
      nLat = UniCampusSW.latitude;
    
    }
    if (UniCampusSW.longitude <= UniCampusNE.longitude) {
    
      sLon = UniCampusSW.longitude;
      nLon = UniCampusNE.longitude;
    
    }else{
    
      sLon = UniCampusNE.longitude;
      nLon = UniCampusSW.longitude;
    }
    

    并且在cameraTargetBounds:你可以使用你创建的变量:

    cameraTargetBounds: CameraTargetBounds(
    LatLngBounds(
    northeast: LatLng(nLat, nLon),
    southwest: LatLng(sLat, sLon),
    ),
    

    【讨论】:

      【解决方案2】:

      试试这个:

      void _onMapCreated(GoogleMapController controller) {
      _controller.complete(controller);
      Future.delayed(
          Duration(milliseconds: 200),
          () => controller.animateCamera(CameraUpdate.newLatLngBounds(
              northeast,southwest,
              1)));
      

      }

      【讨论】:

        【解决方案3】:

        我对这个很愚蠢。

        我在 UniCampusNE 和 SW 的 LatLngs 中的 Lat 是错误的。就那么简单。我发布的代码是正确的。

        您必须确保西南纬度小于(更左)东北纬度。

        【讨论】:

        • 如何获得界限?
        • 嗨@HassanAnsari - 我希望你一切都好。只需访问 Google 地图 (maps.google.com/maps),导航到您要使用的位置,然后右键单击特定位置。您应该看到一个包含坐标的弹出菜单。我希望这会有所帮助。
        • 那是说我得分开拿co或捐赠?
        • 基本上是的 - 获取您的坐标,然后它们使用上面的代码实现边界。确保你也注意接受的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多