Google auto complete places, Omit country name of users location from predictions - google-maps

I am using google.maps.places.AutocompleteService(); to get query predictions. Is it possible to omit country name from the predictions list of users location. For eaxmple. if i am in united states, when i search for sanfransisco. It should not show United states (Country name) in predictions list. But if i search for location- delhi in india, it should show 'Delhi India'(country name).
Is this possible using this service or any other google api service.
Note that, i need list of suggestion, not something tied with a textfield.
Your help will be greatly appreciated.

This is not possible without manually processing the results. So after receiving the json data as a response, you need to check if the suggested country is the same as the user's country and if it is then do not display it in the textfield.
A JSON response looks like this: In the value field you can check the country and decide whether or not to display it by comparing it to the user's country that needs to be set somewhere in your App.
I believe the JSON response will be save in a List<HashMap<String, String>> So you can loop over it and omit the country.
{
"status": "OK",
"predictions" : [
{
"description" : "Paris, France",
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
"matched_substrings" : [
{
"length" : 5,
"offset" : 0
}
],
"place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ",
"reference" : "CjQlAAAA_KB6EEceSTfkteSSF6U0pvumHCoLUboRcDlAH05N1pZJLmOQbYmboEi0SwXBSoI2EhAhj249tFDCVh4R-PXZkPK8GhTBmp_6_lWljaf1joVs1SH2ttB_tw",
"terms" : [
{
"offset" : 0,
"value" : "Paris"
},
{
"offset" : 7,
"value" : "France"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Paris Avenue, Earlwood, New South Wales, Australia",
"id" : "359a75f8beff14b1c94f3d42c2aabfac2afbabad",
"matched_substrings" : [
{
"length" : 5,
"offset" : 0
}
],
"place_id" : "ChIJrU3KAHG6EmsR5Uwfrk7azrI",
"reference" : "CkQ2AAAARbzLE-tsSQPgwv8JKBaVtbjY48kInQo9tny0k07FOYb3Z_z_yDTFhQB_Ehpu-IKhvj8Msdb1rJlX7xMr9kfOVRIQVuL4tOtx9L7U8pC0Zx5bLBoUTFbw9R2lTn_EuBayhDvugt8T0Oo",
"terms" : [
{
"offset" : 0,
"value" : "Paris Avenue"
},
{
"offset" : 14,
"value" : "Earlwood"
},
{
"offset" : 24,
"value" : "New South Wales"
},
{
"offset" : 41,
"value" : "Australia"
}
],
...
Reference: https://developers.google.com/places/webservice/autocomplete#location_biasing

Related

Google Maps Places Autocomplete - Not Returning Street Numbers?

I'm looking to build a pretty basic address search functionality inside of my iOS app. I'm using Google Places Autocomplete to query for results based on users input, like below:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=shoppers#&radius=160934&location=50.445210,-104.618896&components=country:ca&key=xxxxxxxxxxxxx
The results I'm getting back are far too vague to be of practical use. Below is one example, where all I get is the name of the business and the street that it is on. Problem being, there are three different locations for this business on the same street, so how would the user be able to figure out which one they're selecting without being provided a street number?
{
"description" : "Shoppers Drug Mart, Albert Street, Regina, SK, Canada",
"id" : "94065830c922a4239ece26102fb3439fa2f2e155",
"matched_substrings" : [
{
"length" : 8,
"offset" : 0
}
],
"place_id" : "ChIJ4XzAGbCgHlMRGSFBsEbyfCE",
"reference" : "ChIJ4XzAGbCgHlMRGSFBsEbyfCE",
"structured_formatting" : {
"main_text" : "Shoppers Drug Mart",
"main_text_matched_substrings" : [
{
"length" : 8,
"offset" : 0
}
],
"secondary_text" : "Albert Street, Regina, SK, Canada"
},
"terms" : [
{
"offset" : 0,
"value" : "Shoppers Drug Mart"
},
{
"offset" : 20,
"value" : "Albert Street"
},
{
"offset" : 35,
"value" : "Regina"
},
{
"offset" : 43,
"value" : "SK"
},
{
"offset" : 47,
"value" : "Canada"
}
],
"types" : [ "pharmacy", "health", "point_of_interest", "store", "establishment" ]
},
Places Autocomplete is not designed to return detailed address information. It is designed to return predictions based on your input. You then have to use the Place ID that gets returned and make a Place Details request using it to get a response that includes address information, etc.
Using the first result from your autocomplete request, I can form a Place Details request like:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJ4XzAGbCgHlMRGSFBsEbyfCE&key=YOUR_API_KEY
That will give the information you desire.
https://developers.google.com/places/web-service/autocomplete#place_autocomplete_results
https://developers.google.com/places/web-service/place-id#example-using-the-places-api

UISearchBar and JSON Data

This is my first time using a UISearchBar and it is proving to confuse me.
I think my biggest problem here is not understanding how JSON is stored and displayed.
I have my JSON data stored as:
[{
"referralId" : "v-1519014616",
"name" : "Empire State Building",
"storeId" : "",
"hereNow" : {
"count" : 2,
"summary" : "2 people are here",
"groups" : [
{
"count" : 2,
"type" : "others",
"name" : "Other people here",
"items" : [
]
}
]
},
"stats" : {
"tipCount" : 1105,
"checkinsCount" : 185916,
"usersCount" : 129661
},
"venueRatingBlacklisted" : true,
"beenHere" : {
"lastCheckinExpiredAt" : 0
},
"specials" : {
"count" : 0,
"items" : [
]
},
"venuePage" : {
"id" : "64514349"
},
"verified" : true,
"location" : {
"state" : "NY",
"neighborhood" : "Midtown Manhattan, New York, NY",
"crossStreet" : "btwn 33rd & 34th St",
"lat" : 40.748469532965927,
"address" : "350 5th Ave",
"cc" : "US",
"city" : "New York",
"postalCode" : "10118",
"formattedAddress" : [
"350 5th Ave (btwn 33rd & 34th St)",
"New York, NY 10118"
],
"lng" : -73.985513794430119,
"distance" : 17,
"country" : "United States"
},
"hasPerk" : false,
"id" : "43695300f964a5208c291fe3",
"categories" : [
{
... etc
},
I have this stored in an array titled locations and I am trying to filter through it with a search bar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let name = self.locations[0]["name"].string
filteredLocations = locations.filter { names in
return (name!.lowercased().contains(searchText.lowercased()))
}
tableView.reloadData()
}
Now, obviously, name is only going to search for the item in the array. How would I be able to filter through every location's name key to properly filter?
try this one ... don't take name first from location array...
let filterArr = locations.filter {
return (($0["name"] as! String).lowercased().contains(searchText.lowercased())))
}
use this one to get the search result

MongoDB Change/Update Subdocument Field

I currently have a Mongo DB database with one collection ('locations') with one document:
{
"_id" : ObjectId("5875653b89513c8328416522"),
"name" : "Starcups",
"address" : "125 High Street, Reading, RG6 1PS",
"rating" : 3,
"facilities" : [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords" : [
-0.9690884,
51.455041
],
"openingTimes" : [
{
"days" : "Monday - Friday",
"opening" : "7:00am",
"closing" : "7:00pm",
"closed" : false
},
{
"days" : "Saturday",
"opening" : "8:00am",
"closing" : "5:00pm",
"closed" : false
},
{
"days" : "Sunday",
"closed" : true
}
],
"reviews" : [
{
"author" : "Simon Holmes",
"id" : ObjectId("5875663389513c8328416523"),
"rating" : 5,
"timestamp" : ISODate("2013-07-15T23:00:00Z"),
"reviewText" : "What a great place. I can't say enough good things about it."
}
]
}
I need to alter the field "id" (part of the subdocument reviews) to "_id". I have tried, using other similar examples on StackExchange, the following code to no avail:
db.locations.update({}, {$rename:{"reviews.id":"reviews._id"}}, false, true);
But I receive the following error:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (reviews of reviews.id) to traverse the element ({reviews: [ { author: \"Simon Holmes\", id: ObjectId('5875663389513c8328416523'), rating: 5.0, timestamp: new Date(1373929200000), reviewText: \"What a great place. I can't say enough good things about it.\" } ]})"
}
})
I get the same error when I try to alter any other field. Could someone point me in the right direction?
Update:
This seems to be a problem assessing the subdocument field as the following code executes fine:
db.locations.update({}, {$rename:{"name":"names"}}, false, true);
I have also tried searching through the relevant documentation: https://docs.mongodb.com/manual/reference/operator/update/rename/
That's because Reviews is an Array
You cannot $rename to do that , instead you need to $set the new name and $unset the old one

How do I query a nested object in MongoDB?

I have a JSON schema that looks like this:
{
"_id" : ObjectId("5692a3e124de1e0ce2dfda22"),
"title" : "A Decade of Decadence, Pt. 2: Legacy of Dreams",
"year" : 2013,
"rated" : "PG-13",
"released" : ISODate("2013-09-13T04:00:00Z"),
"runtime" : 65,
"countries" : [
"USA"
],
"genres" : [
"Documentary"
],
"director" : "Drew Glick",
"writers" : [
"Drew Glick"
],
"actors" : [
"Gordon Auld",
"Howie Boulware Jr.",
"Tod Boulware",
"Chen Drachman"
],
"plot" : "A behind the scenes look at the making of A Tiger in the Dark: The Decadence Saga.",
"poster" : null,
"imdb" : {
"id" : "tt2199902",
"rating" : 8,
"votes" : 50
},
"awards" : {
"wins" : 0,
"nominations" : 0,
"text" : ""
},
"type" : "movie"
}
I am trying to find a movie released in 2013, that is rated PG-13 and has won no awards. I tried the following query in my Mongo Shell but no luck:
db.movieDetails.find({rated: "PG-13", year:2013, awards: { wins : 0}})
Any ideas?
From the documentation:
When the field holds an embedded document, a query can either specify an exact match on the embedded document or specify a match by individual fields in the embedded document using the dot notation
db.movieDetails.find( { "rated": "PG-13", "year": 2013, "awards.wins" : 0 } )
if this PG-13 rating query would work for you:
db.movieDetails.find({"rated": "PG-13"})
then i would try something like this:
db.movieDetails.find({$and: [{"rated": "PG-13"}, {"year": 2013}, {"awards": { "wins" : 0}}]} )

Google places API hierarchy - neighbourhood/suburb -> city -> region -> country

Problem -
people posting in various places based on their interest while other people are searching for posts in greater areas.
e.g. - user A posts under Bondi Beach, a suburb of Sydney.
user B searches for posts under Sydney.
Desired outcome - user B gets to see user A's post since Bondi is located inside Sydney.
Solution:
Find in Google places API where it tells you that suburb X is of city Y and so forth.
In my example - I would like to get information about Bondi that tells me to which city it belongs.
Here is my simple query for Bondi Beach (I use auto complete because it's an autocomplete dropdown):
https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=true&input=bondi%20beach&key=myKey
{
"predictions" : [
{
"description" : "Bondi Beach, New South Wales, Australia",
"id" : "ffa6ad484d2fec594378fb2a24e8328a2bc4459f",
"matched_substrings" : [
{
"length" : 11,
"offset" : 0
}
],
"place_id" : "ChIJWTi8xgGyEmsRgK0yFmh9AQU",
"reference" : "CkQ_AAAAMnfwZeK5SV6uPBnPr34JVdHudAGxMtna--JWHtwe7t81gLBGgsB6mRgmj375m7KReRhpMq1Y6AYxCrC2MnYn8hIQQfASvoG5-7LwRcVR7ELjNxoURh-iBs6M2dmrU8wLKIPcFFgymn4",
"terms" : [
{
"offset" : 0,
"value" : "Bondi Beach"
},
{
"offset" : 13,
"value" : "New South Wales"
},
{
"offset" : 30,
"value" : "Australia"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Bondi Beachouse YHA, Fletcher Street, Bondi Beach, New South Wales, Australia",
"id" : "fdab345a9db5c6cfcddd7cd561a8b83acc538d73",
"matched_substrings" : [
{
"length" : 11,
"offset" : 0
}
],
"place_id" : "ChIJzwpdVYStEmsRd6F0Wa1NQ2I",
"reference" : "CmRbAAAA_AFGY8xaYlQebohzYWKrvNoMiJAktObvU4Vb03ORpmf7-ZTlv3BkU3dJPdAIBMtgol8ngdxC1NhVZA7AsAnkRfe4WPT7GJlqkIa_S0Hsb87Dlt-OT1Dt5uQNg_ei4cRPEhCvI7pwL5qD1US6yrOxvVpXGhRQxfEObV1zUxys2a2EZTkbKUdMbg",
"terms" : [
{
"offset" : 0,
"value" : "Bondi Beachouse YHA"
},
{
"offset" : 21,
"value" : "Fletcher Street"
},
{
"offset" : 38,
"value" : "Bondi Beach"
},
{
"offset" : 51,
"value" : "New South Wales"
},
{
"offset" : 68,
"value" : "Australia"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Bondi Beach Car Park, Campbell Parade, Bondi Beach, New South Wales, Australia",
"id" : "ba6c34f150f7f3ac035d731662f2a08a22a3b5dd",
"matched_substrings" : [
{
"length" : 11,
"offset" : 0
}
],
"place_id" : "ChIJXch67J6tEmsRFXD11Ce64WA",
"reference" : "CmRcAAAA_H6cxlTUVW2JHFb5EO0MQAL-d1M-_6_DXOQAvw6xJOFSri4HaIvodY6lcZH3F8rrAlyaXqOM4lcgbegqvVEPScyRE20iiJSXC6jHPxH5EwplZGPWx7rxy34l1uweXgmhEhBirrakiRpK-Zd_naBW7Hd8GhTiujZAiOEDsWj9tuP3bumLR6SamQ",
"terms" : [
{
"offset" : 0,
"value" : "Bondi Beach Car Park"
},
{
"offset" : 22,
"value" : "Campbell Parade"
},
{
"offset" : 39,
"value" : "Bondi Beach"
},
{
"offset" : 52,
"value" : "New South Wales"
},
{
"offset" : 69,
"value" : "Australia"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Bondi Beach Public School, Campbell Parade, Bondi Beach, New South Wales, Australia",
"id" : "e808bb9453705761b5b1672065a5aaa5b1b5b52a",
"matched_substrings" : [
{
"length" : 11,
"offset" : 0
}
],
"place_id" : "ChIJaXfeKJ6tEmsRM_pgTjpurLs",
"reference" : "CnRiAAAAN-CRd33lyhCZBryEwtPxVdxweAC-WcxikMSlmiP83st3Eu0qt5tOdx_Kxvfb6aTH_F3szZETbrRYSXb8-Ql-NSdXFhIeZjIf3au9qrwIII7y9-ZhFRImHCeYRJ1xslrS2dal0_lulOh_dY39jgofrRIQoVco7kZBb4DgSaACK9h4ARoUf98GpsdtuHPVY615rIszSUVqRpM",
"terms" : [
{
"offset" : 0,
"value" : "Bondi Beach Public School"
},
{
"offset" : 27,
"value" : "Campbell Parade"
},
{
"offset" : 44,
"value" : "Bondi Beach"
},
{
"offset" : 57,
"value" : "New South Wales"
},
{
"offset" : 74,
"value" : "Australia"
}
],
"types" : [ "establishment" ]
},
{
"description" : "Bondi Beach View, Campbell Parade, Sydney, New South Wales, Australia",
"id" : "16758032a0384967c04985914cd861dc2c488fe8",
"matched_substrings" : [
{
"length" : 11,
"offset" : 0
}
],
"place_id" : "ChIJr6fcCputEmsRtFcOcREuc48",
"reference" : "CmRUAAAAK3dQSHexE9SerblRCR84jdJ8ScqqDwteRMH2H1f_uCRVQNVsFmgELlMlWWCG2mMbbNOL0yQRv2K0Bx2aqKIe3Bt236LvDl5sxjDaRvdd7QTdBATMJsTHec-xXc3z9c02EhC-JDoD5ln1Q-qdeb-urugLGhSur_7TThy4ekl9yoXRFzZwZmn26w",
"terms" : [
{
"offset" : 0,
"value" : "Bondi Beach View"
},
{
"offset" : 18,
"value" : "Campbell Parade"
},
{
"offset" : 35,
"value" : "Sydney"
},
{
"offset" : 43,
"value" : "New South Wales"
},
{
"offset" : 60,
"value" : "Australia"
}
],
"types" : [ "establishment" ]
}
],
"status" : "OK"
}
As you can see, the only result that tells me that Bondi is part of Sydney is the "Bondi Beach View", which doesn't make sense nor does it help solve my problem.
I experience this issue with many other places all over the world.
How can I get accurate info of places hierarchy ?
I had similar problem. I did some research and didn't find accurate solution. Currently I'm using the following workaround which is good enough for my case.
First, get coordinates of a place (place_id you can find in autocomplete results):
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJWTi8xgGyEmsRgK0yFmh9AQU&key=yourKey
After that execute reverse geocoding requests with these coordinates and desired result_types: country, administrative_area_level_1, administrative_area_level_2, locality.
For example:
https://maps.googleapis.com/maps/api/geocode/json?latlng=-33.8914755,151.2766845&result_type=locality&key=yourKey
This is not 100% accurate approach. It gives more than one results for some locations (as you can see, Bondi Beach is one of them), but you can try to choose one by some other attributes (in this case, the result with colloquial_area type is correct one).
I hope, my answer will help you at least a little bit.
You can just
console.log(places.address_components)
then use check the console on the browser.
It will show the hierarchy and the keys you need to access the information you want. Suburb/Neighbourhood should show up as
'sublocality_level_1'