Android - Distance to items - RecyclerView - android

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,

Related

How to manipulate properties of Livedata list

Say like LiveData<List<Item> is received from remote and few of the Item's properties are calculated in the device based on some conditions.
What would be the best practices to do so. Please suggest.
Say e.g., Item class as below:
class Item{
int id
String name
float location
float distance
}
in which the id, name and location of each Item are received from remote but the distance to be calculated from location locally. How to calculate the distance and return a LiveData which can be supplied back to the View?
Appreciate your time and input.
You cant manipulate LiveData, it is an immutable data type. If you want to work with mutable live data you must use the MutableLiveData.
This document told everything about it. https://developer.android.com/topic/libraries/architecture/livedata

How to implement first person view in android google map?

In my android application i want to implement first person view. Please check following URL for first person View -
Google First Person View
I found one link which redirects to first person view please check -
Google Maps Navigation - Using Google Navigation in Android Application
But in it we don't have control over navigation view or first person view. It redirects to google's default first person view. I want to implement it manually. Means in which we can pass latitude and longitude dynamically(from Server)
Is there any way to implement first person view Pro-grammatically, in which we have command over view ?
Thanks in advance!
Don't know exactly, how to achieve that view, but you can do one thing, that is whatever orientation's value you are getting from your server or any gps device , pass it into bearing() method of CameraPosition and set tilt also, like following -
CameraPosition cameraPos = new CameraPosition.Builder().target(latlng)
.zoom(zoomvalue).bearing(orientation).tilt(tiltvalue).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos), null);
by doing this your map will be rotate according to value of orientation. and if you pass value of orientation into rotation(orientation) method of marker then your marker will rotate according to value.
Hope it'll help you.

How do I create a new activity for each city?

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?

Using one activity resources from other class

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.

Get the user's GPS location and store it

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);
}

Categories

Resources