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

Commit

Permalink
Don't wrap LatLngBounds (#13540)
Browse files Browse the repository at this point in the history
* [android] - don't wrap LatLngBounds

* Update GeometryConstants.java

* Update NativeMapTest
  • Loading branch information
osana committed Dec 12, 2018
1 parent 3810e7b commit 4c45123
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class GeometryConstants {
/**
* This constant represents the lowest longitude value available to represent a geolocation.
*/
public static final double MIN_LONGITUDE = Double.NEGATIVE_INFINITY;
public static final double MIN_LONGITUDE = -Double.MAX_VALUE;

/**
* This constant represents the highest longitude value available to represent a geolocation.
*/
public static final double MAX_LONGITUDE = Double.POSITIVE_INFINITY;
public static final double MAX_LONGITUDE = Double.MAX_VALUE;

/**
* This constant represents the lowest latitude value available to represent a geolocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public LatLng[] toLatLngs() {
* otherwise IllegalArgumentException will be thrown.
* latNorth should be greater or equal latSouth, otherwise IllegalArgumentException will be thrown.
* <p>
* This method doesn't recalculate most east or most west boundaries.
* Note @since 7.0.0 lonEast and lonWest will NOT be wrapped to be in the range of [-180, 180],
* see {@link GeometryConstants#MIN_LONGITUDE} and {@link GeometryConstants#MAX_LONGITUDE}
* lonEast should be greater or equal lonWest, otherwise IllegalArgumentException will be thrown.
Expand Down Expand Up @@ -286,6 +287,10 @@ private static void checkParams(
throw new IllegalArgumentException("longitude must not be NaN");
}

if (Double.isInfinite(lonEast) || Double.isInfinite(lonWest)) {
throw new IllegalArgumentException("longitude must not be infinite");
}

if (latNorth > MAX_LATITUDE || latNorth < MIN_LATITUDE
|| latSouth > MAX_LATITUDE || latSouth < MIN_LATITUDE) {
throw new IllegalArgumentException("latitude must be between -90 and 90");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ public void includesOverDateline1() {
.include(new LatLng(0, -190))
.build();

LatLngSpan latLngSpan = latLngBounds.getSpan();
assertEquals("LatLngSpan should be the same",
new LatLngSpan(20, 20), latLngBounds.getSpan());
new LatLngSpan(20, 20), latLngSpan);
}

@Test
Expand Down Expand Up @@ -395,14 +396,20 @@ public void intersectSouthCheck() {
public void intersectSouthLessThanNorthCheck() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latNorth cannot be less than latSouth");

LatLngBounds intersectLatLngBounds =
LatLngBounds.from(10, 10, 0, 0)
.intersect(0, 200, 20, 0);
}


@Test
public void intersectEastLessThanWestCheck() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("lonEast cannot be less than lonWest");
LatLngBounds intersectLatLngBounds =
LatLngBounds.from(10, -10, 0, 0)
.intersect(0, 200, 20, 0);
}

public void intersectEastDoesNotWrapCheck() {

LatLngBounds latLngBounds1 = LatLngBounds.from(10, 210, 0, 0);
Expand Down Expand Up @@ -749,10 +756,10 @@ public void testConstructorChecksNorthLatitudeLessThanThanNegative90() {
}

@Test
public void testConstructorEastLongitudeInfinityAllowed() {
LatLngBounds latLngBounds =
LatLngBounds.from(0, Double.POSITIVE_INFINITY, -20, -20);
assertEquals(Double.POSITIVE_INFINITY, latLngBounds.getLonEast(), DELTA);
public void testConstructorChecksEastLongitudeInfinity() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be infinite");
LatLngBounds.from(0, Double.POSITIVE_INFINITY, -20, -20);
}

@Test
Expand Down Expand Up @@ -784,10 +791,10 @@ public void testConstructorChecksSouthLatitudeLessThanThanNegative90() {
}

@Test
public void testConstructorWestLongitudeInfinityAllowed() {
LatLngBounds latLngBounds =
LatLngBounds.from(20, 20, 0, Double.NEGATIVE_INFINITY);
assertEquals(Double.NEGATIVE_INFINITY, latLngBounds.getLonWest(), DELTA);
public void testConstructorChecksWestLongitudeInfinity() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be infinite");
LatLngBounds.from(20, 20, 0, Double.POSITIVE_INFINITY);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ class NativeMapViewTest {
fun testLatLngForProjectedMeters() {
val expected = LatLng(0.01796630538796444, 0.02694945852363162)
val actual = nativeMapView.latLngForProjectedMeters(ProjectedMeters(2000.0, 3000.0))
assertEquals("Get LatLng for projected meters", expected, actual)
assertEquals("Lat for projected meters", expected.latitude, actual.latitude, DELTA)
assertEquals("Lng for projected meters", expected.longitude, actual.longitude, DELTA)
}

@Test
Expand Down Expand Up @@ -306,7 +307,7 @@ class NativeMapViewTest {
.bearing(0.0)
.build()
val actual = nativeMapView.getCameraForLatLngBounds(
LatLngBounds.from(30.0, 12.0, 16.0, 16.0),
LatLngBounds.from(30.0, 16.0, 16.0, 12.0),
intArrayOf(0, 0, 0, 0),
0.0,
0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.feature.QueryRenderedFeaturesBoxHighlightActivity;

import com.mapbox.mapboxsdk.testapp.utils.TestConstants;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
Expand All @@ -18,7 +19,7 @@
import com.mapbox.mapboxsdk.testapp.R;

/**
* Test activity showcasing restricting user gestures to a bounds around Iceland.
* Test activity showcasing restricting user gestures to a bounds around Iceland, almost worldview and IDL.
*/
public class LatLngBoundsForCameraActivity extends AppCompatActivity implements OnMapReadyCallback {

Expand All @@ -27,6 +28,16 @@ public class LatLngBoundsForCameraActivity extends AppCompatActivity implements
.include(new LatLng(62.985661, -12.626277))
.build();

private static final LatLngBounds ALMOST_WORLD_BOUNDS = new LatLngBounds.Builder()
.include(new LatLng(20.0, 170.0))
.include(new LatLng(-20, -170.0))
.build();

private static final LatLngBounds CROSS_IDL_BOUNDS = new LatLngBounds.Builder()
.include(new LatLng(20.0, 170.0))
.include(new LatLng(-20, 190.0))
.build();

private MapView mapView;
private MapboxMap mapboxMap;

Expand All @@ -35,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restricted_bounds);

mapView = (MapView) findViewById(R.id.mapView);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
Expand All @@ -44,18 +55,43 @@ protected void onCreate(Bundle savedInstanceState) {
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.SATELLITE_STREETS);
mapboxMap.setLatLngBoundsForCameraTarget(ICELAND_BOUNDS);
mapboxMap.setMinZoomPreference(2);
showBoundsArea();
mapboxMap.getUiSettings().setFlingVelocityAnimationEnabled(false);
showCrosshair();
setupBounds(ICELAND_BOUNDS);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bounds, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_allmost_world_bounds:
setupBounds(ALMOST_WORLD_BOUNDS);
return true;
case R.id.menu_action_cross_idl:
setupBounds(CROSS_IDL_BOUNDS);
return true;
}
return super.onOptionsItemSelected(item);
}

private void setupBounds(LatLngBounds bounds) {
mapboxMap.setLatLngBoundsForCameraTarget(bounds);
showBoundsArea(bounds);
}

private void showBoundsArea() {
private void showBoundsArea(LatLngBounds bounds) {
mapboxMap.clear();
PolygonOptions boundsArea = new PolygonOptions()
.add(ICELAND_BOUNDS.getNorthWest())
.add(ICELAND_BOUNDS.getNorthEast())
.add(ICELAND_BOUNDS.getSouthEast())
.add(ICELAND_BOUNDS.getSouthWest());
.add(bounds.getNorthWest())
.add(bounds.getNorthEast())
.add(bounds.getSouthEast())
.add(bounds.getSouthWest());
boundsArea.alpha(0.25f);
boundsArea.fillColor(Color.RED);
mapboxMap.addPolygon(boundsArea);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_action_allmost_world_bounds"
android:title="@string/restrict_almost_worldview"
app:showAsAction="never"/>
<item
android:id="@+id/menu_action_cross_idl"
android:title="@string/restrict_across_idl"
app:showAsAction="never"/>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@
<string name="zoom_by_2">Zoom by 2</string>
<string name="zoom_to_point">Zoom to point</string>
<string name="zoom_to_4">Zoom to 4</string>
<string name="restrict_almost_worldview">Restrict almost worldview</string>
<string name="restrict_across_idl">Restrict across IDL</string>
</resources>

0 comments on commit 4c45123

Please sign in to comment.