How to restrict app usability to a certain geographical area ANDROID

半腔热情 提交于 2019-12-10 14:48:25

问题


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!


回答1:


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.




回答2:


Here's official documentation on how you can get the current location of the device and register for new location updates: http://developer.android.com/training/location/receive-location-updates.html

Once you have the current location, you can use Location.distanceTo(Location) or Location.distanceBetween(double, double, double, double, float[]) to check and see if the device is within the designated radius.




回答3:


Maybe use Geocoder to extract city/state name based on the current location, this answer might help you on how to do that.

so if the city/state name is not allowed, you close the application and show a message.

but note that Geocoder requires internet access, not sure if that suits you though.




回答4:


Define a radius for the location and calculate distance of current location in onLocationChangeListener. Set a Boolean flag if the distance is less than the radius. Do your action when the flag is false.



来源:https://stackoverflow.com/questions/36929834/how-to-restrict-app-usability-to-a-certain-geographical-area-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!