10

I'm developing an Android application which receives a given address. I would like to only let the app run on the device, if the user is on that address, or closer to it.

Is that possible to do only with google maps api?

Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61
  • What would happen when a user tried to launch the app and wasn't within your desired range? – Prmths Sep 27 '13 at 16:52
  • 1
    Maybe this could help you: http://stackoverflow.com/questions/3652951/google-maps-api-get-coordinates-of-address . You basically need to compare two sets of coordinates and figure out if these two pairs are close enough to one another. – Birb Sep 27 '13 at 16:58
  • @Prmths the app will simply not open :) – Felipe Mosso Sep 27 '13 at 17:06
  • @Izmaki thanks! i will take a look at it as soon as I get home – Felipe Mosso Sep 27 '13 at 17:07

2 Answers2

8

You could get the address and from the address get the latitude and longitude:

Geocoder coder = new Geocoder(this);
List<Address> address;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address == null) {
        return null;
    }
    Address location = address.get(0);
    location.getLatitude();
    location.getLongitude();


}

And then compare it with your location:

if (distance(mylocation.latitude, mylocation.longitude,   location.getLatitude(), location.getLongitude()) < 0.1) { // if distance < 0.1

   //   launch the activity
}else {
   finish();
}


/** calculates the distance between two locations in MILES */
private double distance(double lat1, double lng1, double lat2, double lng2) {

    double earthRadius = 3958.75; // in miles, change to 6371 for kilometers

    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);

    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);

    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
        * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    double dist = earthRadius * c;

    return dist;
}

EDIT: As @FelipeMosso was saying, you can also use distanceBetween that computes the approximate distance in meters between two locations or the distanceTo that gives you the distance between where you are at and the destination..

Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
Dyna
  • 2,304
  • 1
  • 14
  • 25
  • Thank you @Dyna! I will take a look at this when I get home and I get back to you with feedbacks – Felipe Mosso Sep 27 '13 at 17:10
  • Your idea worked for me! But I have searched a lit bit before, and I found a Android method of Location class called distanceBetween that does the same thing you distance method. If anybody run into the same problem, I recommend you to use: http://developer.android.com/reference/android/location/Location.html – Felipe Mosso Sep 30 '13 at 11:50
  • OK @FelipeMosso. I'll update my answer with your link so that we can help other developers with this issue. Cheers* – Dyna Sep 30 '13 at 13:39
2

The GMSCore Locations API also has Geocoding, which allows you to detect the proximity of the device to one or more Geofence locations. You can use Maps to get the lat lng for an address.

This won't stop your app from running. A better solution is to start the app, then allow it to go into the background. When the user crosses a Geofence, put up a notification. When the user clicks the notification, bring up an Activity.

See Creating and Monitoring Geofences

Joe Malin
  • 8,621
  • 1
  • 23
  • 18