简体   繁体   English

更改设置后,Google Maps Android API v2不显示地图

[英]Google Maps Android API v2 doesn't display the map when settings are changed

Google Maps Android API v2 doesn't display the map when settings are changed . 更改设置后, Google Maps Android API v2不会显示地图。

ex: when the user selects my location button, the map animate to user's current location but it doesn't render the map. 例如:当用户选择“我的位置”按钮时,地图会以动画形式显示用户的当前位置,但不会渲染地图。

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);
    }

} }

** layout/map_view ** ** 布局/地图视图 **

<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"/>

You missed an important code under OnCreate() like the below. 您错过了OnCreate()下的重要代码,如下所示。 Add this below code; 在代码下面添加;

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

Your code must be like this; 您的代码必须像这样;

@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() {...

Good Luck... 祝好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM