1

Is there a way to get a trigger from Android OS, if a user enters in a predefined area(Lat long list given to the OS) ? e.g. If I give lat long of a particular restaurant to the OS, is there a way OS can send me a trigger when user enters the restaurant?

Thanks

MobileAppDeveloper
  • 1,048
  • 2
  • 16
  • 27

2 Answers2

0

I've already developed an app with a lot of geocoding.

My way was to measure the distance between the current location (lat,lon) and the target location.

If you put this into a (background)-task it could solve your problem.

I think the LocationManager and LocationListener class in namespace android.location would be enough for it to track.

If there's no method to calc the distance between those coordinates you could use this code:

public static class Distance
{
    // cos(d) = sin(φА)·sin(φB) + cos(φА)·cos(φB)·cos(λА − λB),
    //  where φА, φB are latitudes and λА, λB are longitudes
    // Distance = d * R
    public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)
    {
        double R = 6371; // km

        double sLat1 = Math.Sin(Radians(lat1));
        double sLat2 = Math.Sin(Radians(lat2));
        double cLat1 = Math.Cos(Radians(lat1));
        double cLat2 = Math.Cos(Radians(lat2));
        double cLon = Math.Cos(Radians(lon1) - Radians(lon2));

        double cosD = sLat1 * sLat2 + cLat1 * cLat2 * cLon;

        double d = Math.Acos(cosD);

        double dist = R * d;

        return dist;
    }

    public static double Radians(double x)
    {
        return x * PIx / 180;
    }

    const double PIx = Math.PI;
    const double RADIO = 6378.16;


}

Good luck! Marco

Marco
  • 31
  • 4
  • Thanks for the reply. I don't want my app to continuously track the location of the user. I want my app to stay idle until OS triggers me about the location of the user in a particular area. – MobileAppDeveloper Apr 12 '18 at 10:51
0

Your description resembles you are in need of Geo fencing. It allows you to get triggered whenever user enters some proximity.

You can refer here which provides tutorial to fulfill your requirements.

Sagar
  • 23,903
  • 4
  • 62
  • 62