Google Place自动完成 [英] Google Place Auto Complete
问题描述
我正在尝试添加完善的google place自动完成功能,但是每次我单击它来搜索我的位置时,键盘就会显示出来,然后立即消失
I am trying to add the google place auto complete feature it worked perfect but everytime i click on it to search for my location the my the keyboard shows and then disppears immidiately
代码xml
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
java代码
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
location = place.getName().toString();
FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
String user_id = current_user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);
HashMap<String, String> userMap = new HashMap<>();
userMap.put("User location", location);
mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Intent addlocation = new Intent(selectlocation.this, home.class);
addlocation.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(addlocation);
finish();
}
}
});
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
}
});
而且我还没有测试过它,但是请帮助我检查Java代码(如果正确使用了它.)我正尝试将用户位置发送到我的firbase数据库,如果该代码正确,我就不会这样做,因为我还没有测试它
and also i havent tested it, but help me check the java code if the used it correctly.. i am trying to send the user location to my firbase database,i dont if that code is correct cause i havent tested it yet
推荐答案
我遇到了同样的问题.确保在开发者控制台中启用Android的Google Places API,而不仅仅是启用Places API. "Android的Places API"将不会显示在APIs& Auth/API,因为它尚未流行.您将必须使用API搜索框进行搜索.
I was having the same issue. Make sure you enable Google Places API for Android and not just Places API in the Developer Console. "Places API for Android" will not show up under APIs & Auth/APIs because it isn’t a popular API (yet). You will have to search for it using the API search box.
这篇关于Google Place自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!