簡體   English   中英

帶有自定義布局的片段中的Google Map

[英]Google Map inside a fragment with custom layout

我最好的選擇是如何使用自定義布局將Google地圖分割成一個片段?

我正在嘗試將GoogleMapsV2用於其中一個fragment(tab)。 該片段將具有一些按鈕,並且google地圖在背景中帶有標記。 因此,該視圖將看起來像http://www.androidtapp.com/wp-content/uploads/2009/08/CardioTrainer-Map.jpg (不過,我的視圖要簡單得多)

谷歌搜索后,廣泛地溢出堆棧並進行實驗,這是我的理解1.我可以在片段中使用SupportMapFragment(我嘗試了它的工作原理。但是很遺憾,我無法獲得自定義按鈕)

2.我可以將MapFragment與更高的SDK(sdk 12)一起使用。 我不介意失去向后兼容性。 但是此選項看起來不對,因為我的FragmentPagerAdapter期望我返回一個Fragment而不是MapFragment

似乎沒有簡單/標准的方法可以完成此操作,我需要一些幫助以正確的方向進行操作。 我是一個新的自學的android開發人員。 因此,如果我錯過這里的基礎知識,請多多包涵。 如果有幫助,我可以發布代碼。 但是,我在尋求理解前進方向方面尋求幫助,而不是直接代碼方面尋求幫助。 謝謝看

聽起來您想要這樣的東西:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/map_v2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.google.android.gms.maps.MapFragment" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="0px" >

<!-- Lots of fancy layout -->   

</RelativeLayout>
</RelativeLayout>

然后在您的SupportMapFragment中,如下所示:

public class MyFragment extends Fragment {

private SupportMapFragment fragment;
private GoogleMap map;

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
    fragment = SupportMapFragment.newInstance();
    fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}

@Override
public void onResume() {
super.onResume();
if (map == null) {
    map = fragment.getMap();
    map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
}

另外,值得一提...似乎第二次重新加載片段時出現錯誤。 這是SupportMapFragment的解決方法。 看到這里: 解決方法

這里還可以對此進行更好的修復,但是我還沒有嘗試過: 選項2

暫無
暫無

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

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