-2

I develop an Android app. I want to create a search system on GoogleMap using SearchView. I want to get multiple place names from the entered string (as in the original search on Google Maps), but Geocoder always returns a List with a single Address.

How I can solve this problem?

SearchView searchView=(SearchView)findViewById(R.id.search);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String s) {
        return true;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        try {
            Geocoder geocoder=new Geocoder(getApplicationContext());
            List<Address> addresses = geocoder.getFromLocationName(s, 10);
            String[] addresses_string = new String[addresses.size()];
            for(int i=0;i<addresses.size();i++)
            {
                addresses_string[i]=addresses.get(i).getAddressLine(0);
            }
            ArrayAdapter<String> a=new ArrayAdapter<String>(getApplicationContext(),R.layout.record,addresses_string);
            ListView list=(ListView)findViewById(R.id.list);
            list.setAdapter(a);
        }
        catch (Exception e)
        {...}

        return true;
    }
});
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

in my case i used PlaceAutocompleteFragment you can add this to your XML file

   <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginRight="16dp"
            app:cardCornerRadius="4dp"
            app:cardElevation="10dp"
            app:elevation="15dp"
            >

            <fragment
                android:id="@+id/place_autocomplete_fragment"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:layout_width="match_parent"
                android:layout_gravity="center"
                android:layout_height="35dp" />
        </androidx.cardview.widget.CardView>

then add this to your activity

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

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString()));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 12.0f));
                markerOptions.icon(bitmapDescriptorFromVector(MapsActivity.this, R.drawable.ic_map));
                mMap.addMarker(markerOptions);
                LatLng latLng = place.getLatLng();
                ticket_lat = latLng.latitude;
                ticket_long = latLng.longitude;
            }

            @Override
            public void onError(Status status) {

            }
        });
    }

I hope it will help you .

  • I tried using AutocompleteSupportFragment because PlaceAutocomleteFragment is not currently supported, but I ran into this problem. https://stackoverflow.com/questions/59720428/autocompletesupportfragment-not-returning-results – EugenBezu Feb 05 '20 at 15:00