1

I'm trying to create an app for android with little experience programming. I want to use a phones gps to determine a place on google maps then cause an action to happen. The action would be based on the location and I'd also like to have the user designate locations to cause an action.

If there are any resources or websites you can point me to I would really appreciate it.

I'm currently only using android studio, is there anything else I need?

  • I've been searching for a while and learned that what I'm looking for is geofencing, which I believe would be used in tandem with LocationManager. If you still think you can help, it would be greatly appreciated, I'll be searching for more in the mean time. – Taquitoburrito Apr 08 '16 at 06:59

1 Answers1

1

first, you need the current user location. You can follow the steps offered here to obtain it. The locationmanager provides a method onLocationChanged(Location loc) which then is triggered automatically when the location changes. You can implement your "actions" here. Im not quite sure if I have understood what the designation of locations by the user would have to trigger. If you want to trigger a specific "action", you can implement the method onMapClicked(Location loc) by setting an onMapClickListener to your map.

Depending on how your "predefined places" are built, you have different possibilities.

1) Are those places points? --> you can calculate the euclidian (or spherical) distances, and related to that distance decide if you want to trigger something or not.

2) are those places 2d rectangles? --> use LatLngBounds and its contains(LatLng point) method to decide if the userlocation or user-designated-location is inside

3) are those places any polygons? here is a good example on how to create such a place. there is also a contains() method.

4) Are your places not predefined? We need more information on what you want to do in this case.

edit based upon ur comment : so you there are 2 things your app should do: 1) do some action (just like popping up a message), when the user is entering a "place" or comming near to a "place" 2) give the user the possibily to create such a place by himself.

secondary can be done by adding a onMapClickListener to your map.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            // latLng is the lcoation, tapped by the user on the map. lets save it
            chosen_location = new LatLng(latLng.latitude, latLng.longitude);
        }
    });
}

This will save the single points, tabbed by a user on a map. If you wanna go further later for polygons, you can add this point to an ArrayList of points. However, you probably want the user to add more information about that point. Make use of EditText to let the user e.g. enter a description of that point (or later polygon), and finally, when clicking a button to "save that entered place", you can add it to an ArrayList. You probably also gonna have a server-backend in order to update and retrieve a list of places created by multiple people from anywhere.

but for now, lets go on and check if the user came nearTo such a point or entered such a polygon:

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // the users location changed (e.g. he moved some meters)
            current_location = new LatLng(location.getLatitude(), location.getLongitude());
            // somehow get all the places ( i guess a server backend is recommended )
            // for now, lets say all the places are already obtained into an Arraylist called places:
            ArrayList<LatLng> places = new ArrayList<LatLng>;

            // Go check for each of the places, if the users current location is near to such a point
            for (LatLng place : places) {
                if (distance(place,current_location)<100) {
                    LatLng near_place = place;
                    // here we are! near_place is near the users current location, lets do some action
                    Log.d("app","Users location (" + current_location.latitude +","+current_location.longitude+") is near to a place with coordinates "+near_place.latitude+","+near_place.longitude+").");
                }
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    });
}

and here is a method to calculate (spherical) distance in meters between to LatLng objects:

 /**
 * method calculate distance between two LatLng points in meter
 */
public float distance(double lat_a, double lng_a, double lat_b, double lng_b) {
    double earthRadius = 3958.75;
    double latDiff = Math.toRadians(lat_b - lat_a);
    double lngDiff = Math.toRadians(lng_b - lng_a);
    double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
            Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
                    Math.sin(lngDiff / 2) * Math.sin(lngDiff / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = earthRadius * c;

    int meterConversion = 1609;

    return new Double(distance * meterConversion).floatValue();
}
Community
  • 1
  • 1
MojioMS
  • 1,583
  • 4
  • 17
  • 42
  • I was thinking of using place points proivided by google places, later on when I am more experienced I'd probably move on to polygons but I'd like them to be created by multiple users or created by local businesses. – Taquitoburrito Apr 08 '16 at 07:29
  • I updated the answer. However, you probably want to have a server running with a list of those places retrieved by users or local businesses. – MojioMS Apr 08 '16 at 08:54
  • I think I'm in love, Thank you so much! – Taquitoburrito Apr 08 '16 at 09:19