Skip to content Skip to sidebar Skip to footer

How To Use Coarse Location To Compare With Saved Location In Database

I am writing an android app where a user will geolocate using coarse location using Network Location. I want the user to be able to save a location at a particular time into a db (

Solution 1:

What understand from your question is that you are tracking the user's location and saving that locations to the database. Now you want to prompt user when user reach to the previous location but there should be a specific region. If this is then you can use the below code to know the region of the user from latitude and longitude.

String latitude1, latitude2, longitude1, longitude2;
//Now Suppose you have some values in this variables. 
//In your case latitude1 and longitude1 is from database and latitude2 and longitude2 is from current Location.
float radius = 500; //Distance in meters

Location location1 = new Location("GPS");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);

Location location2 = new Location("GPS");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);

Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);

//Here you can compare two distances 1. distance and 2. radius
if(distance <= radius) {
//Use is in a 500 meters radius. You can notify user by notification
}

Post a Comment for "How To Use Coarse Location To Compare With Saved Location In Database"