It want to keep an array (Java generics for example: List) of the last 10 coordinates in order of retrieval so latest coordinates are always at the top of the stack etc. If I ask for this array we can then go back to see what steps the user took by tracing there steps.Please help me here.
Thanks
First you need to read about how to retrive the user GPS location.
http://www.vogella.com/articles/AndroidLocationAPI/article.html
Then make your class implement android.location.LocationListener and in the onLocationChanged() callback just add the location to the list.
#Override
public void onLocationChanged(Location location) {
list.add(location);
}
Related
I'm struggling in finding the proper way to achieve the following:
Context:
I have a recyclerView that displays items retrieved from firebase. Each item has a longitude and a latitude.
I need to display the distance to the item based on the user location, on some activities only.
Is this the right way to proceed?
The activity request for location permission
If permission is granted, an intentService (implementing a
LocationListener)
retrieves the current user location.
onHandleIntent sends an intent
containing the location (latitude, longitude)
From here I'm stuck, I know I have to deal with a ResultReceiver?
Problem : updating the viewholder / recyclerview
The RecyclerView and Items would already been displayed, but once I have the location, I would update the distance to the item.
Question
How should I handle the update from the RecyclerView?
I hope my question is clear enough.
Thanks you for your help,
I am trying to make an application which allows the user to select a location on google map(on the press of a button). It is not related to his current position. He can select any position he wants(I was reading into markers, some help on that will be greatly appreciated). After he selects the location, I want that location to be saved in a string format in my application, so that I can do further work with it. I have searched around a lot, but can't seem to find any which allows the user to select his location through gmaps and save it, all through another application. Please help!
Assuming your map is called myMap, i think it should be something like this.
myMap.setOnMapLongClickListener(new OnMapLongClickListener(){
#Override
public void onMapLongClick(LatLng point) {
//TODO Handle your code.
//Add marker to map for clarity
myMap.addMarker(new MarkerOptions().position(point).title("My Marker"));
}
});
I have situation where I get the nearest location based on checking the distance between current lat and long
public void onLocationChanged(Location loc) {
lat1=loc.getLatitude();
lng1=loc.getLongitude();
Com_Util.check_NearLoc(lat1,lng1);
}
I update a static variable which contains the nearest place from the current location and display in a text view showing the current location in all the activities. I am able to get the nearest place and was able to display it only during onCreate of an activity.
I need a text view showing the current location while using the app. I dont want a thread to be running all the time checking for the location change instead is there any way updating the textviews from the check_NearLoc function itself which is in a nonactivity class.
So, I have plotted a GPX route in my MapView, and I am listening for location changes.
Ultimately I want to give my user notification when they are off/on course.
So, I can imagine just brute-force going through all my GPX coordinates and do a Location.distanceTo for each for each of them. But that seems expensive.
I could reduce the cost by doing it infrequently.
I am wondering if someone has a clever idea for achieving this?
You can use the function distanceTo but not inside onLocationChanged (because it is called very frequent) , you should do a timer and check if the user going off course in each 5 seconds (for example), this is how to implement the timer:
1- declare this as global variable:
private Timer myTimer;
2- add this code in your onCreate():
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 5000);
3- add these two functions in your class:
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//do your test for off course here
}
};
Good luck
I would probably keep track of three coordinates: Last, Current, Next. You can calculate the distance and direction from Last to Current and the location and direction the user is going and come up with some algorithm to ensure the user is going the right way (ensure that the user is approaching Current, ensure that they're within some distance of the line from Last to Current, etc.)
Then at some point you need to realize that the user has come as close as they're ever going to towards Current and are now heading for Next. Then you'll shift the points down (Last = Current; Current = Next; Next = ???;). Again there are a number of ways you could make this determination (user has gotten close enough to Current, user is heading towards Next and away from Current, etc.)
You might want to include some look-ahead in case you miss a few points, especially when points are close together, but you don't want to do too much. Test your route with loops in the paths and make sure you don't jump ahead. You'll probably also need to implement some sort of holyExpletiveIHaveNoIdeaWhereTheUserIsGoing() method to recover from the (inevitable) errors.
I have two class, one is an activity that can handle Chronometer, another is handle LocationListener.I want to run them together to use stopwatch and to get changed location.
Anyone have any samples or suggestions ? Thanks
Addition:
Sorry for the confuse question. I just need to know how to make chronometer run while keeping location that changed.
Well, you could use the LocationListener to add Location to an array every time it is changed to keep track of location.
Example:
public class MyClass implements LocationListener{
private static List<Location> locationList = new ArrayList<Location>();
#Override
onLocationChanged(Location location){
locationList.add(location);
}
}
Then you could use System.getTimeMillis() (or something akin) when you want to get the start time, and the same method when you want to get the end time. Just subtract the two to get the time difference and how long it took.