{"id":2092,"date":"2015-02-06T23:04:03","date_gmt":"2015-02-06T17:34:03","guid":{"rendered":"http:\/\/codetheory.in\/?p=2092"},"modified":"2015-02-07T15:35:13","modified_gmt":"2015-02-07T10:05:13","slug":"google-place-api-autocomplete-service-in-android-application","status":"publish","type":"post","link":"https:\/\/codetheory.in\/google-place-api-autocomplete-service-in-android-application\/","title":{"rendered":"Google Place API Autocomplete Service in Android Application"},"content":{"rendered":"

Recently in one of my android applications I wanted to obtain the user’s location in terms of city and country that they can feed from their (edit) profile section. So one way to do this is basically have two dropdowns or dialogs (or even open an entirely new activity where the user can search through entities and select one). One of them would display all the countries in which, once a selection is made, the other one will show a restricted set of cities based on the prior country selection. Now the number of cities in the world is large, so to do this we’ll need to get a database that contains all the countries and cities and then make sure we can query that over HTTP to get the cities based on what the user types into the app (autocomplete box). We’ve to make sure the response is really quick and doesn’t cause lags. We can also bundle all the city and country data into our app but then that’ll blow up the apk size.<\/p>\n

<\/p>\n

Anyway, while searching the web and trying out apps like Airbnb<\/a> and Couchsurfing<\/a> I figured Google has the Place Autocomplete service<\/a> using which I can allow the user to type in a city name which’ll fetch all the cities containing that string from Google’s servers that can be presented in an AutoCompleteTextView<\/a>.<\/p>\n

\"Google<\/p>\n

So let’s see how to execute this.<\/p>\n

Getting an API Key<\/h2>\n

The first thing to do is to get an API key from Google Developers Console. More on this here<\/a>. You’ll find four different key options – server, browser, android and iOS – you should go for the browser one as according to their documentation Places API doesn’t work with Android or iOS API key and since the app is like a browser client, a server key doesn’t make sense.<\/p>\n

Know your Limits<\/h2>\n

You should know about the usage limitation. All the information pertaining to that is available here<\/a>.<\/p>\n

Place Autocomplete Requests and Responses<\/h2>\n

You should go through the Place Autocomplete service documentation<\/a> where you can learn everything about the requests and responses. Basically you’ll have to make a request to https:\/\/maps.googleapis.com\/maps\/api\/place\/autocomplete\/output?parameters<\/code> where output<\/code> can be either json<\/code> or xml<\/code> indicating the type of expected response. There are 2 required GET<\/code> parameters which are input<\/code> that should contain the string (that the user will type) to use as the search term and key<\/code> which should contain your API key. So then the URL will look something like this:<\/p>\n

\r\nhttps:\/\/maps.googleapis.com\/maps\/api\/place\/autocomplete\/json?input=ban&key=API_KEY\r\n<\/pre>\n

There are other optional parameters that you can read up on but the most important one that you’ll mostly use is the types<\/code> parameter. You can pass various values to types<\/code> like:<\/p>\n