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.
Related
I am working with Google Maps. Let's say I have around 1 million position markers stored in my database. What I want to do is load or display only those markers that should be on the part of map that is currently shown on screen. (For example, if my map is showing Asia, it should only show markers that are in Asia; if I move to any other place on the map, it should show markers in that region.) I'm doing this so I don't have to load the entire database at once, since that can cause delays in the app. I tried using Spatialite, but I didn't find a good tutorial, or information on how to use it. This is one of the links I followed, but I didn't get a good idea. Is there any other way to do this, or is Spatialite the best option?
You will have to figure out the best way to retrieve these positions from your database, but one way to add markers based on the map's camera's position is to add relevant markers in GoogleMap.OnCameraChangeListener.
// Check to see if your GoogleMap variable exists.
if (mGoogleMap != null) {
// Respond to camera movements.
mGoogleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
#Override
public void onCameraMove() {
// Get the current bounds of the map's visible region.
LatLngBounds bounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds;
// Loop through your list of positions.
for (LatLng latLng: yourLatLngList) {
// If a position is inside of the bounds,
if (bounds.contains(latLng)) {
// Add the marker.
mGoogleMap.addMarker(new MarkerOptions()
.position(latLng));
}
}
}
});
}
I wouldn't suggest looping through a million positions every time the map's camera's position is changed. I think the best way for you to do it is to get the current bounds of the map's visible region when the camera is moved, then send the bounds in a call to your backend on a different thread, and have your backend do the work of finding the positions that fit in those bounds and then return that list back to your app where you can add markers accordingly.
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,
Forgive me for being new to android app building.
My plan is to build an app that would take a city and open a new activity for it. The problem is I really don't know how to go about that. My plan would be very similar to how the app yik yak does it where you go put a marker in a certain area and it brings you to the activity for that location. I believe yik yak only shows the ones that are close to you but my plan was to take you to an activity for that location. Is that possible to do it that way or should I take yik yak's route on it and only show things that are within a certain radius of you?
Where can I get started on learning how to do that?
First, you need to choose a provider for your maps.
I would say google map is a good start as you suggested in the tag, but beware, it might not be available on some Chinese devices without play services.
You can follow the documentation here for a quick start
https://developers.google.com/maps/documentation/android/
Regarding the number of pins you want to put on your map, I see no problem to display a lot of them, you just have to group markers as the user zooms out, so you don't have 10 pins overlapping themselves.
So if in a quite zoomed state, the user clicks a group of markers, you can display a dialog to choose the city.
If the user zooms in a lot and clicks a particular pin, then you can start an activity.
Your main concern is listening for a zoom level change, like that:
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
private float currentZoom = -1;
#Override
public void onCameraChange(CameraPosition pos) {
if (pos.zoom != currentZoom){
currentZoom = pos.zoom;
// do you action here
}
}
});
Credits: https://stackoverflow.com/a/15046761/689710
Then, you can create a Map of Lists, the keys of the map will be the a custom lat / long object. Let's call it Pin. Pin provides it a custom isCloseTo method (taking a zoomlevel in params, and an other Pin object).
For each city you want to add on the map
For each key in the map
If the Pin object of your city isCloseTo the Pin key of the map
Add it to the list for that key
Else
Add a new Map entry with you city Pin as key and a list with your city as value
You Pin.isCloseTo method will be somehow similar to that:
https://stackoverflow.com/a/18170277/689710
Returning true or false according to "dist" return and your zoom level.
When you processed all your cities, you can browse the map and create a marker for each Pin key.
In a zoomed in state, you will have a Map with lists containing only one item, but i don't think it's much a problem.
You know yik yak is not starting a new activity when the place on the map is clicked. It rather updates the data underneath it when a new place is clicked. I think what you mean is you want to refresh/change the data displayed when a new city on google maps is clicked?
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);
}
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.