1

Google Maps Android API v2 では、設定を変更すると地図が表示されません。

例:ユーザーが自分の場所のボタンを選択すると、マップはユーザーの現在の場所にアニメーション化されますが、マップはレンダリングされません。

public class MapView extends android.support.v4.app.FragmentActivity {

    private GoogleMap mMap;
    private UiSettings mUiSettings;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map_view);

        setUpMapIfNeeded();
    }

    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 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {

        mMap.setMyLocationEnabled(true);

        mUiSettings = mMap.getUiSettings();

        mUiSettings.setZoomControlsEnabled(true);
        mUiSettings.setMyLocationButtonEnabled(true);
        mUiSettings.setCompassEnabled(true);
    }

}

** レイアウト/マップ ビュー **

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/googleMap"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment"/>
4

1 に答える 1

1

以下のような OnCreate() の下の重要なコードを見逃しています。以下のコードを追加します。

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

コードは次のようにする必要があります。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.map_view);

    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

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 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {...

幸運を...

于 2013-02-22T09:15:37.597 に答える