This Android tutorial is to learn about using Google Places API to find places nearby in Google maps. I will walk you through to create an Android app to find nearby places to you. There are two steps in this tutorial,
Google Places API is one among the many APIs provided by Google and this is to get geographic information about places using HTTP request. We need to pass the latitude and longitude coordinates of the location to know about the location.
To authenticate with the Google Places API, we need to get a API key. The type of key is “Server Key”. The steps are common for all the APIs. You should login to the Google API console and create a server key. You can refer the previous linked tutorial to for more detail.
The following example application first gets the current location and shows the Google map accordingly. Then based on user input the type of geographic location that needs to find is done. Example for keywords to find using the Google Places API are atm, hospital, airport, bank, etc.
Google Play Services is required for Google Maps and Google Places. If you are using Android Studio and Gradle, you should have the following dependencies added in build.grade file.
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:20.+' compile 'com.google.android.gms:play-services:6.1.+' }
Note the following permissions to be given for the app. The API_KEY specified here in the manifest is for Google Maps.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javapapers.android.googleplacesdetail"> <permission android:name="com.javapapers.android.googleplacesdetail.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.javapapers.android.googleplacesdetail.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".GooglePlacesActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzafSyBBDFDFxrYDadfaK-7HsCABg4LZpdk2wJnKZsfvq6A" /> </application> </manifest>
The API key specified here is for the Google Places API access and it’s a server key. We are using LocationListener
to get the current location and show in Google maps. Using the location received, we get the latitude and longitude and the form the Google Places API request http url.
package com.javapapers.android.googleplacesdetail; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; public class GooglePlacesActivity extends FragmentActivity implements LocationListener { private static final String GOOGLE_API_KEY = "AIzaSSDFSDF8Kv2eP0PM8adf5dSDFysdfas323SD3HA"; GoogleMap googleMap; EditText placeText; double latitude = 0; double longitude = 0; private int PROXIMITY_RADIUS = 5000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //show error dialog if GoolglePlayServices not available if (!isGooglePlayServicesAvailable()) { finish(); } setContentView(R.layout.activity_google_places); placeText = (EditText) findViewById(R.id.placeText); Button btnFind = (Button) findViewById(R.id.btnFind); SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); googleMap = fragment.getMap(); googleMap.setMyLocationEnabled(true); LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(bestProvider); if (location != null) { onLocationChanged(location); } locationManager.requestLocationUpdates(bestProvider, 20000, 0, this); btnFind.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String type = placeText.getText().toString(); StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); googlePlacesUrl.append("location=" + latitude + "," + longitude); googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS); googlePlacesUrl.append("&types=" + type); googlePlacesUrl.append("&sensor=true"); googlePlacesUrl.append("&key=" + GOOGLE_API_KEY); GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask(); Object[] toPass = new Object[2]; toPass[0] = googleMap; toPass[1] = googlePlacesUrl.toString(); googlePlacesReadTask.execute(toPass); } }); } private boolean isGooglePlayServicesAvailable() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (ConnectionResult.SUCCESS == status) { return true; } else { GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); return false; } } @Override public void onLocationChanged(Location location) { latitude = location.getLatitude(); longitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longitude); googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); googleMap.animateCamera(CameraUpdateFactory.zoomTo(12)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }
We use an AsyncTask to run the the request in background. It runs in a separate thread and requests the Google server for the Google Places information.
package com.javapapers.android.googleplacesdetail; import android.os.AsyncTask; import android.util.Log; import com.google.android.gms.maps.GoogleMap; public class GooglePlacesReadTask extends AsyncTask<Object, Integer, String> { String googlePlacesData = null; GoogleMap googleMap; @Override protected String doInBackground(Object... inputObj) { try { googleMap = (GoogleMap) inputObj[0]; String googlePlacesUrl = (String) inputObj[1]; Http http = new Http(); googlePlacesData = http.read(googlePlacesUrl); } catch (Exception e) { Log.d("Google Place Read Task", e.toString()); } return googlePlacesData; } @Override protected void onPostExecute(String result) { PlacesDisplayTask placesDisplayTask = new PlacesDisplayTask(); Object[] toPass = new Object[2]; toPass[0] = googleMap; toPass[1] = result; placesDisplayTask.execute(toPass); } }
Then we use another AsyncTask
to parse the HTTP result json object and show the places in Google map.
package com.javapapers.android.googleplacesdetail; import android.os.AsyncTask; import android.util.Log; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import org.json.JSONObject; import java.util.HashMap; import java.util.List; public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> { JSONObject googlePlacesJson; GoogleMap googleMap; @Override protected List<HashMap<String, String>> doInBackground(Object... inputObj) { List<HashMap<String, String>> googlePlacesList = null; Places placeJsonParser = new Places(); try { googleMap = (GoogleMap) inputObj[0]; googlePlacesJson = new JSONObject((String) inputObj[1]); googlePlacesList = placeJsonParser.parse(googlePlacesJson); } catch (Exception e) { Log.d("Exception", e.toString()); } return googlePlacesList; } @Override protected void onPostExecute(List<HashMap<String, String>> list) { googleMap.clear(); for (int i = 0; i < list.size(); i++) { MarkerOptions markerOptions = new MarkerOptions(); HashMap<String, String> googlePlace = list.get(i); double lat = Double.parseDouble(googlePlace.get("lat")); double lng = Double.parseDouble(googlePlace.get("lng")); String placeName = googlePlace.get("place_name"); String vicinity = googlePlace.get("vicinity"); LatLng latLng = new LatLng(lat, lng); markerOptions.position(latLng); markerOptions.title(placeName + " : " + vicinity); googleMap.addMarker(markerOptions); } } }
package com.javapapers.android.googleplacesdetail; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Places { public List<HashMap<String, String>> parse(JSONObject jsonObject) { JSONArray jsonArray = null; try { jsonArray = jsonObject.getJSONArray("results"); } catch (JSONException e) { e.printStackTrace(); } return getPlaces(jsonArray); } private List<HashMap<String, String>> getPlaces(JSONArray jsonArray) { int placesCount = jsonArray.length(); List<HashMap<String, String>> placesList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> placeMap = null; for (int i = 0; i < placesCount; i++) { try { placeMap = getPlace((JSONObject) jsonArray.get(i)); placesList.add(placeMap); } catch (JSONException e) { e.printStackTrace(); } } return placesList; } private HashMap<String, String> getPlace(JSONObject googlePlaceJson) { HashMap<String, String> googlePlaceMap = new HashMap<String, String>(); String placeName = "-NA-"; String vicinity = "-NA-"; String latitude = ""; String longitude = ""; String reference = ""; try { if (!googlePlaceJson.isNull("name")) { placeName = googlePlaceJson.getString("name"); } if (!googlePlaceJson.isNull("vicinity")) { vicinity = googlePlaceJson.getString("vicinity"); } latitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lat"); longitude = googlePlaceJson.getJSONObject("geometry").getJSONObject("location").getString("lng"); reference = googlePlaceJson.getString("reference"); googlePlaceMap.put("place_name", placeName); googlePlaceMap.put("vicinity", vicinity); googlePlaceMap.put("lat", latitude); googlePlaceMap.put("lng", longitude); googlePlaceMap.put("reference", reference); } catch (JSONException e) { e.printStackTrace(); } return googlePlaceMap; } }
package com.javapapers.android.googleplacesdetail; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Http { public String read(String httpUrl) throws IOException { String httpData = ""; InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try { URL url = new URL(httpUrl); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer = new StringBuffer(); String line = ""; while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); } httpData = stringBuffer.toString(); bufferedReader.close(); } catch (Exception e) { Log.d("Exception - reading Http url", e.toString()); } finally { inputStream.close(); httpURLConnection.disconnect(); } return httpData; } }
Comments are closed for "Find Places Nearby in Google Maps using Google Places API–Android App".
Amazing tutorial sir..
Thank you…
thank you ……
i followed the tutorial but after clicking the button, nothing happened..please help
same prob here.. i followed the tutorial and clicked the button.. nothing happened
hello sir after clicking the button, nothing happened…………
Plz update me we new code or correct code …..
I incorporated the code and nothing happened.
Hello Sir,
Please tell me how to get the contact number of near by police stations, By using place api I am getting the address, lat lng and other details about police station but this api is not returning the contact number.
Excellent Job. Thank You Very Much.
Same Problem Here… Everything works fine except after clicking find button it shows nothing.. Plz if anyone found the solution about this then plz post it & sir plz help us about this problem..
how to show maximum results its showing only one
It works perfectly fine, use your own API key and not the one provided, also make sure you use the Browser API key for google places as opposed to the Android API key. Make sure Google Maps API v2, Places API and Google Maps Engine API is turned on in your developers console.
Thanks a million, great tutorial!
when click the find button near by places not showed. is there any code to be added new? reply with some suggestions sir…
Thank so much for sharing the tutorials :-)
Browser api key???
I got server key, how do i get browser api key. When i click on find app crashes.
In the PlacesDisplayTask at:
Places placeJsonParser = new Places();
I am getting an error saying ‘The constructor Places() is not visible’
Please Help!!!!
Thank you
thanks. the tutorial working perfect
the api key shuold be changed two times
1)
In values google_maps_api,xml
your value
2)
In GooglePlacesActivity class
private static final String GOOGLE_API_KEY = “AIzaSyDDfPTAeQB2gDcwU6nIUkjIaKrbBkHR4ug”
try this i m sure its working after this
Hello. I have enabled all the APIs and made sure my API keys are changed in both locations. But, when I press the button it still won’t find anything. Any tips?
sir kindly provide me an code whre i can find out hospitals and govt local office and police station with use of google places api its an urgent thnk u in adv
which key i generate for android app..
If I am placing new elements in String.xml (suppose Rail Station) it results nothing.what to do? It is not possible that there is no railway station within 5000 m :(
how to find blood banks?
Sir,
I have followed your tutorial but whenever i click on the button the app crashes and i am not able to understand where i went wrong.
Great Tutorial! But I think I miss something as others… when I press the find button nothing happens… Any sugestions?
I use my own API key. Android Key and Browser key.
but it still show nothing.
can u help me, please.
@dewigim, can you check the trace and post the errors you get?
First, thank you for replying:)
I run the application on device.
it shows my location. but when I click find button, it doesn’t show the markers, I tried string “hospital”.
there is no error. I think the problem is key.
neither browser key nor server key worked.
I have executed this code many times it shows the same problem on clicking search button it force closes.I have checked in Logcat the only error it showing is on PlacesDisplayTask class onPostExecute Function except this all things are working fine…..so Please help me regarding this
Para todos los que dicen “no pasa nada al pulsar el boton Find” el problema seguramente sea que habeis puesto solamente un API KEY, se necesitan dos API KEY diferentes, en GooglePlacesActivity.java teneis que poner el API KEY del navegador no de android.
hello sir after clicking the button, nothing happened………please help me how to find near by hospitals using google map in android
hello sir after clicking the button, nothing happening, please help me how to find near by hospitals using google map in android
Hello,
I am getting null on getMap() method in GooglePlacesActivity.java
In the PlacesDisplayTask at:
Places placeJsonParser = new Places();
I am getting an error saying ‘The constructor Places() is not visible’
Please Help!!!!
Thank you
Sir i follow the program code but after run i press btn then nothing happen. please help
i am getting java.nullpointer exception
please help me
Thanks bro.. Awesome tutorial.. I am getting error when i give multiple words to search.. Could u help me regarding that?.. Thanks :)
sir if i want include extra like routes how can includes routes code????
when i use android key showing not authorised api key with empty reference.when i user browser key plaease enable services in developer.api android please help me.
Sir thank you for the tutorial. i love it
But one issue is that where is the activity_google_places layout file .. i cannot able to find it.
thank you for the tutorial.app runs perfectly but i am unable to find any places.it always shows map.what should i do. please help me on that issue.