maplibre / flutter-maplibre-gl

Customizable, performant and vendor-free vector and raster maps, flutter wrapper for maplibre-native and maplibre-gl-js (fork of flutter-mapbox-gl/maps)
https://pub.dev/packages/maplibre_gl
Other
226 stars 125 forks source link

How to extend LatLngBounds #312

Closed imerzi closed 1 year ago

imerzi commented 1 year ago

Hi, i'm trying to extend the bounds to include a given LngLatLike or LngLatBoundsLike. But there is no extends in the LatLngBounds class.

Is there another way of doing this ?

m0nac0 commented 1 year ago

I don't quite understand what you are trying to achieve. Could you elaborate a little more on your motivation and maybe share some sample code of what you want to do?

imerzi commented 1 year ago

Here is an example

    for (final IsarAudit audit in audits) {
        final Map<String, dynamic> auditGeom = await AuditEntityApiService.getGeoJson(ref, audit.id);

        await mapController!.addGeoJsonSource(audit.id, auditGeom);
        await mapController!.addCircleLayer(
          audit.id,
          'circle-${audit.id}',
          CircleLayerProperties(
            circleRadius: 8,
            circleColor: Colors.blue.toHexStringRGB(),
          ),
        );

        await mapController!.setCameraBounds(
          west: (auditGeom['bbox'] as List<dynamic>)[0] as double,
          south: (auditGeom['bbox'] as List<dynamic>)[3] as double,
          east: (auditGeom['bbox'] as List<dynamic>)[2] as double,
          north: (auditGeom['bbox'] as List<dynamic>)[1] as double,
          padding: 100,
        );
      }

I'm going through several geom and I want to extend the bounds each time to match all my geom I know there is the extend method in maplibre-gl in typescript

/**
     * Extend the bounds to include a given LngLatLike or LngLatBoundsLike.
     *
     * @param obj - object to extend to
     * @returns `this`
     */
    extend(obj: LngLatLike | LngLatBoundsLike): this;

Is there a way to do this ?

imerzi commented 1 year ago

For now i'm implementing a way to extends bounds like this with a bbox.

  LatLngBounds _extendBounds(List<String> bboxList) {
    double minLat = double.infinity;
    double minLng = double.infinity;
    double maxLat = -double.infinity;
    double maxLng = -double.infinity;

    for (final String bboxString in bboxList) {
      final List<String> bboxCoordinates = bboxString.split(',');

      if (bboxCoordinates.length == 4) {
        final double bboxMinLng = double.parse(bboxCoordinates[0]);
        final double bboxMinLat = double.parse(bboxCoordinates[1]);
        final double bboxMaxLng = double.parse(bboxCoordinates[2]);
        final double bboxMaxLat = double.parse(bboxCoordinates[3]);

        if (bboxMinLng < minLng) minLng = bboxMinLng;
        if (bboxMinLat < minLat) minLat = bboxMinLat;
        if (bboxMaxLng > maxLng) maxLng = bboxMaxLng;
        if (bboxMaxLat > maxLat) maxLat = bboxMaxLat;
      }
    }

    return LatLngBounds(
      southwest: LatLng(minLat, minLng),
      northeast: LatLng(maxLat, maxLng),
    );
  }
m0nac0 commented 1 year ago

Ah, got it. That is not currently supported. If you want to open a PR, we could review it. I assume the tricky parts would be edge cases where you have to consider how exactly you want the implementation to behave. E.g. should it always return the minimal possible bounding box, even if that e.g. crosses the antimeridian etc.