-
Notifications
You must be signed in to change notification settings - Fork 12
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
ekigamba
wants to merge
3
commits into
master
Choose a base branch
from
43-accurately-determine-bound
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 37 additions & 27 deletions
64
utils/src/main/java/io/ona/kujaku/utils/CoordinateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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
105
utils/src/test/java/io/ona/kujaku/utils/CoordinateUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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