【发布时间】:2016-03-18 22:17:39
【问题描述】:
我需要添加一个使用 AndroidStudio 创建的 SupportMapFragment。使用它的片段活动工作正常。它在悉尼显示带有标记的地图。 但我需要将该地图放在一个 Layout(即 LinearLayout)上以添加按钮并与地图进行交互。
我创建了一个类 MapsActivity.java 及其对应的布局 activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some Text"
android:id="@+id/textView" />
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
既然如此:public class MapsActivity extends AppCompatActivity,在 MainActivity.onCreate() 里面我试过了:
1)
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
manager.beginTransaction().replace(R.id.map, mapFragment).commit();
setContentView(R.layout.activity_maps);
GoogleMap mMap = mapFragment.getMap();
// NOT GOOD: nMap is null.
2)
FragmentManager manager = getChildFragmentManager();
[...]
// NOT GOOD: Cannot resolve method getChildFragmentManager.
3)
GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// NOT GOOD: nMap is null.
接下来我可以尝试什么?
【问题讨论】:
-
在哪里可以找到 com.google.android.gms.maps.SupportMapFragment 的依赖项?
标签: android google-maps android-layout