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

时间:2017-06-20 08:55:49

标签: java android google-maps android-fragments

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

enter image description here

我想添加一个搜索栏,我可以在任何地方搜索Google地图。搜索栏应采用自动完成方式。我从https://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/

获取此代码

看看上面的链接。 这些代码用于ORDINARY APP获取自动完成搜索栏。它不适合使用片段的应用程序。我不知道如何用碎片做到这一点。 这是我的代码

对于主要活动(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

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

 }
 }

我必须添加一个像Google map一样的自动完成搜索栏。请参考我在开始时提供的链接,可以任何人为我的MapFragment编码吗?

1 个答案:

答案 0 :(得分:0)

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

1。Google Place Autocomplete