【问题标题】:Can i add google map autocomplete to my EditText?我可以将谷歌地图自动完成添加到我的 EditText 吗?
【发布时间】:2019-01-29 17:09:43
【问题描述】:

我的应用中有一个搜索 EditText,我想通过自动完成功能将所有地址从谷歌地图添加到这个 EditText。

我想知道我该怎么做。 谢谢!

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

【问题讨论】:

  • 没有。自动完成可以转换为 EditText 而不是 TextView,如下所示: ((EditText)autocompleteFragmentFrom.getView().findViewById(R.id.place_autocomplete_search_input));希望对您有所帮助。
  • 是的,你是对的,但我想从谷歌地图导入所有地址
  • 无论您在自动完成编辑文本中输入什么地址,当您选择一个地点时,您需要那个地址吗?这是你的要求吗?

标签: android google-maps autocomplete android-edittext


【解决方案1】:
            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);

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多