現在のフラグメントにMapFragmentを追加しようとしています。ネストされたフラグメントの使用はFragmentTransactionsに制限されており、レイアウトでxmlタグを使用することはできません。また、ユーザーがボタンを押したときにメインフラグメントに追加したいと思います。getInstance()
そのため、ユーザーがそのボタンを押したときにプログラムでMapFragmentを作成し、適切な場所に追加しています。これまでのところ、正しく表示されています。
問題は、MapFragmentをアタッチした後GoogleMap
、マーカーを配置するための参照を取得する必要があることですが、getMap()
メソッドはnullを返します(フラグメントonCreateView()
はまだ呼び出されていないため)。
デモのサンプルコードを見て、彼らが使用している解決策は、でMapFragmentを初期化し、が呼び出された後onCreate()
にGoogleMapへの参照を取得することであることがわかりました。onResume()
onCreateView()
ユーザーがボタンで地図を表示または非表示にできるようにしたいので、MapFragmentの初期化の直後にGoogleMapへの参照を取得する必要があります。考えられる解決策は、上記のように最初にマップを作成し、可視性をなくすように設定することですが、明示的に要求されない場合はユーザーの帯域幅を使用しないように、デフォルトでマップをオフにします。それのための。
で試しましたがMapsInitializer
、どちらも機能しません。私はちょっと立ち往生しています。何か案は?これまでの私のテストコードは次のとおりです。
public class ParadaInfoFragment extends BaseDBFragment {
// BaseDBFragment is just a SherlockFragment with custom utility methods.
private static final String MAP_FRAGMENT_TAG = "map";
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
private TextView mToggleMapa;
private boolean isMapVisible = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_parada_info, container, false);
mToggleMapa = (TextView) v.findViewById(R.id.parada_info_map_button);
return v;
}
@Override
public void onStart() {
super.onStart();
mToggleMapa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isMapVisible) {
openMap();
} else {
closeMap();
}
isMapVisible = !isMapVisible;
}
});
}
private void openMap() {
// Creates initial configuration for the map
GoogleMapOptions options = new GoogleMapOptions().camera(CameraPosition.fromLatLngZoom(new LatLng(37.4005502611301, -5.98233461380005), 16))
.compassEnabled(false).mapType(GoogleMap.MAP_TYPE_NORMAL).rotateGesturesEnabled(false).scrollGesturesEnabled(false).tiltGesturesEnabled(false)
.zoomControlsEnabled(false).zoomGesturesEnabled(false);
// Modified from the sample code:
// It isn't possible to set a fragment's id programmatically so we set a
// tag instead and search for it using that.
mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
// We only create a fragment if it doesn't already exist.
if (mMapFragment == null) {
// To programmatically add the map, we first create a
// SupportMapFragment.
mMapFragment = SupportMapFragment.newInstance(options);
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.parada_info_map_container, mMapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
// We can't be guaranteed that the map is available because Google Play
// services might not be available.
setUpMapIfNeeded(); //XXX Here, getMap() returns null so the Marker can't be added
// The map is shown with the previous options.
}
private void closeMap() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.remove(mMapFragment);
fragmentTransaction.commit();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = mMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(37.4005502611301, -5.98233461380005)).title("Marker"));
}
}
}
}
ありがとう