簡體   English   中英

如何將Google Maps嵌入到活動中?

[英]How to embed Google Maps into an activity?

我目前正在嘗試將Google Map嵌入自定義視圖中。

本教程顯示的唯一內容是如何制作完整的地圖xml,而不是“窗口式”版本。

誰能幫我?

在您的xml中嘗試一下:

<!-- Map -->
<FrameLayout
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="200dp"">
</FrameLayout>

在您的代碼中:

// This method adds map fragment to the container.
private void addMapFragment() {
    SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    mMapFragment.getMapAsync(this);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.map_container, mMapFragment)
            .commit();
}

如果您在片段中,還請使用getChildFragmentManager()而不是getSupportFragmentManager()

在您的自定義視圖xml文件中,嘗試更改地圖片段的大小。 在下面的示例中,我將height和width設置為30dp

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         /> 

過去在您的活動xml文件中,並更改片段的ID,例如:android:id =“ @ + id / map1”

然后

轉到您的Java文件並在create上的setcontent之后

MapFragment  mMapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction =
                getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.map1, mMapFragment);
        fragmentTransaction.commit();
        mMapFragment.getMapAsync(this);

實現onMapReadyCallBack方法並覆蓋活動

注意:-僅當您在應用程序中添加了Google地圖時,此功能才有效

暫無
暫無

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

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