2017-09-14 116 views
0

我正在做一個可以瀏覽目的地的項目。我想利用自動填充搜索來幫助用戶搜索目的地。使用谷歌地圖進行自動完成搜索Android Studio

我的問題是,片段顯示我需要使用的搜索欄,但是當我點擊它進行搜索時 - 它會打開覆蓋搜索欄,我可以在其中鍵入,但消失得太快,我甚至無法使用它。

activity_search.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/traintracks_image" 
    tools:context="travel_buddyapp.travelbuddyapp.Search_Activity"> 

    <Button 
     android:id="@+id/bBack_Search" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Back" 
     tools:layout_editor_absoluteX="0dp" 
     android:textColor="#f7f7f9" 
     android:background="@null" 
     tools:layout_editor_absoluteY="2dp" /> 

    <Button 
     android:id="@+id/bSearch_Search" 
     android:layout_width="232dp" 
     android:layout_height="48dp" 
     android:text="Search" 
     android:background="#4a90e2" 
     android:textColor="#f7f7f9" 
     android:layout_marginTop="35dp" 
     app:layout_constraintTop_toBottomOf="@+id/edSearch" 
     android:layout_marginLeft="75dp" 
     app:layout_constraintLeft_toLeftOf="@+id/bBack_Search" 
     android:layout_marginStart="75dp" /> 

    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="286dp" 
     android:layout_height="80dp" 
     android:text="Enter your dream destination" 
     android:textSize="35sp" 
     android:textAlignment="center" 
     android:textColor="#f7f7f9" 
     android:layout_marginLeft="49dp" 
     app:layout_constraintLeft_toLeftOf="@+id/bBack_Search" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="65dp" 
     android:layout_marginStart="49dp" /> 

    <EditText 
     android:id="@+id/edSearch" 
     android:layout_width="335dp" 
     android:layout_height="45dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:hint="I want to visit..." 
     android:textColor="#b6bdc6" 
     android:background="#f7f7f9" 
     android:layout_marginTop="134dp" 
     app:layout_constraintTop_toBottomOf="@+id/textView6" 
     android:layout_marginLeft="25dp" 
     app:layout_constraintLeft_toLeftOf="@+id/bBack_Search" 
     android:layout_marginStart="32dp" /> 

    <fragment 
     android:id="@+id/place_autocomplete_fragment" 
     android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
     android:layout_width="335dp" 
     android:layout_height="45dp" 
     android:layout_marginLeft="38dp" 
     android:layout_marginTop="30dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/textView6" /> 

    <TextView 
     android:id="@+id/searched_address" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:layout_marginTop="80dp" 
     app:layout_constraintTop_toBottomOf="@+id/bSearch_Search" 
     android:layout_marginLeft="154dp" 
     app:layout_constraintLeft_toLeftOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

Search_Activity.java

public class Search_Activity extends AppCompatActivity { 

    public Button but1; 
    private GoogleMap mMap; 
    TextView txtPlaceDetailss; 
    private static final String TAG = "Search_Activity"; 

    public void init(){ 
     but1= (Button)findViewById(R.id.bBack_Search); 
     but1.setOnClickListener(new View.OnClickListener(){ 
      @Override 
        public void onClick(View v){ 
       Intent toy = new Intent(Search_Activity.this,WelcomeActivity.class); 
       startActivity(toy); 
      } 
     }); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search_); 
     init(); 

     txtPlaceDetailss = (TextView)findViewById(R.id.searched_address); 

     PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
       getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

     autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       Log.i("Tag", "Place: " + place.getName()); 
       String placeDetailsStr = place.getName() + "\n" 
         + place.getId() + "\n" 
         + place.getLatLng().toString() + "\n" 
         + place.getAddress() + "\n" 
         + place.getAttributions(); 
       txtPlaceDetailss.setText(placeDetailsStr); 
      } 

      @Override 
      public void onError(Status status) { 
       Log.i("Tag", "An error occurred: " + status); 
      } 
     }); 
    } 
} 

誰能請幫助我?

回答

0

您的google_maps_key是否有效並且您是否啓用了密鑰?

+0

我沒有將它添加到我的清單文件中並創建它。如果它被啓用且有效,我怎麼知道呢? –

+0

您可以在開發人員控制檯https://console.developers.google.com/中查看它 –

相關問題