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

How do I restrict the bounds of a Google Map in 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): The following assertion was thrown building TheMap(dirty, state: _TheMapState#a8840): I/flutter (12525): 'package:google_maps_flutter/src/location.dart': Failed assertion: line 68 pos 16: I/flutter (12525): 'southwest.latitude <= northeast.latitude': is not true.

谢谢

我被这个弄傻了。

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

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

试试这个:

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

}

朋友们大家好!

零安全解决方案

注意错误:'southwest.latitude <= northeast.latitude':不正确。

根据文档,link 如下:

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

需要检查这些值是否不是静态的,因为southwest.latitude必须小于等于northeast.latitude,而northeast.longitude必须小于等于至 southwest.longitude.

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