Android: Google Place API delivers ZERO RESULTS - android

I'm going nuts on this one. I used Places Autocomplete before and that worked like a charm. e.g: https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=false&location=53.6020,53.6020&input=mcdonalds&key=
Now I'm trying to use the search in a range of 1000m of the users location, but I keep getting ZERO RESULTS. Can someone see, what I can't?
https://maps.googleapis.com/maps/api/place/search/json?location=53.6020,53.6020&radius=1000&keyword=mcdonalds&sensor=false&key=
Thanx in advice!

You have to specify a type of search in your URL (i.e. /nearbysearch/, /textsearch/, etc)
try this:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=53.6020,53.6020&radius=1000&keyword=mcdonalds&sensor=false&key=
Also, don't forget to append your API key to the URL :)
Lastly, for debugging purposes, try to use a different latitude,longitude coordinate and see if that works (i.e. use the lat/lng for New York City).

Related

How to do a simple search in string in Firebase database?

I want to create a simple search in my app, but cannot find anything on interwebs about it, that's more recent than 2014. There must be a better way. There are startAt and endAt functions but they don't work as expected and are case sensitive. How do you guys solve this problem? How can this functionality still not exist in 2016?
In my case I was able to partly achieve a SQL LIKE in the following way:
databaseReference.orderByChild('_searchLastName')
.startAt(queryText)
.endAt(queryText+"\uf8ff")
The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText.
In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName property from the database.
Create two String variables
searchInputToLower = inputText.getText().toString().toLowerCase();
searchInputTOUpper = inputText.getText().toString().toUpperCase();
Then in the Query set it to:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Products");//Your firebase node you want to search inside..
FirebaseRecyclerOptions<Products> options =
new FirebaseRecyclerOptions.Builder<Products>()//the Products is a class that get and set Strings from Firebase Database.
.setQuery(reference.orderByChild("name").startAt(searchInputUpper).endAt(searchInputLower + "\uf8ff"),Products.class)
.build();
the "name" it's the node inside the Products Main Node.
the .startAt(searchInputUpper) & .endAt(searchInputLower + "\uf8ff") to make the search as contains all characters that typed in the inputText.getText() that you get.
finally I got it you can use where clause to get you result like SQL
LIKE keyword like% or %like
syntax :
Firestore.collection(collectionName).orderBy(field).where(field, ">=", keyword.toUpperCase()).where(field, "<=", keyword.toUpperCase() + "\uf8ff").get()
I my case used:
var query = 'text'
databaseReference.orderByChild('search_name')
.startAt(`%${query}%`)
.endAt(query+"\uf8ff")
.once("value")
In this way, searching by "test" I could get the records having "Test 1, Contest, One test" as value in 'search' property from the database.
Firebase is noSQL therefore it does not have searches built in like you'll find in SQL. You can either sort by values/key or you can equalto
https://firebase.google.com/docs/database/android/retrieve-data
You can find examples at the link above. That is the latest documentation for firebase.
If you are looking for SQL like searches. Then take a look at elastic search. But that will increase the complexity since you need a platform to put it on. For that i could recommend Heroku or maybe GoogleCloudServers
Here is a blog post about advanced searches with elastic search
https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html
This question might be old but there is a documented way of how to achieve this way, It is simple to implement. Quoted:
To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document:
Algolia will be part of your firebase functions and will do all the searches you want.
// Update the search index every time a blog post is written.
exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(event => {
// Get the note document
const note = event.data.data();
// Add an 'objectID' field which Algolia requires
note.objectID = event.params.noteId;
// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(note);
});
To implement the search, the best way is to use instant search - android
Sample Search Image: GIF
The feature you're looking for is called full-text search and this is something most databases (including Firebase) don't provide out-of-the-box because it requires storing the data in a format that's optimized for text search (vs optimized for filtering) - these are two different problem sets with a different set of trade-offs.
So you would have to use a separate full-text search engine in conjunction with Firebase to be able to do this, especially if you need features like faceting, typo tolerance, merchandizing, etc.
You have a few options for a full-text search engine:
There's Algolia which is easy to get up and running but can get expensive quickly
There's ElasticSearch which has a steep learning curve but uber flexible
There's Typesense which aims to be an open source alternative to Algolia.
I don't know about the certainty of this approach but using the firebase version 10.2.6 on android, i get to do something like this:
firebaseDatabase.getReference("parent")
.orderByChild("childNode")
.startAt("[a-zA-Z0-9]*")
.endAt(searchString)
It seems to work well sometimes
Finally joined SO just to answer this.
For anyone coming here from/for the python firestore.client here's a solution that seems to work for me.
It's based on the accepted answer's concept but via the client rather than db.reference() and mixed with the answer from user12750908.
from firebase_admin import firestore
users = db.collection("users")\
.order_by("last_name")\
.where("last_name", ">=", last_name.upper())\
.where("last_name", "<=", last_name.lower() + "\uf8ff")\
.stream()
It works for the simple test I did, but I'll update my answer if I have issues with it later. And just a reminder, this is similar to
LIKE search%
and not
LIKE %search%.
Edit 1
I didn't see any tags for the question, but the title attribute mentions Android so this may not necessarily answer the question directly, but if you have a python API, this should work. I'm unfortunately not sure if there's an equivalent client/db separation in the Android version like there is in the Firebase Admin for Python. I didn't want to delete the answer since I hadn't seen any answers for firestore client during my search for a similar answer and hope it helps anyone else stumbling around.
Edit 09-03-2020 This works a portion of the time it seems. Most of the time I didn't seem to have an issue, but when I applied it to another project I was getting unexpected results. Long story short you may need to replicate how you save the data you're comparing against. For example, you may need to have a field to save the last_name in all caps and another field to save it in all lowercase, then you change the first where clause to compare last_name_upper and the second to compare last_name_lowercase. In my second project so far this seems to yield more accurate results so you may want to give that a try if the previous answer doesn't work well
EDIT 09-07-2020 Previous edit from 09-03-2020 is partially accurate. During my haste of thinking I had it fully resolved I completely forgot firebase doesn't let you use <, >, <=, >= across different fields. You may need to do two queries and merge them, but you'd probably still be reading more docs than you really intend. Doing the comparison against either the upper or lower version with the appropriate search term seems to give the original results expected from the original answer. For example:
.orderBy("last_name_upper")
.where("last_name_upper", ">=", this.searchForm.text.toUpperCase())
.where("last_name_upper", "<=", this.searchForm.text.toUpperCase() + "\uf8ff")
As firebase documentation, firebase doesn't support full text search.
But to do that you can use third-party tools.
Check this link to learn more https://firebase.google.com/docs/firestore/solutions/search

Getting the Suggestions from the Current Location when working with Google Places API

I'm working with Google Places API which gives the auto suggestions from the keyword mentioned in AutoCompleteTextView. Its working fine.
But when i enter some keyword(some text in the AutoCompleteTextView) it was giving the places according to the Current Location. but i need the places from the United States
Using the following Url to get the places
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=?&types=geocode&sensor=false&AIzaSyAIjrt28Os8Eyp0rVQbdkKNXNi5YrGx0AE
Example
"New" text entered
Giving the places near to my CurrentLocation..(Places near current location)
Expected Places from the UnitedStates.. (NewYork, NewBerlin, NewLenox)
Please can i get any luck
Thank you
Refering to this article you can pass a location and a radius as parameter to your query which should be a solution to your problem.
As location you could pass the middle of the United States and a radius that fits to all borders.
Taking this similar question into consideration this seems to be impossible other than the way I described it.

How to get all cities info over a route between two geo points

I have an question that while using Google Direction Api we can get the list of data like city's name, its Lat & Lng etc but the data provided by Api is limited to some extend. It's not able to provide all cities coming with-in that particular route.
E.g. If we try to go for Chandigarh to Delhi, then the route has a fixed result but when we try to reverse that same search i.e. Delhi to Chandigarh, some of the cities coming in previous result get vanished in api's new result, moreover, we just have a limited amount of locations/cities in result while we need the route completely detailed.
Do any of you guys faced this issue before? Is there any other way to match such requirement?
Hope I am understandable.
Thanks.
Use the below url:
https://maps.googleapis.com/maps/api/directions/json?origin="latvalue","longvalue"&destination="destnLat","destnLong"
&sensor=false&avoid=highways&mode=driving&alternatives=true
you have to set the alternatives value to true so it will result different routes available between the source and destination

prioritize within city response for address using maps.googleapis

As I am typing autocomplete address using http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=** I would like to prioritize within that city any address that begins with the following name .For ex: if I type mount and priority is California then it should give back Mountain view as the recommendation answer.How do I do that .I really appreciate any help.PLease let me know with respect to the above api call.Thanks in advance.
See https://developers.google.com/maps/documentation/geocoding/#Viewports
The short of it is that you can add a 'bounds' parameter that specifies lat-lng points to hint to the geocoder that you wish to look within a particular area. An example from that page is:
http://maps.googleapis.com/maps/api/geocode/json?address=Winnetka&bounds=34.172684,-118.604794|34.236144,-118.500938&sensor=false

Why is the `sll` query parameter for a Maps URL being (sort of) ignored?

According to various references, Google Maps has a query param called sll which does the following:
Latitude,longitude of the point from which the business search
should be performed. You could use this to perform an off-centre
busness search. Google use it so "link to this page" can record a map
that has had the centre moved after performing a business search. When
requesting a route
map.getBounds().getSouthWest().toUrlValue() returns the
same value maps.google.com uses.
So, for instance, if (43.464258, -80.52041) happens to be in Waterloo, Ontario, and (42.24370, -82.98320) happens to be in Windsor, Ontario (many hundreds of kilometers away), then my interpretation of the above documentation would be that the following query:
https://maps.google.ca/maps?saddr=43.464258,-80.52041&daddr=mcdonalds&sll=42.24370,-82.98320
will do a search for "Mcdonalds" from Waterloo, to some McDonald's in Windsor.
Unfortunately, that's not what happens (click for yourself to see!). It ends up doing a search from the saddr point, to the nearest McDonald's to that point. The value of sll doesn't factor into it at all.
But here's where it gets weird! If I omit the sll parameter entirely, then Maps does something completely unexpected: it picks some random McDonald's in Toronto (a city that is many kilometers away from either Waterloo or Windsor). I need to add some sll parameter (for any value) to make Maps return a result in Waterloo (even though what I'm asking for is a result in Windsor).
Am I misinterpreting all the various sources of documentation about this parameter? Is this a bug on Maps' end? And, most importantly, is there any working way to do what I'm trying to do, even if it doesn't involve the sll parameter at all?
I can get it to work with a simple query :
from:43.464258,-80.52041 to:mcdonalds near 42.24370,-82.98320
Which gives the expected result. It won't work without near which is a keyword.
You can also search from:43.464258,-80.52041 to:mcdonalds, Windsor if you have the city name.
About the sll (and sspn) parameters, it doesn't work for directions AFAIK. It only works with searches for a single location.

Categories

Resources