2016-07-16 77 views
0

我需要Google Map上的搜索工具欄。Android - MapActivity中的搜索工具欄

我使用MapActivity顯示地圖。
我做的方式是通過創建項目嚮導,當您創建一個新項目時,您可以從基本活動,空活動,地圖活動中進行選擇。
我選擇了MapActivity。 在XML文件中我只有這個:

<resources> 
    <string name="google_maps_key" templateMergeStrategy="preserve" 
     translatable="false">AIzaSyCx3iR5fkeWr2LUf6JBHwGLEEkeghL3snk 
    </string> 
</resources> 

那麼什麼是插在地圖上搜索工具欄的方法是什麼?

然後如何添加那個搜索工具欄。如果我添加一個佈局,那麼會顯示一個錯誤。

+2

請更明確地說明你想要達到的目標以及你嘗試的方式,因爲我不知道什麼是「這裏」以及你使用的是什麼「字段」。 –

+0

我需要谷歌地圖上的搜索工具欄。我使用MapActivity顯示地圖(當您創建一個新項目時,它會詢問基本活動,空活動,地圖活動,我選擇了MapActivity,在xml文件中只有這個: AIzaSyCx3iR5fkeWr2LUf6JBHwGLEEkeghL3snk 那麼如何添加搜索工具欄。如果我添加布局它顯示錯誤 –

+0

添加到您的問題,請。 –

回答

0

我會這樣做的方式如下:
我會有一個活動,其中有一個片段,其中包含地圖裏面。
裏面的活動我會與地圖顯示的片段的方法:

public void showMap() { 

    MapLocationFragment locationFragment = MapLocationFragment.newInstance(); 
    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.fragment_container, locationFragment) 
      .addToBackStack(null) 
      .commit(); 
} 

activity xml: 
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <include 
     android:id="@+id/app_bar_include" 
     layout="@layout/app_bar_layout_device" /> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize" /> 
    </RelativeLayout> 

現在,MapLocationFragment裏面,你需要有類似的東西:

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.maps_layout, container, false); 
    return view; 
} 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    initializeMapIfNeeded(); 
} 

private void initializeMapIfNeeded() { 
    if (googleMap == null) { 
     loadMapAsync(); 
    } 
} 

private void loadMapAsync() { 
    ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map)).getMapAsync(getOnMapReadyCallback()); 
} 

public OnMapReadyCallback getOnMapReadyCallback() { 
    return new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap map) { 
      googleMap = map; 
      if (googleMap == null) { 
       return; 
      } 

      googleMap.setIndoorEnabled(true); 
      googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
      googleMap.setOnMyLocationButtonClickListener(getMapMpOnMyLocationClickListener()); 
      googleMap.setLocationSource(getNewLocationSource()); 
      googleMap.setOnCameraChangeListener(getCameraChangeListener()); 
      //do other stuff too 
     } 
    }; 
} 

xml: 

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

<fragment 
     android:id="@+id/location_map" 
     class="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     android:src="@android:drawable/ic_menu_set_as" 
     app:layout_anchorGravity="bottom|right|end" 
     android:layout_gravity="bottom|end"/> 

</android.support.design.widget.CoordinatorLayout> 

對於更多細節請看這個項目:
Sunshine app