google-mapsflutterdartgoogle-maps-flutter

How do I restrict the bounds of a Google Map in Flutter?


I am using google_maps_flutter package and I need to restrict the scrollable area of a map to a specific area. I have both the SW and NE corners, but I can't figure out how to do it.

I have tried the code below, but it's not working.

uniCampusSW and uniCampusNE are both 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,
                ),
              ),
            ),

This is the error I get

/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.

thanks


Solution

  • I was being stupid with this one.

    The Lats in the LatLngs I had in UniCampusNE and SW were the wrong way round. Simple as that. The code I posted is correct.

    You have to make sure that the south west latitude is less than (further left) than the north east latitude.