如何将Google地图嵌入到活动中?

时间:2016-05-22 12:47:03

标签: android google-maps android-view

我目前正在尝试将Google地图嵌入到自定义视图中。

本教程展示的唯一内容是如何制作完整的地图xml而不是“窗口”版本。

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

在你的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()

答案 1 :(得分:2)

在自定义视图xml文件中,尝试更改地图片段的大小。在下面的示例中,我将高度和宽度设置为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" />

答案 2 :(得分:-1)

<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 例如:机器人:ID = “@ + ID / MAP1”

然后

转到你的java文件,并在创建

后的setcontent之后过去
MapFragment  mMapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction =
                getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.map1, mMapFragment);
        fragmentTransaction.commit();
        mMapFragment.getMapAsync(this);

实现onMapReadyCallBack方法并覆盖活动

注意: - 仅当您在应用程序中添加Google地图

时,此工作才有效