Google places API - Can I separate out the output? - google-places-api

Hiting the endpoint:
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Hoboken%20NJ&fields=formatted_address,name&inputtype=textquery&key=xxxxxxxxxxxxxxxxxxx
Getting the result:
{
"candidates" : [
{
"formatted_address" : "New Jersey, USA",
"name" : "Hoboken"
}
],
"debug_log" : {
"line" : []
},
"status" : "OK"
}
What bugs me is that I can't find a way to separate out the region and country - Yes, I know I can parse the result myself. But is there an option I get shoot out to Google Places API to have the response separate out city/state(or region)/country in the returned JSON?
Something like:
{
"candidates" : [
{
"state" : "New Jersey",
"country" : "USA",
"name" : "Hoboken"
}
],
"debug_log" : {
"line" : []
},
"status" : "OK"
}

As far as I know, it isn't possible, you'll have to parse it. Places API is designed to search businesses and POIs at first place.
Google does have, however a geocoding API which seems to give out Postal Code, Country, State, Address, separetely.
There are also some free alternatives

Related

How to get original, not translated, name of the place using Google Places API?

I want to get info about some place, let's say I use this link https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJFfz5V9zi20ARwBqCYQXOIVM&fields=vicinity,name&key=MY_API_KEY, I will get this result
{
"html_attributions" : [],
"result" : {
"name" : "Try Bobry",
"vicinity" : "Dmytra Yavornytskoho Avenue, 42, Dnipro"
},
"status" : "OK"
}
but the original name of this place is Три бобри, not Try Bobry.
You say I can use language=uk parameter to get this name, but if I will use it - it will break another place, which original name is in English, for example this https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJHZkI69zi20ARfbLn6MV6S9o&fields=vicinity,name&key=MY_API_KEY&language=uk will return
{
"html_attributions" : [],
"result" : {
"name" : "Візьміть каву, щоб піти",
"vicinity" : "вулиця Барикадна, 5-7, Дніпро́"
},
"status" : "OK"
}
so it will be Візьміть каву, щоб піти instead of Take coffee to go.
All I want is to get places' names in the language of origin, not translated.

Problems accessing _source fields with a dot in the name when creating Slack action for Elasticsearch Watcher

I am trying to create a Slack action with a dynamic attachment. My _source looks like this:
{
"user.url": "https://api.github.com/users/...",
"user.gists_url": "https://api.github.com/users/.../gists{/gist_id}",
"user.repos_url": "https://api.github.com/users/.../repos",
"date": "2018-04-27T14:34:10Z",
"user.followers_url": "https://api.github.com/users/.../followers",
"user.following_url": "https://api.github.com/users/.../following{/other_user}",
"user.id": 123456,
"user.avatar_url": "https://avatars0.githubusercontent.com/u/123456?v=4",
"user.events_url": "https://api.github.com/users/.../events{/privacy}",
"user.site_admin": false,
"user.html_url": "https://github.com/...",
"user.starred_url": "https://api.github.com/users/.../starred{/owner}{/repo}",
"user.received_events_url": "https://api.github.com/users/.../received_events",
"metric": "stars",
"user.login": "...",
"user.type": "User",
"user.subscriptions_url": "https://api.github.com/users/.../subscriptions",
"user.organizations_url": "https://api.github.com/users/.../orgs",
"user.gravatar_id": ""
}
and here is my Slack action
"actions": {
"notify-slack": {
"throttle_period_in_millis": 240000,
"slack": {
"account": "monitoring",
"message": {
"from": "Elasticsearch Watcher",
"to": [
"#watcher"
],
"text": "We have {{ctx.payload.new.hits.total}} new stars! And {{ctx.payload.old.hits.total}} in total.",
"dynamic_attachments" : {
"list_path" : "ctx.payload.new.hits.hits",
"attachment_template" : {
"title" : "{{_source.[\"user.login\"]}}",
"text" : "Users Count: {{count}}",
"color" : "{{color}}"
}
}
}
}
}
I can't seem to figure out how to access my _source fields since they have dots in them. I have tried:
"{{_source.[\"user.login\"]}}"
"{{_source.user.login}}"
"{{_source.[user.login]}}"
"{{_source.['user.login']}}"
The answer to my question is that you can't access _source keys with dots in them directly using mustache, you must first transform your data.
Update:
I was able to get this working by using a transform to build a new object. Mustache might not be able to access fields with dots in their names, but painless can! I added this transform to my slack object:
"transform" : {
"script" : {
"source" : "['items': ctx.payload.new.hits.hits.collect(user -> ['userName': user._source['user.login']])]",
"lang" : "painless"
}
}
and now in the slack action dynamic attachments, I can access the items array:
"dynamic_attachments" : {
"list_path" : "ctx.payload.items",
"attachment_template" : {
"title" : "{{userName}}",
"text" : "{{_source}}"
}
}
Old Answer:
So according to this Watcher uses mustache.
and according to this mustache can't access fields with dots in the names.

Querying for specific strings in Firebase?

I have a database which looks something like this:
"trucks" : {
"3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
"email" : "sandwiches#123.com",
"name" : "Sandwich Truck",
"phone" : "123 - 456 - 1234",
"provider" : "password",
"selfDescription" : "We serve delicious sandwiches at a moderate price. Cards Accepted.",
"userType" : "truck",
"website" : "www.sandwiches.com"
},
"54fea8cd-2203-46bd-aaf8-9d823e85313d" : {
"email" : "pizza#123.com",
"name" : "Supa Pizza",
"phone" : "619 - 222 - 4444",
"provider" : "password",
"selfDescription" : "We serve incredible pizza at an incredibly unfair price.",
"userType" : "truck",
"website" : ""
},
"6c542367-507c-4d01-af2c-bf93a7efaef4" : {
"email" : "fries#123.com",
"name" : "Pete's Fries",
"phone" : "11111111111111111111",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We make some of the world's most delicious fries.",
"userType" : "truck",
"website" : ""
},
"7c6c4395-aec1-443c-908d-62db517def5e" : {
"email" : "chili#123.com",
"name" : "Mark's Chili",
"phone" : "1-800-CHIL-LLL",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We serve the most delicious chili, chili your mamma's mamma is scared to try.",
"userType" : "truck",
"website" : ""
}
}
I'd like to implement a search bar for which trucks whose names match the search terms will be returned. For example, any truck name containing the string 'fries' will have its ID committed to an array of search result IDs.
Here's what I've tried so far, but no dice. When I type in fries in my searchbar and hit the search button, it does not print "6c542367-507c-4d01-af2c-bf93a7efaef4"
let usersRef = Firebase(url: "https://•••••••••.firebaseIO.com/users")
usersRef.queryOrderedByChild("name").queryEqualToValue(searchBar.text).observeEventType(.ChildAdded, withBlock:{
snapshot in
print(snapshot.key)
})
I'm not even sure how close I am, any help would be massively appreciated.
Thanks!
You are querying using queryEqualToValue (Query.equalTo()), which will return result only if there is an exact match.
Explanation in Detail:
In the search bar if we are entering "fries" it will look for an exact match of "fries", which is not available in our document and hence the desired value "6c542367-507c-4d01-af2c-bf93a7efaef4" is not printed.
Instead if we give the value "Pete's Fries" in the search bar then we will get the result "6c542367-507c-4d01-af2c-bf93a7efaef4"
If we give partial value for our search, then we are trying to implement a search which is similar to LIKE Query in SQL. Please refer the below posts to get more info on "how to perform LIKE query in firebase"
How to perform sql "LIKE" operation on firebase?

Google Places API Web Service shows photo_reference, but Javascript library doesn't

I'm building a basic places search page and I'm using the Google Places Javascript library for it (text search). I do get one "photo" element back, but I noticed that the "photo_reference" field is empty. Here a sample request:
var service = new google.maps.places.PlacesService(this.map);
service.textSearch({query: 'harbour bridge'}, this.callback.bind(this));
Repsonse:
{
"formatted_address":"Sydney Harbour Bridge, Sydney NSW, Australia",
"geometry":{
"location":{
"lat":-33.8523063,
"lng":151.21078710000006
}
},
"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id":"e22913360d0b946d099c7a32a77a95e49f9ead66",
"name":"Sydney Harbour Bridge",
"photos":[{
"height":4000,
"html_attributions":["Jasper Straver"],
"width":6000
}],
"place_id":"ChIJ49XqJV2uEmsRPsTAF7eOlGg",
"rating":4.7,
"reference":"[...]",
"types":["point_of_interest", "establishment"],
"html_attributions":[]
}
If I do the same request using the web service, I get the photo_reference. Example:
https://maps.googleapis.com/maps/api/place/textsearch/json?key=[...]&query=harbour%20bridge
Response:
{
"formatted_address" : "Sydney Harbour Bridge, Sydney NSW, Australia",
"geometry" : {
"location" : {
"lat" : -33.8523063,
"lng" : 151.2107871
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "e22913360d0b946d099c7a32a77a95e49f9ead66",
"name" : "Sydney Harbour Bridge",
"photos" : [
{
"height" : 4000,
"html_attributions" : ["\u003ca href=\"https://maps.google.com/maps/contrib/113178678511744469415/photos\"\u003eJasper Straver\u003c/a\u003e"],
"photo_reference" : "CmRdAAAAY-WUame_CRFnMFmUN4UlvFHI7o3tQOqXJxTkjQINgzMQOOheBzLPIm43dlIAIkhFyugFAw8fnf-ItEiUp1j48B23sCDFRtCWM123euhDif_P1jYkvFAjDrPxq1rCnmi2EhCt6LpVl5W-AKPLRkW_tzs6GhRlG4dx2CVuTNZZMFFo3eYMSFWzGg",
"width" : 6000
}
],
"place_id" : "ChIJ49XqJV2uEmsRPsTAF7eOlGg",
"rating" : 4.7,
"reference" : "[...]",
"types" : [ "point_of_interest", "establishment" ]
}
How come there is a difference between the JS and Web Services API? How can I get the photo_reference from the JS library? (I don't want to make a getDetails(...) request for each search result)
Thanks!
photo_reference is for the Google Places API Web Service. With the Places Javascript library you can just call getUrl on the elements of the photos array, e.g. place.photos[0].getUrl({maxWidth: 1000}).
Like the Web Service, search results won't have more than 1 photo.

How to get elasticsearch suggestions working on multiple fields from multiple types?

I have a requirement to have a search box globally on the website that user can type anything in, a bit like google. As the user is typing along, he should get suggestions. I have multiple types in an Index, I am using Completion suggester to get suggestions from one field like below:
GET /index/_suggest/
{
"person-suggest" : {
"text" : "m",
"completion" : {
"field" : "nameSuggest"
}
}
}
The requirement is such that when they type a person name which is stored in person type or type in a company name which is stored in company type...both suggestions should appear. Also, within the same type, it should be able to suggest based on multiple fields not just one like what I've got. And finally once the user selects a suggestion, do a search and show facets which are based on multiple types.
For multi-type suggestions on multiple fields, use-
curl -XPOST 'localhost:9200/indexName/type1,type2/_suggest' -d '{
"my-suggestion-1" : {
"text" : "some text",
"term" : {
"field" : "field1"
}
},
"my-suggestion-2" : {
"text" : "some text",
"term" : {
"field" : "field2"
}
}
}'

Resources