简体   繁体   English

在片段中使用Google Places API自动填充搜索栏

[英]Autocomplete search bar using Google Places API in a fragment

My app currently looks like this: 我的应用目前看起来像这样:

在此输入图像描述

I want to add a search bar where I can search any place as Google maps. 我想添加一个搜索栏,我可以在任何地方搜索Google地图。 The search bar should be in an Auto Complete way.I got this code from https://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/ 搜索栏应该是自动完成的方式。我从https://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/获取此代码

Have a look at the above link. 看看上面的链接。 And these codes where for an ORDINARY APP to get Auto Complete search bar. 这些代码用于ORDINARY APP获取自动完成搜索栏。 It doesnt suit for app using fragment. 它不适合使用片段的应用程序。 And I dont know how to do it with fragments. 我不知道如何用碎片做到这一点。 Here is my code 这是我的代码

For the Main Activity (ProfileActivity) 对于主要活动(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);

}
}

MapFragment MapFragment

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){

 }
 }

I have to add a search bar with Auto Complete like Google map.Please with reference of the Link which I have given at starting, Can Anyone code for my MapFragment? 我必须添加一个像Google map一样的自动完成搜索栏。请参考我在开始时提供的链接,任何人可以为我的MapFragment编码吗?

No one will do your homework. 没有人会做你的功课。 If you want to learn how to create Autocomplete Search bar in Google Map you can follow these links. 如果您想了解如何在Google地图中创建自动填充搜索栏,可以点击这些链接。

1. Google Place Autocomplete 1. Google Place自动填充功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM