-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically calculate map bounds from a list of LatLng
values
#36653
Comments
I am having the same issue, I want to build the bounds from a couple of Latitude and Longitude Coordinates but I am unable to find how to implement this in FLutter with the classes we have been given. Any ideas? |
same issue. We need to calculate the zoom and bounds of the map between 2 markers |
@unamed000 I was able to solve this by adding this function and using it to calculate
|
Hi @adim86, Edit: Thanks adim,
|
While @adim86's solution works like a charm, I still believe it should be part of the standard API to maintain feature parity with the Google Maps JS SDK. Additionally, LatLngBounds should have a .getCenter() method like its JS counterpart does. For me this would be useful as I'm trying to make the map show all markers at once, so I need to make a bounds that includes every marker and also dispatch a camera update that sets the target and zoom to adequate values such that the map shows every marker. |
I also need this feature, but sadly don't have the time to help in development. |
LatLng
values
@adim86 thank youuuuuuuu |
@Nexuist Yeah that is actually somehting that I would like to achieve as well, did you manage to get something like that working by now? |
@DanielFortuyn Yes, I did using some custom code mixed and matched from SO: LatLngBounds boundsFromLatLngList(List<LatLng> list) {
double x0, x1, y0, y1;
for (LatLng latLng in list) {
if (x0 == 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));
}
void _onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
Future.delayed(
Duration(milliseconds: 200),
() => controller.animateCamera(CameraUpdate.newLatLngBounds(
boundsFromLatLngList(
widget.locations.values.map((loc) => loc.asLatLng()).toList()),
1)));
}
|
@Nexuist Thanks for the
even though the map should be all ready in |
Google team committed a getVisibleRegion method, would that be the way forward? #25755 |
@Nexuist how did you get the appropriate zoom level though? Nvm, it auto zooms. Unfortunate that you can't set it in the initializer which what was broken for me. Whats the point of |
One can use the LatLngBounds.fromPoints(points) built-in method if using the flutter_map library. |
Here is a re-written version of @Nexuist suggestion as an extension function to extension LatLngBoundsExtension on LatLngBounds {
static fromLatLng(List<LatLng> latLngList) {
assert(latLngList.isNotEmpty);
double? x0, x1, y0, y1;
for (LatLng latLng in latLngList) {
if (x0 == 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!));
}
} Usage: LatLngBoundsExtension.fromLatLng(markers.map((e) => e.position).toList()), 1) |
Null-safe version of @adim86 solution : 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(LatLng(x1!, y1!), LatLng(x0!, y0!));
} |
So I am trying to implement exactly this right now. Fortunately stumbled on this issue here. Thanks @adim86 for this. Works perfectly |
Just read the source code. Cannot find a method to build a LatLngBounds from a list of LatLng.
https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLngBounds.Builder
The text was updated successfully, but these errors were encountered: