Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict app usability to a certain geographical area ANDROID

Tags:

I want to define a geographical boundary outside of which, the app will refuse to work. I already know how to do this with a square bound by two lat/long pairs:

 if ((dLAT.doubleValue() > 35.309171) || (dLAT.doubleValue() < 35.226442) || (dLON.doubleValue() < -92.790165) || (dLON.doubleValue() > -92.707081))
    {
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);

        localBroadcastManager.sendBroadcast(new Intent("killapp"));
    }

I also know about geofencing... or enough to know that with geofencing, areas are defined as circles with a radius from a single point.

But like I said I would like to define a boundary that matches reality:

For example, if there was an app that was designed NOT to work if the user is outside the border of Kansas, it would not be satisfactory to define a RADIUS, as the state of Kansas is not circular, and its border is wiggly.

I happen to be using Android for this, but I doubt that really matters for this question.

Thanks for any help!

like image 855
Nerdy Bunz Avatar asked Apr 29 '16 04:04

Nerdy Bunz


1 Answers

This is much easier than one thinks:

define the region(s) as polygon, having N points in longitude, latitude coordinates.

Use a point in polygon method:

Point in Polygon Algorithm

To test if the current point is inside our outside of the polygon, the code above is using x,y coordinates. Just use longitude instead of x and latitude as y.
The code may not work when the region crosses the North or South pole, and when it crosses the datum limit (border of longitude = -180 to 180) but nobody uses your app there.

Altough one cannot always use formulas desigend to work in the x,y (cartesian) plane, it works here for spherical lat,lon, coordinates too.

like image 120
AlexWien Avatar answered Sep 28 '22 04:09

AlexWien