我已经尝试了几天而且我不能让它起作用,是否可以从googleMap
而不是Fragment
延长FragmentActivity
?
我使用相同的示例代码android studio
创建了map fragment
示例,并且只是尝试将其设置为Fragment
而不是FragmentActivity
。
有可能吗?
我使用目标sdk21和minsdk 21
答案 0 :(得分:3)
在您要使用地图的活动/片段中使用以下代码
<fragment
class="com.google.android.gms.maps.MapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在Fragment的onCreateView中,您可以使用以下代码
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMapFragment == null) {
// Try to obtain the map from the SupportMapFragment.
mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
// Check if we were successful in obtaining the map.
if (mMapFragment != null) {
setUpMap();
}
}
}
设置你用下面提到的方法映射东西
private void setUpMap() {
mMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
...
}
}
}
希望它有所帮助...