【问题标题】:Google Maps Android API v2 doesn't display the map when settings are changed更改设置时,Google Maps Android API v2 不显示地图
【发布时间】:2013-02-22 17:15:37
【问题描述】:

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

【问题讨论】:

  • 地图是在打开应用时出现,然后在移动时消失,还是根本不出现?
  • 移动时出现又消失,不刷新
  • 您使用什么来更新您的位置?位置监听器?或者是其他东西?早上我会仔细看看,到目前为止这似乎很奇怪。同时,请确保您已阅读有关如何执行此操作的开发指南。我知道我第一次尝试时犯了几个错误:link
  • 感谢朋友的帮助
  • 这就是你的全部代码吗?我这么说只是因为这看起来是对的。如果您最初能够获得地图,则意味着您正确设置了地图。您看过 Google 地图示例应用程序吗?它提供了一些关于如何使用地图、四处移动和更改地图选项的非常好的示例。在您的代码中没有看到任何其他内容,我会说看看 SDK 文件中的示例应用程序。如何设置它的说明是here。这对我有帮助。

标签: java android google-maps google-maps-android-api-2


【解决方案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() {...

祝你好运……

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2019-01-06
    相关资源
    最近更新 更多