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

Commit

Permalink
[android] - create smallest possible LatLngBounds when visible region…
Browse files Browse the repository at this point in the history
… crosses the dateline
  • Loading branch information
tobrun committed Aug 17, 2017
1 parent fb05d2e commit 830bf15
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ public LatLng[] toLatLngs() {
return new LatLng[] {getNorthEast(), getSouthWest()};
}

/**
* Constructs a LatLngBounds from doubles representing a LatLng pair.
* <p>
* This method doesn't recalculate most east or most west boundaries.
* </p>
*/
public static LatLngBounds from(double latNorth, double lonEast, double latSouth, double lonWest) {
return new LatLngBounds(latNorth, lonEast, latSouth, lonWest);
}

/**
* Constructs a LatLngBounds from current bounds with an additional latitude-longitude pair.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ public LatLng fromScreenLocation(PointF point) {
* @return The projection of the viewing frustum in its current state.
*/
public VisibleRegion getVisibleRegion() {
LatLngBounds.Builder builder = new LatLngBounds.Builder();

float left = 0;
float right = nativeMapView.getWidth();
float top = 0;
Expand All @@ -105,12 +103,13 @@ public VisibleRegion getVisibleRegion() {
LatLng bottomRight = fromScreenLocation(new PointF(right, bottom));
LatLng bottomLeft = fromScreenLocation(new PointF(left, bottom));

builder.include(topLeft)
.include(topRight)
.include(bottomRight)
.include(bottomLeft);

return new VisibleRegion(topLeft, topRight, bottomLeft, bottomRight, builder.build());
return new VisibleRegion(topLeft, topRight, bottomLeft, bottomRight,
LatLngBounds.from(
topRight.getLatitude(),
topRight.getLongitude(),
bottomLeft.getLatitude(),
bottomLeft.getLongitude())
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.mapbox.mapboxsdk.testapp.activity.maplayout;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.testapp.R;

import timber.log.Timber;

/**
* Test activity showcasing a simple MapView without any MapboxMap interaction.
*/
Expand All @@ -20,6 +27,19 @@ protected void onCreate(Bundle savedInstanceState) {

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
final Projection projection = mapboxMap.getProjection();

mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
Timber.e(projection.getVisibleRegion().toString());
}
});
}
});
}

@Override
Expand Down

0 comments on commit 830bf15

Please sign in to comment.