java - 在 fragment 中使用 Google Places API 自动完成搜索栏

标签 java android google-maps android-fragments

我的应用目前看起来像这样:

enter image description here

我想添加一个搜索栏,我可以在其中搜索任何地方作为谷歌地图。搜索栏应采用自动完成方式。我从 https://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/ 获得此代码

看看上面的链接。
这些代码让普通应用程序在哪里获得自动完成搜索栏。它不适合使用 fragment 的应用程序。而且我不知道如何处理 fragment 。
这是我的代码

对于主要 Activity (ProfileActivity)

public class ProfileActivity extends AppCompatActivity {

final String TAG = this.getClass().getName();


BottomBar mBottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_profile);
    mBottomBar = BottomBar.attach(this, savedInstanceState);
    mBottomBar.setItemsFromMenu(R.menu.menu_user, new OnMenuTabClickListener() {
        @Override
        public void onMenuTabSelected(@IdRes int i) {
            if(i == R.id.ButtonBarFeed)
            {
                NewsFragment f = new NewsFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
            }
            else if(i == R.id.ButtonBarMap)
            {
                MapFragment f = new MapFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
            }
            else if(i == R.id.ButtonBarUser)
            {
                UserFragment f = new UserFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
            }

        }

        @Override
        public void onMenuTabReSelected(@IdRes int menuItemId) {

        }
    });
    mBottomBar.mapColorForTab(0,"#28809f");
}
public boolean googleServicesAvailable(){
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int isAvailable = api.isGooglePlayServicesAvailable(this);
    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }else if(api.isUserResolvableError(isAvailable)){
        Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
        dialog.show();
    } else {
        Toast.makeText(this,"Can't connet to Play Services", Toast.LENGTH_LONG).show();
    }
    return false;


}


boolean twice;
@Override
public void onBackPressed() {

    Log.d(TAG, "click");

    if(twice == true){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
    }
    twice = true;
    Log.d(TAG, "twice:" + twice);

    Toast.makeText(ProfileActivity.this, "Please press BACK again to exit", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            twice = false;
            Log.d(TAG, "twice:" + twice);

        }
    }, 3000);

}
}

map fragment
public class MapFragment extends Fragment implements OnMapReadyCallback {



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.map, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SupportMapFragment fragment =  (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.mapView1);
    fragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap){

 }
 }

我必须添加一个带有自动完成功能的搜索栏,如谷歌地图。请引用我在开始时给出的链接,任何人都可以为我的 MapFragment 编码吗?

最佳答案

没有人会做你的功课。如果您想了解如何在 Google map 中创建自动完成搜索栏,您可以点击这些链接。

1. Google Place Autocomplete

关于java - 在 fragment 中使用 Google Places API 自动完成搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648350/

相关文章:

javascript - 尽管 API 已激活,但尝试使用嵌入 map API 时出现 ApiNotActivatedMapError

java - Android - 我怎样才能实现这个谷歌地图动画?

java - 第一次调用一个方法,当数据有变化时再次调用该方法

java - Java 中的 TLS 性能?

android - 关闭 Activity 时从最近使用的应用程序中删除应用程序

android - 仅通过电子邮件共享图像

android - Android ndk 上的 dlopen

java - 线程中的异常 "main"java.util.IllegalFormatConversionException : d ! = java.lang.String

java - 使用正则表达式验证输入字符串 "RX-EZ12345678912345B"

javascript - 使用 AJAX 将数据存储在 MySQL 服务器数据库中