【问题标题】:Can i use the Google places API in an edittext?我可以在编辑文本中使用 Google Places API 吗?
【发布时间】:2019-10-29 09:15:29
【问题描述】:

是否可以在此布局中使用 Google Places API:

我突出显示了我想在其中使用 API 的 EditText 字段。

我已经搜索了许多关于如何执行此操作的指南,但似乎所有指南都使用了已弃用的代码。我对 Fragments 也很陌生,但我想更多地了解这个主题。

【问题讨论】:

    标签: android android-fragments android-edittext google-places-api


    【解决方案1】:

    如果您使用 Web API,这是可能的,但需要大量工作。

    不过,好像以前有人做过:https://github.com/mukeshsolanki/Google-Places-AutoComplete-EditText

    或者,您可以使用Places SDK for Android,它提供AutoCompleteSupportFragment 和AutoCompleteActivity。

    【讨论】:

    • 感谢您的回答,我尝试使用 Web API,但这超出了我的知识范围。我现在正在使用 Places SDK,但不推荐使用的代码让我很恼火。
    • 你查看 Github 链接了吗?
    【解决方案2】:

    在该edittext上设置onclicklistener并在函数下方的onclick方法调用中

    private void searchPlace() { 
    if (!Places.isInitialized(){ Places.initialize(this, getApplication().getString(R.string.google_maps_key)); } 
    // Set the fields to specify which types of place data to 
    // return after the user has made a selection. 
    List<Place.Field> fields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ADDRESS);
     // Start the autocomplete intent. 
    Intent intent = new Autocomplete.IntentBuilder( AutocompleteActivityMode.FULLSCREEN, fields) .build(this);
     startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE); }
    
    

    并在onresult方法中得到选择的结果

    Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    Place place = Autocomplete.getPlaceFromIntent(data);
                    destinationAddress = place.getAddress();
                    lblDestinationAddress.setText(destinationAddress);
    
                } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                    // TODO: Handle the error.
                    Status status = Autocomplete.getStatusFromIntent(data);
                    Toast.makeText(this, "Some went wrong. Search again", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, status.getStatusMessage());
                } 
    
    

    依赖

    //search places implementation
     'com.google.android.libraries.places:places:2.0.0'
    

    【讨论】:

    • 我不认为这是他要找的东西
    • 最近,我在我的应用程序中添加了相同的功能,用户点击edittext并弹出谷歌自定义搜索视图
    • 我相信他希望将 Places 建议附加到他的 EditText 中。
    • See this link 我想他想要这样的东西
    • 感谢您的回复,@VivekVinodh 是对的,我希望在我的编辑文本中显示地点建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多