Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Android beta 6 : LatLngBounds.Builder() #11629

Closed
Etienne-io opened this issue Apr 9, 2018 · 4 comments
Closed

Android beta 6 : LatLngBounds.Builder() #11629

Etienne-io opened this issue Apr 9, 2018 · 4 comments
Assignees
Labels
Android Mapbox Maps SDK for Android

Comments

@Etienne-io
Copy link

Etienne-io commented Apr 9, 2018

Platform: Android
Mapbox SDK version: 6.0.0-beta.6

Steps to trigger behaviour

  1. Create Polygon that is a Rectangle
  2. Add each coordinate of this Polygon using LatLngBounds.Builder().include(latLng)
  3. Display a line from south west to north east

Expected behavior

The line should be a diagonale of the original rectangle.

Actual behavior

The line seems to be the left edge of the original rectangle

Exemple :

Original polygon :
screen shot 2018-04-09 at 12 38 11

Computed bounds :
screen shot 2018-04-09 at 12 45 43

Note : I have no issue with non-rectangle polygon

It works on Android 5.5.0. The problem seems to come from : e4a40fe#diff-24a1903a5def25e6a975df6b3ea38b40

@tobrun tobrun added the Android Mapbox Maps SDK for Android label Apr 9, 2018
@tobrun tobrun added this to the android-v6.0.0 milestone Apr 9, 2018
@tobrun
Copy link
Member

tobrun commented Apr 9, 2018

cc @osana

@osana osana self-assigned this Apr 9, 2018
@osana
Copy link
Contributor

osana commented Apr 10, 2018

Here is a code snippet that I tried and it works as expected:

    LatLngBounds visibleBounds = mapboxMap.getProjection().getVisibleRegion().latLngBounds;
    LatLng northEast = visibleBounds.getNorthEast();
    LatLng southWest = visibleBounds.getSouthWest();

    final double delta = 0.01;

    LatLngBounds newBounds =
      new LatLngBounds.Builder()
        .include(new LatLng(northEast.getLatitude() - delta,
          northEast.getLongitude() - delta))
        .include(new LatLng(southWest.getLatitude() + delta,
          southWest.getLongitude() + delta))
        .build();

    // add rectangle to a map
    PolygonOptions boundsArea = new PolygonOptions()
      .add(newBounds.getNorthWest())
      .add(newBounds.getNorthEast())
      .add(newBounds.getSouthEast())
      .add(newBounds.getSouthWest());
    boundsArea.alpha(0.25f);
    boundsArea.fillColor(Color.RED);
    mapboxMap.addPolygon(boundsArea);
    
    // draw rectangle's diagonal
    PolylineOptions diagonal = new PolylineOptions()
      .add(newBounds.getNorthEast())
      .add(newBounds.getSouthWest());
    diagonal.color(Color.BLUE);
    diagonal.width(4);
    mapboxMap.addPolyline(diagonal);

I tried rotating the map and doing and diagonal was drawn from northEast to souWest corners.

Could you provide us with a code snippet of what is not working. Thank you.

cc @tobrun

@Etienne-io
Copy link
Author

You can try this code :

PolygonOptions opts = new PolygonOptions()
                        .add(new LatLng(50.63018102643986, 3.0535175809382062))
                        .add(new LatLng(50.63073906475279, 3.0535175809382062))
                        .add(new LatLng(50.63073906475279, 3.054754028380558))
                        .add(new LatLng(50.63018102643986, 3.054754028380558))
                        .add(new LatLng(50.63018102643986, 3.0535175809382062))
                        .fillColor(Color.GREEN)
                        .alpha(0.4f);
                mapboxMap.addPolygon(opts);

                LatLngBounds llb = new LatLngBounds.Builder()
                        .include(new LatLng(50.63018102643986, 3.0535175809382062))
                        .include(new LatLng(50.63073906475279, 3.0535175809382062))
                        .include(new LatLng(50.63073906475279, 3.054754028380558))
                        .include(new LatLng(50.63018102643986, 3.054754028380558))
                        .include(new LatLng(50.63018102643986, 3.0535175809382062))
                        .build();
                PolylineOptions plopts = new PolylineOptions()
                        .add(llb.getSouthWest())
                        .add(llb.getNorthEast())
                        .color(Color.RED);
                mapboxMap.addPolyline(plopts);

                mapboxMap.setCameraPosition(new CameraPosition.Builder().target(new LatLng(50.63018102643986, 3.0535175809382062)).zoom(18).build());

If you want funny behaviour, replace the last line with :

mapboxMap.setCameraPosition(new CameraPosition.Builder().target(llb.getCenter()).build());

@osana
Copy link
Contributor

osana commented Apr 10, 2018

The bug is when the first two points have the same longitude.

Fails when points are passed to the Builder in the order like:
sw, nw, ne, se
nw, sw, ne, se
ne, se, sw, nw
se, ne, sw, nw

I will add unit tests with first two points having the same longitude and another test with first two points having the same latitude.

cc @tobrun

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

3 participants