我可以将Google地图自动填充功能添加到我的EditText吗?

时间:2018-08-23 10:35:00

标签: android google-maps autocomplete android-edittext

我的应用程序中有一个搜索EditText,我想使用自动完成功能将所有地址从Google地图添加到此EditText。

我想确切地知道该怎么做。 谢谢!

EditText adress = (EditText) findViewById(R.id.adress);

1 个答案:

答案 0 :(得分:0)

            AutoCompleteTextView placeAutoSearch;
            ArrayList<String> placesList;
            ArrayAdapter<String> placesListAdatper;
            String placesURL;
        String PLACES_BASE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?";


        placeAutoSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count)
                    {
                            placesList = new ArrayList<String>();
                            updateList(s.toString());
                    }

                    @Override
                    public void afterTextChanged(Editable s)
                    {

                    }
                });

    private void updateList(String place)
        {

            String parameter = "input=" + place + "&types=geocode&sensor=true&key=" + PLACES_API_KEY;

            placesURL = PLACES_BASE_URL + parameter;

            ((MapForAddressActivity)controller).getPlacesList(placesURL);

        }

        public void updatePlacesList(ArrayList<PlacesNameEntity> list)
        {
            placesList.clear();

            for (int i=0; i<list.size(); i++)
            {
                placesList.add(i, list.get(i).getDescription());
            }

            placesListAdatper = new ArrayAdapter<String>(getBaseActivity(), android.R.layout.simple_list_item_1, placesList)
            {
                @Override
                public View getView(int position, View convertView, ViewGroup parent)
                {
                    View view = super.getView(position, convertView, parent);
                    android.widget.TextView text = (android.widget.TextView) view.findViewById(android.R.id.text1);
                    return view;
                }
            };

            placeAutoSearch.setAdapter(placesListAdatper);
            placesListAdatper.notifyDataSetChanged();
        }



public void getAddressLatLong(String addressString)
    {
        Geocoder coder = new Geocoder(getBaseActivity());
        List<Address> address;
        LatLng latLng = null;

        try {
            address = coder.getFromLocationName(addressString, 5);
            if (address == null) {

            }
            Address location = address.get(0);
            latLng = new LatLng(location.getLatitude(), location.getLongitude() );
            userLatLng.setLat(location.getLatitude());
            userLatLng.setLon(location.getLongitude());
            mMap.getGoogleMap().clear();
            mMap.drawMarker(latLng);
            ((MapForAddressActivity)controller).hideKeyboard();

        } catch (IOException ex) {

            ex.printStackTrace();
        }
    }

从服务器获取位置列表(Google地方信息API)

public void getPlacesList(String parameter)
    {
        ((ServiceFactory) serviceFactory).getProviderService()
                .GetPlacesNameList(parameter)
                .enableRetry(false)
                .enqueue(new ServiceCallback(this, this)
                {
                    @Override
                    protected void onSuccess(Object response, int code)
                    {
                        ((MapForAddressActivityView)view).updatePlacesList(((PlacesNameResponse) response).getList());
                    }

                    @Override
                    protected void onFailure(String errorMessage, int code) {
                        showToast(errorMessage);
                    }
                });
    }

您的改造界面:

@GET()
ServiceCall<PlacesNameResponse> GetPlacesNameList(@Url String url);