Google map show toast at particular location - android

I want to make a small project that will allow a user to mark attendance only if he is within a particular location with tolerance of 15/20meters. I'm completely confused how to do it. It will be on a button click.
I have not written any code for the same as I don't know how to do it. I have got the api key as well as have the standard Google map fragment.

get location from google map and compare location like this
float radius = 17f;
Location newlocation = new Location("");
newlocation.setLatitude(lattitude); //lattitude is your old location
newlocation.setLongitude(longitude); // longitude is your old location
distance = myLocation.distanceTo(newlocation); //myLocation is your current location
if(distance < radius){
Toast.makeText(getApplicationContext(), "your Toast message", Toast.LENGTH_LONG).show();
}

Step 1: know your location coordinates (latitude, longitude) using Android location services
Step 2: know the "particular location" coordinates
Step 3: calculate the distance between these 2 sets of coordinates (google how to do it)
Step 4: if (distance < threshold) then { display button }

Related

How to check the locations using LatLng values?

I'm a beginner in android and my question is regarding about google map api v2.I drew a path using my current position and my destination and I got some locations of a bus which is travelling through my path. How can I check the buse's location LatLng values are matching with my path LatLng values???
Try this,
Location crntLocation=new Location("crntlocation");
crntLocation.setLatitude(currentLatitude);
crntLocation.setLongitude(currentLongitude);
Location newLocation=new Location("newlocation");
newLocation.setLatitude(server_latitude);
newLocation.setLongitude(server_longitude);
float distance = crntLocation.distanceTo(newLocation); in meters
//distance =crntLocation.distanceTo(newLocation) / 1000; // in km
if(distance<200){
//You are within 200 meter range
}
Put this in a loop.

Check if a GPS location is within a certain radius of another GPS location in android

I'm developing an Android app which takes the user's current location and shows other users' gps locations on a map which falls with a certain radius, say 20 kilometers.
For example, if I am the user, I want to see other users' locations plotted on a map who are within a radius of 20 kilometers from my location.
I am able to save my location, other users' locations and plot them on map also. I am not able to figure out if those users fall in 20 kilometers radius from my location or not.
I use Google Maps Android API v2 to plot locations on map and Parse for saving the GPS locations (latitude and longitude coordinates)
I Googled for solutions but in vain. Could anyone please provide a hint or reference or sample code on how to check if a GPS location is within a certain radius of another GPS location or not.
Any help will be appreciated.
It's very easy. When you add Marker into your Map just get distance from that Marker to your Current Location if distance is <= 20 then add that marker into Map. Like so
float[] results = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
newPosition.latitude, newPosition.longitude, results);
First get distance between 2 position:
float[] distance = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
currentPosition.latitude, currentPosition.longitude, distance);
distance will be in meter.
So, we have to convert our designer radius (20 KM) distance in meter to compare.
double radiusInMeters = 20.0*1000.0; //1 KM = 1000 Meter
New we can check that current position is inside 20 KM radius or not using...
if( distance[0] > radiusInMeters ){
Toast.makeText(getBaseContext(),
"Outside, distance from center: " + distance[0] + " radius: " + radiusInMeters,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(),
"Inside, distance from center: " + distance[0] + " radius: " + radiusInMeters ,
Toast.LENGTH_LONG).show();
}

how to open up a map of a particular city using google APIs in android

I have been able to open up a map of the world through code and mark a few places. However, I always have to zoom down to the city, which takes a little while. Is there any way to directly open up a map of a particular city?
You can use moveCamera with your last stored location (lat, lng and maybe zoomLevel too) for example.
final LatLng location = new LatLng(..., ...);// Loaded from SharedPreferences maybe.
// Move the camera to location with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
You can use this sample code to animate the map directly to your city coordinate (lat, lng) and automatically zooming out to specified level :
// Set desired lat lng location, and zoom level (for example 10)
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(locationCity.getLatitude(), locationCity.getLongitude()), 10);
// Move the camera to location
gMap.animateCamera(cameraUpdate);

Location.distanceTo not giving correct results in android

I am trying to calculate the distance between two locations (Current location with the previous location).
So I tried the following:
Location previousLocation = new Location("");
previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f));
previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f));
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
editor.putFloat("totalDistanceInMeters", totalDistanceInMeters);
Log.e("Location Update","totalDistance"+totalDistanceInMeters);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+totalDistanceInMeters/1609, Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
To test the above code. The first time result was perfect and when it triggered the second time. The phone was in the same location but I am getting distances like 141.0111 m. thrid time 304.0011 m. Am I doing something wrong here?
The results are not showing up correctly. According to doc online the results are in metres.
Is there an easy way to calculate the difference between the first location results with the second one and if it is more than 10m I would like to do some other calculation if not just keep quite.
Let me know.
why are you even using the following code
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
That adds up the previous distance to present distance and gives the added value everytime....
example
first time
distance between A and B is 100 m
second time
distance between A and B is 100 m+100 m=200 m
so try using distance directly in toast
float distance = location.distanceTo(previousLocation);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+distance/1609,Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
I think accuracy needs to be set in order to get the exact distance. Also try to get hold of the previous and current locations manually so as to calculate the distance and verify.

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 (savedLocation), and then if the user returns to savedLocation, (or within a coarse range of that location since i'm suing network location) I want to fire to a broadcast receiver. I know fine and well how to save the user's location to the db, and to compare it with the current location, etc etc.
However, given that the network location is a relatively inaccurate method for determining location, what would be a good approach for determining when the user approaches within distance X of that savedLocation.
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
}

Categories

Resources