Select Current Place and Show Details on a Map

This tutorial shows you how to find the current location of an Android device and display details of the place (a business or other point of interest) at that location. Follow this tutorial to build an Android app using the Maps SDK for Android, the Places SDK for Android, and the fused location provider in the Google Play services location APIs.

Get the code

Clone or download the Google Maps Android API v2 Samples repository from GitHub.

Set up your development project

Follow these steps to create the tutorial project in Android Studio.

  1. Download and install Android Studio.
  2. Add the Google Play services package to Android Studio.
  3. Clone or download the Google Maps Android API v2 Samples repository if you didn't do that when you started reading this tutorial.
  4. Import the tutorial project:

    • In Android Studio, select File > New > Import Project.
    • Go to the location where you saved the Google Maps Android API v2 Samples repository after downloading it.
    • Find the CurrentPlaceDetailsOnMap project at this location:
    • Select the project directory, then click OK. Android Studio now builds your project, using the Gradle build tool.

Get an API key and enable the necessary APIs

To complete this tutorial, you need a Google API key that's authorized to use the Maps SDK for Android and the Places SDK for Android.

Click the button below to get a key and activate the APIs.

Get Started

For more details, see the full guide to getting an API key.

Add the API key to your app

  1. Edit your project's file.
  2. Paste your API key into the value of the GOOGLE_MAPS_API_KEY property:


    When you build your app, Gradle copies the API key into the app's Android manifest. The app's build.gradle file contains the following line, which maps the string google_maps_key in the manifest to the gradle property GOOGLE_MAPS_API_KEY:

    resValue "string", "google_maps_key",
            (project.findProperty("GOOGLE_MAPS_API_KEY") ?: "")

Build and run your app

  1. Connect an Android device to your computer. Follow the instructions to enable developer options on your Android device and configure your system to detect the device. (Alternatively, you can use the Android Virtual Device (AVD) Manager to configure a virtual device. When choosing an emulator, make sure you pick an image that includes the Google APIs. For more details, see the getting started guide.)
  2. In Android Studio, click the Run menu option (or the play button icon). Choose a device as prompted.

Android Studio invokes Gradle to build the app, and then runs the app on the device or on the emulator. You should see a map with a number of markers centered around your current location, similar to the image on this page.


  • If you don't see a map, check that you've obtained an API key and added it to the app, as described above. Check the log in Android Studio's Android Monitor for error messages about the API key.
  • If the map shows just a single marker located on Sydney Harbour Bridge (the default location specified in the app), check that you've granted location permission to the app. The app prompts for location permission at run time, following the pattern described in the Android permissions guide. Note that you can also set permissions directly on the device, by choosing Settings > Apps > app name > Permissions > Location. For details on how to handle permissions in your code, see the guide below to requesting location permission in your app.
  • Use the Android Studio debugging tools to view logs and debug the app.

Understand the code

This part of the tutorial explains the most significant parts of the CurrentPlaceDetailsOnMap app, to help you understand how to build a similar app.

Instantiate the Places API clients

The following interfaces provide the primary entry points to the Places SDK for Android:

  • The GeoDataClient provides access to Google's database of local place and business information.
  • The PlaceDetectionClient provides quick access to the device's current place, and offers the opportunity to report the location of the device at a particular place.

The LocationServices interface is the main entry point for Android location services.

To use the APIs, instantiate GeoDataClient, PlaceDetectionClient, and FusedLocationProviderClient in your fragment's or activity's onCreate() method, as shown in the following code sample:

protected void onCreate(Bundle savedInstanceState) {

    // Construct a GeoDataClient.
    mGeoDataClient = Places.getGeoDataClient(this, null);

    // Construct a PlaceDetectionClient.
    mPlaceDetectionClient = Places.getPlaceDetectionClient(this, null);

    // Construct a FusedLocationProviderClient.
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

Request location permission

Your app must request location permission in order to determine the location of the device and to allow the user to tap the My Location button on the map.

This tutorial provides the code you need to request fine location permission. For more details, see the guide to Android permissions.

  1. Add the permission as a child of the <manifest> element in your Android manifest:

    <manifest xmlns:android=""
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  2. Request runtime permissions in your app, giving the user the opportunity to allow or deny location permission. The following code checks whether the user has granted fine location permission. If not, it requests the permission:

    private void getLocationPermission() {
         * Request location permission, so that we can get the location of the
         * device. The result of the permission request is handled by a callback,
         * onRequestPermissionsResult.
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  3. Override the onRequestPermissionsResult() callback to handle the result of the permission request:

    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        mLocationPermissionGranted = false;
        switch (requestCode) {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mLocationPermissionGranted = true;

    A later section of this tutorial describes the updateLocationUI() method.

Add a map

Display a map, using the Maps SDK for Android.

  1. Add a <fragment> element to your activity's layout file, activity_maps.xml. This element defines a SupportMapFragment to act as a container for the map and to provide access to the GoogleMap object. The tutorial uses the Android support library version of the map fragment, to ensure backward compatibility with earlier versions of the Android framework.

    <fragment xmlns:android=""
        tools:context="com.example.currentplacedetailsonmap.MapsActivityCurrentPlace" />
  2. In your activity's onCreate() method, set the layout file as the content view:

    protected void onCreate(Bundle savedInstanceState) {
  3. Implement the OnMapReadyCallback interface and override the onMapReady() method, to set up the map when the GoogleMap object is available:

    public void onMapReady(GoogleMap map) {
        mMap = map;
        // Do other setup activities here too, as described elsewhere in this tutorial.
        // Turn on the My Location layer and the related control on the map.
        // Get the current location of the device and set the position of the map.
  4. In your activity's onCreate() method, get a handle to the map fragment by calling FragmentManager.findFragmentById(). Then use getMapAsync() to register for the map callback:

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  5. Write an updateLocationUI() method to set the location controls on the map. If the user has granted location permission, enable the My Location layer and the related control on the map, otherwise disable the layer and the control, and set the current location to null:

    private void updateLocationUI() {
        if (mMap == null) {
        try {
            if (mLocationPermissionGranted) {
            } else {
                mLastKnownLocation = null;
        } catch (SecurityException e)  {
            Log.e("Exception: %s", e.getMessage());

Get the location of the Android device and position the map

Use the fused location provider to find the device's last-known location, then use that location to position the map. The tutorial provides the code you need. For more details on getting the device's location, see the guide to the fused location provider in the Google Play services location APIs.

private void getDeviceLocation() {
     * Get the best and most recent location of the device, which may be null in rare
     * cases when a location is not available.
    try {
        if (mLocationPermissionGranted) {
            Task locationResult = mFusedLocationProviderClient.getLastLocation();
            locationResult.addOnCompleteListener(this, new OnCompleteListener() {
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful()) {
                        // Set the map's camera position to the current location of the device.
                        mLastKnownLocation = task.getResult();
                                new LatLng(mLastKnownLocation.getLatitude(),
                                        mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
                    } else {
                        Log.d(TAG, "Current location is null. Using defaults.");
                        Log.e(TAG, "Exception: %s", task.getException());
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
    } catch(SecurityException e)  {
        Log.e("Exception: %s", e.getMessage());

Get the current place

Use the Places SDK for Android to get a list of likely places at the device's current location. In this context, a place is a business or other point of interest.

This tutorial gets the current place when the user clicks a Get Place button. It offers the user a list of likely places to choose from, then adds a marker on the map at the location of the selected place. The tutorial provides the code you need to interact with the Places SDK for Android. For more details, see the guide to getting the current place.

  1. Create a layout file (current_place_menu.xml) for the options menu, and override the onCreateOptionsMenu() method, to set up the options menu. See the accompanying sample app for the code.
  2. Override the onOptionsItemSelected() method to get the current place when the user clicks the Get Place option:
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == {
        return true;
  3. Create a showCurrentPlace() method to get a list of likely places at the device's current location:

    private void showCurrentPlace() {
        if (mMap == null) {
        if (mLocationPermissionGranted) {
            // Use fields to define the data types to return.
            List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.ADDRESS,
            // Use the builder to create a FindCurrentPlaceRequest.
            FindCurrentPlaceRequest request =
            // Get the likely places - that is, the businesses and other points of interest that
            // are the best match for the device's current location.
            @SuppressWarnings("MissingPermission") final
            Task<FindCurrentPlaceResponse> placeResult =
            placeResult.addOnCompleteListener (new OnCompleteListener<FindCurrentPlaceResponse>() {
                public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        FindCurrentPlaceResponse likelyPlaces = task.getResult();
                        // Set the count, handling cases where less than 5 entries are returned.
                        int count;
                        if (likelyPlaces.getPlaceLikelihoods().size() < M_MAX_ENTRIES) {
                            count = likelyPlaces.getPlaceLikelihoods().size();
                        } else {
                            count = M_MAX_ENTRIES;
                        int i = 0;
                        mLikelyPlaceNames = new String[count];
                        mLikelyPlaceAddresses = new String[count];
                        mLikelyPlaceAttributions = new List[count];
                        mLikelyPlaceLatLngs = new LatLng[count];
                        for (PlaceLikelihood placeLikelihood : likelyPlaces.getPlaceLikelihoods()) {
                            // Build a list of likely places to show the user.
                            mLikelyPlaceNames[i] = placeLikelihood.getPlace().getName();
                            mLikelyPlaceAddresses[i] = placeLikelihood.getPlace().getAddress();
                            mLikelyPlaceAttributions[i] = placeLikelihood.getPlace()
                            mLikelyPlaceLatLngs[i] = placeLikelihood.getPlace().getLatLng();
                            if (i > (count - 1)) {
                        // Show a dialog offering the user the list of likely places, and add a
                        // marker at the selected place.
                    else {
                        Log.e(TAG, "Exception: %s", task.getException());
        } else {
            // The user has not granted permission.
            Log.i(TAG, "The user did not grant location permission.");
            // Add a default marker, because the user hasn't selected a place.
            mMap.addMarker(new MarkerOptions()
            // Prompt the user for permission.
  4. Create an openPlacesDialog() method to display a form allowing the user to select a place from a list of likely places. Add a marker on the map for the selected place. The marker content includes the name and address of the place, and any attributions that the API supplies:

    private void openPlacesDialog() {
        // Ask the user to choose the place where they are now.
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // The "which" argument contains the position of the selected item.
                LatLng markerLatLng = mLikelyPlaceLatLngs[which];
                String markerSnippet = mLikelyPlaceAddresses[which];
                if (mLikelyPlaceAttributions[which] != null) {
                    markerSnippet = markerSnippet + "\n" + mLikelyPlaceAttributions[which];
                // Add a marker for the selected place, with an info window
                // showing information about that place.
                mMap.addMarker(new MarkerOptions()
                // Position the map's camera at the location of the marker.
        // Display the dialog.
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setItems(mLikelyPlaceNames, listener)
  5. Create a custom layout for the info window content. This makes it possible to display multiple lines of content in the info window. First, add an XML layout file, custom_info_contents.xml, containing a text view for the info window title, and another text view for the snippet (that is, the info window's textual content):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
            android:textStyle="bold" />
            android:textColor="#ff7f7f7f" />
  6. Implement the InfoWindowAdapter interface to inflate the layout and load the info window content:

    public void onMapReady(GoogleMap map) {
        // Do other setup activities here too, as described elsewhere in this tutorial.
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        // Return null here, so that getInfoContents() is called next.
        public View getInfoWindow(Marker arg0) {
            return null;
        public View getInfoContents(Marker marker) {
            // Inflate the layouts for the info window, title and snippet.
            View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
            TextView title = ((TextView) infoWindow.findViewById(;
            TextView snippet = ((TextView) infoWindow.findViewById(;
            return infoWindow;

Save the map's state

Save the map's camera position and the device location. When a user rotates an Android device, or makes configuration changes, the Android framework destroys and rebuilds the map activity. To ensure a smooth user experience, it's good to store relevant application state and restore it when needed.

This tutorial provides all the code you need to save the map's state. For more details, see the guide to the savedInstanceState bundle.

  1. In your map activity, set up key values for storing activity state:

    private static final String KEY_CAMERA_POSITION = "camera_position";
    private static final String KEY_LOCATION = "location";
  2. Implement the onSaveInstanceState() callback to save the state when the activity pauses:

    protected void onSaveInstanceState(Bundle outState) {
        if (mMap != null) {
            outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
            outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
  3. In your activity's onCreate() method, retrieve the device's location and the map's camera position if previously saved:

    protected void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);