簡體   English   中英

在片段中使用Google Places API自動填充搜索欄

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

我的應用目前看起來像這樣:

在此輸入圖像描述

我想添加一個搜索欄,我可以在任何地方搜索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編碼嗎?

沒有人會做你的功課。 如果您想了解如何在Google地圖中創建自動填充搜索欄,可以點擊這些鏈接。

1. Google Place自動填充功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM