Iterating over a List with multiple Place AddressComponents - Kotlin - android

I am receiving a Place Detail (from Google Maps SDK Android) from a specific place.
This Place has different Place Details as a Result.
The output is the following:
Place found: AddressComponents{asList=[AddressComponent{name=58, shortName=58, types=[street_number]}, AddressComponent{name=Schwertstraße, shortName=Schwertstraße, types=[route]}, AddressComponent{name=Sindelfingen, shortName=Sindelfingen, types=[locality, political]}, AddressComponent{name=Böblingen, shortName=BB, types=[administrative_area_level_3, political]}, AddressComponent{name=Stuttgart, shortName=Süd, types=[administrative_area_level_2, political]}, **AddressComponent{name=Baden-Württemberg, shortName=BW, types=[administrative_area_level_1, political]}**, AddressComponent{name=Deutschland, shortName=DE, types=[country, political]}, AddressComponent{name=71065, shortName=71065, types=[postal_code]}]}
I am looking for the specific information of the typ=administrative_area_level_1 and level_2.
But i am not able to find a way of iteration.
With Kotlin i am able to access values hard, but this is not the solution.
The Objects AddressComponent change their posisitions in new requests to Google Place API. Even some places do only provide less information.
System.out.println(place.addressComponents?.asList()?.get(5)?.name)
System.out.println(place.addressComponents?.asList()?.get(5)?.shortName)
Results in:
Baden-Württemberg
BW
So how can i access the PlaceResult without hard-coding the positions, ignoring the order and find the specific information i am looking for?

The functionality that you're looking for is collections filtering. Assuming your list is named place, the following will return the two matching rows:
val desiredTypes = setOf("administrative_area_level_1",
"administrative_area_level_2")
println(place.filter { it.types.any(desiredTypes::contains) } )
// Prints: [name=Stuttgart, shortName=Süd, types=[administrative_area_level_2, political], name=Baden--Württemberg, shortName=BW, types=[administrative_area_level_1, political]]
The filter function returns any matching elements. it is the (default) name of the element under consideration. The any function returns whether the types sub-element for each element contains any of the items in the desiredTypes set.

Related

Migration Firestore replace field

Let's say that I have a document like this:
Document {
tags: list<Int> {0,1,2}
}
I want to change it to this:
Document {
tags: list<String> {SEASON, TRAINING, TOURNAMENT}
}
I have active users which uses the list of ints, How do I create a migration in Firestore for this problem?
One solution I have in mind is to make 2 migrations:
For creating a new tags called tagsStrings.
For deleting all users who still have tags.
But can I make it in 1?
I was unable to find documentation for this, on https://cloud.google.com/firestore/docs/manage-data/move-data
Thanks in advance
Firestore does not have a "migration" like SQL databases. The only way to modify data in existing documents, in bulk, is to:
Query for the documents to change
Iterate the results
Update each document with new values
Each one of these tasks should be straightforward.
You might also consider lazily updating each document as each are individually read during the normal course of your app's usage. So, if your app reads a document in the old format, immediately update it to the new format.
It's often helpful to have a dedicated field in each document to indicate which version of data that's contained within. So, initially set v=1 in each document, assign v=2 to mean that the document has strings instead of numbers for tags, then use that number to determine which documents have yet to be migrated.

Query GeoHashes in Firetore in range Android

What I'm trying is to get the documents that are in range of a point.
Following the videos and taking a look to the geo-hash library for android, I'm able to get the bounding box and get the necessary geohashes to query in firebase.
As example:
Point -> LatLng(40.4378698,-3.8196205) (Madrid,Spain)
Radius -> 5000meters (5 km)
The boundary box I get is:
[GeoHashQuery{startValue='ezjnh', endValue='ezjns'}, GeoHashQuery{startValue='ezjjs', endValue='ezjj~'}, GeoHashQuery{startValue='ezjq0', endValue='ezjq8'}, GeoHashQuery{startValue='ezjm8', endValue='ezjmh'}]
One I have this list, I call Firebase to retrieve the documents that "match" this criteria:
fun getUpTos(queries: MutableSet<GeoHashQuery>, onSuccessListener: OnSuccessListener<QuerySnapshot>, onFailureListener: OnFailureListener) {
var reference = Firebase.firestore.collection("pois")
queries.forEach { entry ->
reference
.whereGreaterThanOrEqualTo("geohash", entry.startValue)
.whereLessThanOrEqualTo("geohash", entry.endValue)
}
reference.get()
.addOnSuccessListener(onSuccessListener)
.addOnFailureListener(onFailureListener)
}
At this moment I have around 20 Poi's in firebase to start doing the test. All Poi's are in Barcelona and 1 in Madrid.
After doing the query, I'm gettin ALL the poi's, when it was supposed to just return the Madrid poi.
How can I get only the pois that fit the query? It seems is not working properly (or I'm doing obviously something wrong)
Is possible to achieve this type of querys?
Is possible to achieve this type of querys?
Yes, it is possible.
When you are iterating through your queries MutableSet, at every iteration you are creating a new Query object. So you cannot simply call get() outside the loop only once and expect to have all those queries working. What can you do instead, is to add the get() call to every query inside the loop. The type of the object that results is Task<QuerySnapshot>. Add all those Task objects to a List<Task<QuerySnapshot>>. In the end, pass that list of tasks to Tasks's whenAllSuccess(Collection> tasks) method as explained in my answer from the following post:
Android Firestore convert array of document references to List<Pojo>

Filter children by inner list value

According to the previous image
Is it possible to get every child of that matches the value of "11000" in that is inside the array ?
(there might be multiple entries for )
It depends on where you start. From /planes/PaMé7800_..._785/directiones it is definitely possible. But from /planes it is not possible, since you can only query values at a known path under each child.
Essentially your current structure allows you to efficiently find the directiones for each plane, but is does not allow you to efficiently find the planes for a directione. If you want to allow the latter, consider adding an additional data structure for it. For example:
directionesStCPToPlanes
dir11000
PaMé7800_..._785: true
With this additional data structure, you can also look up the inverse relation.
This type of double data storage is quite common and is known as denormalizing the data. For more on this, see:
Many-to-many using Firebase
Many to Many relationship in Firebase
Firebase Query Double Nested

Get current place type in Google Places API

I'm working with Google Places API.
I'm getting right the place in my log, but I just want the name of the type of place. Let's say this is my output
Place 'Parque Las Tejas' has likelihood: 0.950000 Type: [69, 1013, 34]
So, at first I get the position where I am, the likelihood of where I am and then I just used:
List<Integer> types = placeLikelihood.getPlace().getPlaceTypes();
thinking it would return like "park" or "square" but instead of that I get those array of numbers [69, 1013, 34].
According to what I read here, there is lots of types that defines a certain place.
What I want is to get that kind of types only, so if I'm at a restaurant I don't want the name of the restaurant but instead just the type, so "Restaurant" will be my output.
I need this because I want to give the user options depending on what type of place they are.
Any idea what I'm doing wrong?
The List<Integer> that you get is actually the id of type of places, according to the docs:
The elements of this list are drawn from Place.TYPE_*
The list is here. So basically your goal is to convert int code to a string using this list. You can find your solution here, basically you obtain all the fields from the Place class, find all the fields that start with "TYPE", get the int value and compare it to the value that you get from the getPlaceTypes().
even though this is an old post, it will help you if you an encountering this issue,
i was encountering this issue
and here is my approach
List<Place.Type> types = placeLikelihood.getPlace().getTypes();
to get all the type you can use a foreach loop
for(Object type:types){
// get all individual type
}

emoji look up table and algorithm

I am trying to build an android app that the user can enter a string, and a list emoji related to that string would show up. (Just like Venmo app) For example:
case 1: User enters "pizz", and in the list there would be "🍕", note that the users enter "pizz", not pizza!
case 2: User enters "rabb", and in the list there would be "🐇" and "🐰", note that the users enter "rabb", not rabbit!
What would be a good data structure and algorithm for this problem?
A trie is what your looking for. From Wikipedia
A trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search tree—an ordered tree data structure ...
A trie is similar to a HashMap<K,V>, you can perform a lookup with keys and get a value. The difference is that you can also search by prefix. Given a prefix, it will find all the key-value pairs in the structure that have that prefix. It's basically the data structure for generating search suggestions.
General Idea:
Trie<String, String> t = new Trie<String, String>();
t.insert("pizza", "🍕");
t.insert("rabbit1", "🐇");
t.insert("rabbit2", "🐰");
// then later...
t.findByPrefix("rabb"); // [🐇,🐰]
Unfortunately, tries are too generic and are not present in any popular data structure libraries (like Java Collections Framework or Google Guava, for example). You'd have to implement one yourself or find an existing implementation and modify it.
I'd recommend:
Learning the theory. Watch this video. There are many more on YouTube that will teach you the basics. You can also search google for "N-way trie" and read notes about it.
Taking this class TrieST and modifying it. It's very similar (or already perfect) for what you need: http://algs4.cs.princeton.edu/52trie/TrieST.java.html see specifically thekeysWithPrefix method.

Categories

Resources