Java – How to integrate autocomplete search with Google Maps in Android?

How to integrate autocomplete search with Google Maps in Android?… here is a solution to the problem.

How to integrate autocomplete search with Google Maps in Android?

Expected Results: Queries in the AutoComplete widget return the location indicated by the marker in the embedded Google Map fragment.

Code so far:

Activity class:

// Get the SupportMapFragment and request notification
 when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().
        findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
        getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

EditText attributeText = (EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input);
attributeText.setHint("find your space!");
autocompleteFragment.getView().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_search));

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

@Override
    public void onPlaceSelected(Place place) {
         TODO: Get info about the selected place.
        Log.i(TAG, "Place: " + place.getName());
    }

@Override
    public void onError(Status status) {
         TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

@Override
public void onMapReady(GoogleMap googleMap) {

 Customise the styling of the base map using a JSON object defined
 in a raw resource file.
googleMap.setMapStyle(
        MapStyleOptions.loadRawResourceStyle(
                getContext(), R.raw.style_json));

 adjust camera view
LatLng kekistan = new LatLng(54.60651219, -9.931456);
googleMap.addMarker(new MarkerOptions().position(kekistan)
        .title("Kekistan City Centre"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(kekistan));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(kekistan, 16));
}

XML layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTilt="50"
        map:uiCompass="false"
        map:uiZoomGestures="true"
        map:uiZoomControls="false"
        map:uiRotateGestures="false"
        map:uiScrollGestures="true" />

<android.support.percent.PercentRelativeLayout
        android:id="@+id/topper2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@android:color/transparent"
        android:orientation="vertical">

<fragment
            android:id="@+id/place_autocomplete_fragment"
            app:layout_widthPercent="90%"
            android:layout_marginTop="15dp"
            app:layout_marginStartPercent="5%"
            android:layout_height="40dp"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
    </android.support.percent.PercentRelativeLayout>
</RelativeLayout>

Solution

First, you need to save the GoogleMap reference in an instance variable and assign it in the onMapReady() callback:

GoogleMap mMap;

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

//...........

}

Then, you just need to get the name and location, and then put Marker:

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.i(TAG, "Place: " + place.getName());
        String name = (String) place.getName();
        LatLng latLng = place.getLatLng();

MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(name);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mMap.addMarker(markerOptions);

move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
    }

@Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});

Related Problems and Solutions