Skip to content
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

Accurately determine if the user's location is within the visible map region #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ dependencies {
transitive = true;
exclude group: 'com.android.support', module: 'support-v4'
}
compile('io.ona.kujaku:utils:0.2.6-11-3') {
compile('io.ona.kujaku:utils:0.2.6-43-1') {
transitive = true;
exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-sdk'
exclude group: 'com.android.support', module: 'support-v4'
Expand Down
38 changes: 28 additions & 10 deletions library/src/main/java/io/ona/kujaku/activities/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.VisibleRegion;
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.services.commons.geojson.Feature;

import org.json.JSONArray;
Expand Down Expand Up @@ -122,7 +122,7 @@ public class MapActivity extends AppCompatActivity implements MapboxMap.OnMapCli

private Marker myLocationMarker;
private LatLng focusedLocation;
private boolean focusedOnOfMyLocation = false;
private boolean focusedOnMyLocation = false;

//Todo: Move reading data to another Thread

Expand Down Expand Up @@ -222,20 +222,18 @@ private void initMapBoxSdk(Bundle savedInstanceState, String mapBoxStylePath,

mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
public void onMapReady(final MapboxMap mapboxMap) {
//Set listener for markers
MapActivity.this.mapboxMap = mapboxMap;
mapboxMap.setOnMapClickListener(MapActivity.this);
mapboxMap.setOnScrollListener(new MapboxMap.OnScrollListener() {
@Override
public void onScroll() {
if (focusedLocation != null) {
VisibleRegion mapsVisibleRegion = MapActivity.this.mapboxMap.getProjection().getVisibleRegion();
//LatLngBounds mapBounds = LatLngBounds.from(mapsVisibleRegion.l);
//Todo: Use the actual corners instead of the smallest bounding box (latLngBounds) which are more accurate
LatLng[] mapBounds = getMapBounds(mapboxMap, mapView);

if (!CoordinateUtils.isLocationInBounds(focusedLocation, mapsVisibleRegion.latLngBounds) && focusedOnOfMyLocation) {
focusedOnOfMyLocation = false;
if (!CoordinateUtils.isLocationInBounds(focusedLocation, mapBounds[0], mapBounds[1], mapBounds[2], mapBounds[3]) && focusedOnMyLocation) {
focusedOnMyLocation = false;

// Revert the icon to the non-focused grey one
changeTargetIcon(R.drawable.ic_my_location);
Expand Down Expand Up @@ -604,7 +602,6 @@ public void onAnimationUpdate(ValueAnimator animation) {
});

valueAnimator.start();
//Todo Figure out how to handle another item being selected while this one is being animated
}

private int getScreenWidth(Activity activity) {
Expand Down Expand Up @@ -730,7 +727,7 @@ private void focusOnMyLocation(@NonNull MapboxMap mapboxMap) {
if (lastLocation != null) {
LatLng newTarget = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
focusedLocation = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
focusedOnOfMyLocation = true;
focusedOnMyLocation = true;

// Change the icon to the blue one
changeTargetIcon(R.drawable.ic_my_location_focused);
Expand Down Expand Up @@ -837,4 +834,25 @@ private void changeDrawable(@NonNull View view, int drawableId) {
}
}
}

public LatLng[] getMapBounds(MapboxMap mapboxMap, MapView mapView) {
float left = 0;
float right = mapView.getWidth();
float top = 0;
float bottom = mapView.getHeight();

Projection projection = mapboxMap.getProjection();

LatLng topLeft = projection.fromScreenLocation(new PointF(left, top));
LatLng topRight = projection.fromScreenLocation(new PointF(right, top));
LatLng bottomRight = projection.fromScreenLocation(new PointF(right, bottom));
LatLng bottomLeft = projection.fromScreenLocation(new PointF(left, bottom));

return new LatLng[]{
topLeft,
topRight,
bottomRight,
bottomLeft
};
}
}
2 changes: 1 addition & 1 deletion utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'

version '0.2.6-11-3'
version '0.2.6-43-1'
project.version = this.version

task sourceJar(type: Jar) {
Expand Down
64 changes: 37 additions & 27 deletions utils/src/main/java/io/ona/kujaku/utils/CoordinateUtils.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
package io.ona.kujaku.utils;

import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;

/**
* Created by Ephraim Kigamba - [email protected] on 19/12/2017.
*/

public class CoordinateUtils {

/**
* Checks whether the provided coordinate is within the provided Bounds.
*
* <strong>NOTE: The order in which you provide the bounds does not affect the results</strong>
*
* @param positionInQuestion
* @param myMapBounds
* @param topLeft
* @param topRight
* @param bottomLeft
* @param bottomRight
* @return
*/
public static boolean isLocationInBounds(@NonNull LatLng positionInQuestion, @NonNull LatLngBounds myMapBounds) {
return isLocationInBounds(positionInQuestion, myMapBounds.getLatNorth(), myMapBounds.getLatSouth(), myMapBounds.getLonEast(), myMapBounds.getLonWest());
public static boolean isLocationInBounds(@NonNull LatLng positionInQuestion, LatLng topLeft, LatLng topRight, LatLng bottomLeft, LatLng bottomRight) {
Point[] bounds = new Point[4];
bounds[0] = new Point(topLeft.getLongitude(), topLeft.getLatitude());
bounds[1] = new Point(topRight.getLongitude(), topRight.getLatitude());
bounds[2] = new Point(bottomLeft.getLongitude(), bottomLeft.getLatitude());
bounds[3] = new Point(bottomRight.getLongitude(), bottomRight.getLatitude());

return contains(bounds, new Point(positionInQuestion.getLongitude(), positionInQuestion.getLatitude()));
}

/**
* Checks whether the provided coordinate is within the provided Bounds.
* Return true if the given point is contained inside the boundary.
* See: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
* @param needle The point to check
* @return true if the point is inside the boundary, false otherwise
*
* @param positionInQuestion
* @param latNorth
* @param latSouth
* @param lonEast
* @param lonWest
* @return
*/
public static boolean isLocationInBounds(@NonNull LatLng positionInQuestion, double latNorth, double latSouth, double lonEast, double lonWest) {
return (positionInQuestion.getLatitude() <= latNorth
&& positionInQuestion.getLatitude() >= latSouth
&& positionInQuestion.getLongitude() <= lonEast
&& positionInQuestion.getLongitude() >= lonWest);
public static boolean contains(Point[] bounds, Point needle) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use LatLngBounds.contains()? Less code to maintain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

com.mapbox.mapboxsdk.geometry.LatLngBounds is also available, if you'd rather go the MapBox way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to test this & read up on it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both contains method are the same and inaccurate. They are not comparable

int i;
int j;
boolean result = false;
for (i = 0, j = bounds.length - 1; i < bounds.length; j = i++) {
if ((bounds[i].y > needle.y) != (bounds[j].y > needle.y) &&
(needle.x < (bounds[j].x - bounds[i].x) * (needle.y - bounds[i].y) / (bounds[j].y-bounds[i].y) + bounds[i].x)) {
result = !result;
}
}
return result;
}

/*public static boolean isLocationInBounds(@NonNull LatLng positionInQuestion, @NonNull LatLng topLeft, @NonNull LatLng topRight, @NonNull LatLng bottomRight, @NonNull LatLng bottomLeft) {
if (positionInQuestion.getLatitude() <= latNorth
&& positionInQuestion.getLatitude() >= latSouth
&& positionInQuestion.getLongitude() <= lonEast
&& positionInQuestion.getLongitude() >= lonWest) {
return true;
}
static class Point {
public final double x;
public final double y;

return false;
}*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
}
105 changes: 105 additions & 0 deletions utils/src/test/java/io/ona/kujaku/utils/CoordinateUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.ona.kujaku.utils;

import com.mapbox.mapboxsdk.geometry.LatLng;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by Ephraim Kigamba - [email protected] on 07/02/2018.
*/
public class CoordinateUtilsTest {

@Test
public void isLocationInBounds() {
LatLng bound1 = new LatLng(1.053254883591756,
0.339202880859375);
LatLng bound2 = new LatLng(0.707226913459037,
-0.11260986328124999);
LatLng bound3 = new LatLng(0.20324664405209258,
0.278778076171875);
LatLng bound4 = new LatLng(0.546561534676349,
0.98052978515625);

LatLng point1 = new LatLng(0.7218541012920028,
0.6592279349874681);
LatLng point2 = new LatLng(0.2881703637723443,
0.25182564298088916);
LatLng point3 = new LatLng(0.9615897751356437,
0.8265933408660401);
LatLng point4 = new LatLng(0.8249779712917739,
0.33087073166495085);
LatLng point5 = new LatLng(0.37328038670170727,
0.8613979983001541);
LatLng point6 = new LatLng(0.5828701459957611,
0.6038155629767493);
LatLng point7 = new LatLng(0.6336809580943352,
0.41203384171480817);
LatLng point8 = new LatLng(0.5391318208772033,
0.12247511076740225);
LatLng point9 = new LatLng(0.6852696915452708,
0.9021573945160707);
LatLng point10 = new LatLng(0.8761255229096057,
0.58502197265625);
LatLng point11 = new LatLng(0.49513234253574634,
0.8266160519883283);

assertEquals(true, CoordinateUtils.isLocationInBounds(point1, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point2, bound1, bound2, bound3, bound4));
assertEquals(false, CoordinateUtils.isLocationInBounds(point3, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point4, bound1, bound2, bound3, bound4));
assertEquals(false, CoordinateUtils.isLocationInBounds(point5, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point6, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point7, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point8, bound1, bound2, bound3, bound4));
assertEquals(false, CoordinateUtils.isLocationInBounds(point9, bound1, bound2, bound3, bound4));
assertEquals(false, CoordinateUtils.isLocationInBounds(point10, bound1, bound2, bound3, bound4));
assertEquals(true, CoordinateUtils.isLocationInBounds(point11, bound1, bound2, bound3, bound4));
}

@Test
public void contains() throws Exception {
CoordinateUtils.Point[] bounds = new CoordinateUtils.Point[4];
bounds[0] = new CoordinateUtils.Point(0.339202880859375,
1.053254883591756);
bounds[1] = new CoordinateUtils.Point(-0.11260986328124999,
0.707226913459037);
bounds[2] = new CoordinateUtils.Point(0.278778076171875,
0.20324664405209258);
bounds[3] = new CoordinateUtils.Point(0.98052978515625,
0.546561534676349);

CoordinateUtils.Point points[] = new CoordinateUtils.Point[11];

points[0] = new CoordinateUtils.Point(0.6592279349874681,
0.7218541012920028);
points[1] = new CoordinateUtils.Point(0.25182564298088916,
0.2881703637723443);
points[2] = new CoordinateUtils.Point(0.8265933408660401,
0.9615897751356437);
points[3] = new CoordinateUtils.Point(0.33087073166495085,
0.8249779712917739);
points[4] = new CoordinateUtils.Point(0.8613979983001541,
0.37328038670170727);
points[5] = new CoordinateUtils.Point(0.6038155629767493,
0.5828701459957611);
points[6] = new CoordinateUtils.Point(0.41203384171480817,
0.6336809580943352);
points[7] = new CoordinateUtils.Point(0.12247511076740225,
0.5391318208772033);
points[8] = new CoordinateUtils.Point(0.9021573945160707,
0.6852696915452708);
points[9] = new CoordinateUtils.Point(0.58502197265625,
0.8761255229096057);
points[10] = new CoordinateUtils.Point(0.8266160519883283,
0.49513234253574634);

boolean[] expectedResults = new boolean[]{true, true, false, true, false, true, true, true, false, false, true};

for(int i = 0; i < 11; i++) {
assertEquals(expectedResults[i], CoordinateUtils.contains(bounds, points[i]));
}
}

}