Student
Student

Reputation: 21

How to compare latitude and longitude values in geofencing?

I have written Geo-fencing code in open layers. Geo-fencing has to be done in a website. I am able to create a Geo-fence in the website. Also I know the latitude and longitude of the centre of the Geo-fence. But I don't have latitude and longitudes of any other points on that circle.

Now I have an android application which is the user. Whenever the application is in the Geo-fenced area, it should check and display a message "I am in the Geo-fenced area." So How I should compare users current location with and with what should I compare. I am using PostgreSQL database. I expect that if the user is in the Geo-fenced area it should display "I am in the Geo-fenced area"

Upvotes: 2

Views: 1842

Answers (2)

Siva Kalidasan
Siva Kalidasan

Reputation: 119

Please use some algorithms to calculate whether you are inside boundary or not. Boundary can be any shape for example a polygon.

Please check algorithms like point-in-polygon (PIP) to find out if you are inside geo-fence or not.

Upvotes: 0

Ankit Dubey
Ankit Dubey

Reputation: 1170

Initially you'll have two LatLng points. first for Geofencing center and second for user current location. Before you want to check user is in geofence area or not, you've to decide how many Kilometers you'll allow from the center of geofence in radius to detect that user has come in range.

then compare both the points as :

public int CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));

    return kmInDec;
}

Suppose if user is only inside the 1 KM range from Geofence, you'll allow him. So just call above CalculationByDistance(-,-) and it will tell you user is near geofence of not.

Upvotes: 2

Related Questions