如何在Flutter中限制Google Map的边界?

时间:2019-10-30 14:24:35

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

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

我已经尝试了下面的代码,但是没有用。

uniCampusSW和uniCampusNE都是LatLng。

_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):   'package:google_maps_flutter / src / location.dart':断言失败:   第68行pos 16:I / flutter(12525):'southwest.latitude <=   northeast.latitude':不正确。

谢谢

2 个答案:

答案 0 :(得分:0)

我对这个很愚蠢。

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

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

答案 1 :(得分:0)

试试这个:

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

}