Reputation: 658
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
Upvotes: 4
Views: 9752
Reputation: 948
Based on the this issue for automatically calculate map bounds
LatLngBounds boundsFromLatLngList(List<LatLng> list) {
double? x0, x1, y0, y1;
for (LatLng latLng in list) {
if (x0 == null || x1 == null || y0 == null || y1 == null) {
x0 = x1 = latLng.latitude;
y0 = y1 = latLng.longitude;
} else {
if (latLng.latitude > x1) x1 = latLng.latitude;
if (latLng.latitude < x0) x0 = latLng.latitude;
if (latLng.longitude > y1) y1 = latLng.longitude;
if (latLng.longitude < y0) y0 = latLng.longitude;
}
}
return LatLngBounds(northeast: LatLng(x1!, y1!),southwest: LatLng(x0!, y0!));
}
Upvotes: 0
Reputation: 39
Hello friends!
Solution with Null Safety
Note the error: 'southwest.latitude <= northeast.latitude': is not true.
According to documentation, link below:
It is necessary to check if these values are not static, as southwest.latitude must be less than or equal to northeast.latitude, and northeast.longitude must be less than or equal to 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;
}
And in cameraTargetBounds: you can use the variables you created:
cameraTargetBounds: CameraTargetBounds(
LatLngBounds(
northeast: LatLng(nLat, nLon),
southwest: LatLng(sLat, sLon),
),
Upvotes: 3
Reputation: 3493
try this:
void _onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
Future.delayed(
Duration(milliseconds: 200),
() => controller.animateCamera(CameraUpdate.newLatLngBounds(
northeast,southwest,
1)));
}
Upvotes: 0
Reputation: 658
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.
Upvotes: 1