android notification for geolocation - android

I made a android application with multiple Geolocation on Google map (pins)
I would like to send a notification when the user is close to one of these locations.
Any ideas?

This is actually fairly easy. First, set up an application that monitors your location. This post will show you more on that.
Once you know your position, you can simply determine if you're within a certain range of it. To calculate the distance between two points, try the Location class:
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
results is a float[], so to return the distance, simply use float distance = results[0];.
So, in a nutshell, compile a list of waypoints you want to recognize. Then, in your GPS monitoring code, regularly check the distance between yourself and the list of points. If you're within a threshold, say 100m, then send a notification as a Toast message or something.
Good luck!

Related

How to figure out when a gps coordinate is valid in Android

I'm developing an Android app and I need some help to save GPS coordinates for a route... coordinates are very important for my app, so: how can I do in order to get very good coordinates (especially at a first try)? How can I understand whether the position is correct or not? How can I use getAccuracy method or similar to figure out whether the position is wrong so that I have to reject it?
For example: I get a 1st LatLng coordinate but the 2nd LatLng is located 100 meters away from the 1st coordinate, so I guess that is very unlikely that a user can move 100 meters away in a few seconds... how can I create such an alghoritm?
Android Defining a Model for the Best Performance to get location
http://developer.android.com/guide/topics/location/strategies.html#BestPerformance

how to calculate distance while walking in android?

I am developing a demo for my app, in which there are two buttons named as "START" and "STOP". When user taps on "START" he will start walking. What I want to do is make it so that when users tap "STOP" then the demo will calculate his distance between "START" and "STOP". If the user pressed "START" and pressed "STOP" without taking a single step, then it must show 0km or 0m. I don't have any idea how I should start this; please make a suggestion.
There are different ways to do this:
GPS: Keep adding GPS distance between 2 points every X seconds (say 10 sec). Check Android Location.distanceTo or distanceBetween. Check My Tracks app, it is open source. GPS is not available indoors and would have error if user is changing direction very frequently (read every 1-2 second)
Accelerometer: Look for code/library for step detection using accelerometer. Distance comes from double integration of acceleration, errors can add up very quickly here.
Step detector: Built-in in Nexus 5. Google must have taken care of accelerometer errors to extent possible. This is hardware-based computation, consumes less battery but not available in most of handsets as of date.
You can also check Pedestrian dead reckoning
One way to go about it is using the accelerometer data. Your app should continuously record the accelerometer data after the user presses the Start button. You will observe a peak in your data whenever the user takes a step. Apply a filter on this data, and you shall be able to detect the number of steps taken with reasonable accuracy. Multiply it by the step length and you should get an approximation of the distance travelled. Take height of the user as an input argument. Step length is around 0.45*Height of a person. Since this approach is independent of GPS, It will also work indoors.
EDIT:
You'll need to use the accelerometer values for all three axes to make it fairly independent of the device orientation.You can go with x^2 + y^2 + z^2
Ask for GPS permissions in your app. When start is tapped, record the GPS coordinates. Do likewise for stop. You now have two coordinates. You can then apply the distance formula to get the total distance traveled.
Edit:
As for the case clarified in the comments, I think what you need to look into is Android's motion sensors. You may have to make a lot of assumptions or ask your users to calibrate your app before actual use.
Assume that you know your user's pace factor. Using the motion sensor, time how long is the user "walking" (obviously, there's no easy way to determine if your user is actually walking or just shaking the phone). Multiply this with your user's pace factor and you get a pretty rough idea of how much walking has your user done.
Comment to "There is one problem with this solution. If you are traveling at constant speed, the acceleration is 0, so accelerometer wont pick-up any readings. – jnovacho 16 secs ago"
(sorry, don't have enough reputation to comment directly)
when you accerlerate, save the accerleration and then calculate the speed you are walking. Stop calculation of speed whenever the acceleration changes and start over. If you stop, you should receive a negative accerleration, you'd then have to calculate if you just slowed down or stopped completely. But thats simply math :)
I had gone with the Gps method.
With the following steps:
On the click of start button, the latitude and longitude of the starting point were fetched and stored it in my dto with a proper TripId.
on the click of stop button :
TripDto dto = service.GetStartLatLong(TripIdA);
double lat = Double.valueOf(dto.getStartLati());
double lon = Double.valueOf(dto.getStartLongi());
Location locationa = new Location("point A");
locationa.setLatitude(lat);
locationa.setLongitude(lon);
double distance = location.distanceTo(locationa);
The distance returned by the location.distanceTo() method is in meters.
Try using sensors for this, I feel you should not use GPS as it may not be so accurate.
Refer to the following open source pedometer project for what you are talking about.
Pedometer library
Will update this answer with more specified code if you want to go with sensor.
public double getDistance(double lat1, double lon1, double lat2, double lon2)
{
double latA = Math.toRadians(lat1);
double lonA = Math.toRadians(lon1);
double latB = Math.toRadians(lat2);
double lonB = Math.toRadians(lon2);
double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
(Math.sin(latA) * Math.sin(latB));
double ang = Math.acos(cosAng);
double dist = ang *6371;
return dist;
}
You can find the Latitude and Longitude of the current location using START button using location manager and store it in the variables. Then find the latitude and longitude of your end point using same method. Find the Distance between them by using this -
https://www.geeksforgeeks.org/program-distance-two-points-earth/#:~:text=For%20this%20divide%20the%20values,is%20the%20radius%20of%20Earth.
if your track is not a direct way (curve or zigzag) then you should use check location every 3-10 second
some one else say before me (x second).

Find locations that are behind my current location in Android

I have some locations (lat & long) and I need to show these locations in a listview. I can do this perfectly.
But now I want to show the locations that are ahead of my current location. That means I want to skip the locations that I have already passed during my driving.
Let me clarify more clearly. we have locations like loc1 (lat,lon) , loc2(lat,lon), loc3(lat,lon).. loc100 (lat,lon). during my driving I like to see the locations (loc1 - loc100). But now I want to hide the locations (between loc1 to loc100) which I have passed from my current position/location. Say, I have passed loc1, loc3, loc5 so I need to skip these 3 locations from my listing in listview. To achieve this, I need to know which locations (loc1 - loc100) are behind my current location (gps current location) so that I can skip that locations.
Any idea? how i can achieve this in my code? Please help regarding this.
I would have two data structures (e.g., LinkedList) of Location objects:
unvisitedLocations
visitedLocations
Your list being shown to the user would be based on the contents of the unvisitedLocations data structure.
Based on your real-time location, you will need to detect proximity to each of the Location objects in the unvisitedLocations data structure based on given radius threshold value that you choose. To do this, you can either register each location with the LocationManager.addProximityAlert() methods (which will fire a PendingIntent when detecting proximity), or loop through the unvisitedLocations data structure and do this on your own - something like:
if(currentLocation.distanceTo(unvisitedLocations[i]) < threshold){
visitedLocations.add(unvisitedLocations[i]);
unvisitedLocations.remove(i);
}
This will give you the list of currently visited and unvisitedLocations based on your real-time locations.
Note that as initial size of the unvisitedLocations grows, performance will become an issue since you'd be looping through the entire data structure contents to determine proximity of location for each real-time location update, which can be once per second from GPS. If performance becomes an issue, you should look into moving the proximity detection server-side and using a spatial database, which has special data structures to make proximity detection far less intensive.

Distance between a point and several locations

I am developing an application that shows the distance between the user and multiple locations (such as foursquare in the image below) on android, I am using eclipse and would like to know how to calculate the distance between various points. Thank you!
Image:
http://i.stack.imgur.com/rsWmO.jpg
There are probably many ways to get this done, here's an option.
You could use the distanceTo() method. If you want more than one distance, simply use a loop to repeat it until you've calculated the distances between all the Locations you have at hand.
If you're using Google Maps, you can use a Distance Matrix
https://developers.google.com/maps/documentation/distancematrix/
https://developers.google.com/maps/documentation/javascript/distancematrix
Here I am providing you some sample code for Distance calculation. I have done like this in my project. Here distanceTo() method will return you the distance in double.
private Location currentLocation, distanceLocation;
double distance = 0;
//set your current location
currentLocation.setLatitude(currentLat);
currentLocation.setLongitude(currentLong);
//set your destination location
distanceLocation = new Location("");
distanceLocation.setLatitude(destinatioLat);
distanceLocation.setLongitude(destinationLong);
distance = currentLocation.distanceTo(distanceLocation)/1000;
Same for more then one location you can use Array for storing distance. Its on you how you want to use it as per your requirement.
Hope this will help you.

How can I find the distance from a particular latitude and longitude to surrounding area in km?

I am new to Android. In my application, customer is at a location. I want to find agents near-by to that customer using his/her latitude and longitude. How can I do this? customer is on one location & We want to search agents surronding that perticular customer.
I have latitude of customer and agents and based on I want to search agents(From customer's latitude longitude in surrounding area of 5 km. which agents are thare that i want to search).
pseudocode
area = 100;
for a over allAgents
if(Math.abs(a.x - customer.x) < area || Math.abs(a.y - customer.y) < area)
nearCustomerArray.add(a);
If you already know agent's location, grab you current location using built-in device gps receiver.
Then, calculate distance between your coordinates and the one of the agent using distanceTo method of Location class
Finally, found the small distance among all the distances you calculated.

Categories

Resources