Google 地图 Android API v2 仅在我触摸屏幕时显示图块

Google Maps Android API v2 only shows tiles when I touch the screen

问题

当应用程序启动时,它会显示一个白色的 Google 地图(我可以看到 Google 徽标),即使在互联网连接良好的情况下等待几分钟,它也不会加载图块。

但是,如果我触摸屏幕,就会开始出现图块(每次触摸一个图块)。

我测试了3部实体手机,得到了相同的结果。

MapActivity

public class MapActivity extends Activity {

    private MapView mMapView;
    private GoogleMap gmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        gmap = mMapView.getMap();
        gmap.getUiSettings().setMyLocationButtonEnabled(false);
        gmap.getUiSettings().setZoomControlsEnabled(false);
        gmap.getUiSettings().setCompassEnabled(false);
        gmap.getUiSettings().setAllGesturesEnabled(false);
        gmap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        MapsInitializer.initialize(mMapView.getContext());
    }

}

布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/frameLayout">
    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</FrameLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nst.lazzus" >

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.nst.lazzus.MapActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MY_API_KEY"/>
</application>

日志

03-01 13:37:53.627  18306-18306/com.nst.lazzus I/x﹕ Making Creator dynamically
03-01 13:37:53.631  18306-18306/com.nst.lazzus W/ResourcesManager﹕ Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
03-01 13:37:53.632  18306-18306/com.nst.lazzus W/ResourcesManager﹕ Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
03-01 13:37:53.648  18306-18306/com.nst.lazzus I/Google Maps Android API﹕ Google Play services client version: 6587000
03-01 13:37:53.656  18306-18306/com.nst.lazzus I/Google Maps Android API﹕ Google Play services package version: 6776438
03-01 13:37:54.095  18306-18321/com.nst.lazzus W/art﹕ Suspending all threads took: 5.532ms
03-01 13:37:54.108  18306-18321/com.nst.lazzus I/art﹕ Background sticky concurrent mark sweep GC freed 50758(2MB) AllocSpace objects, 15(1627KB) LOS objects, 14% free, 21MB/25MB, paused 7.115ms total 52.092ms
03-01 13:37:54.222  18306-18367/com.nst.lazzus D/OpenGLRenderer﹕ Render dirty regions requested: true
03-01 13:37:54.228  18306-18306/com.nst.lazzus D/Atlas﹕ Validating map...
03-01 13:37:54.278  18306-18367/com.nst.lazzus I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
03-01 13:37:54.279  18306-18367/com.nst.lazzus I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-01 13:37:54.292  18306-18367/com.nst.lazzus D/OpenGLRenderer﹕ Enabling debug mode 0

getMap() 方法已弃用。请改用 getMapAsync(OnMapReadyCallback)。回调方法为您提供一个 GoogleMap 实例,保证为非空且随时可用。

有时Google地图绘制有问题。尝试修改您的布局,添加叠加视图——这解决了我的类似问题

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/frameLayout">
    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</FrameLayout>

确保从 Activity 的相应方法转发对 MapView 上这些方法的调用:

onCreate(Bundle savedInstanceState)
onDestroy()
onLowMemory()
onPause()
onResume()
onSaveInstanceState(Bundle outState)

我能看到你现在转发的唯一方法是onCreate

但是,更方便的解决方案是使用 MapFragmentSupportMapFragment

MapView 用于 Google Maps Android API v1,对于 Google Maps Android API v2,您应该改用 MapFragment。但是你仍然可以在 Map API V2 中使用 MapView,但需要:

Users of this class must forward all the activity life cycle methods - such as onCreate(), onDestroy(), onResume(), and onPause() - to the corresponding methods in the MapView class.

此外,在mMapView.onCreate(savedInstanceState);之后调用mMapView.onResume();使地图快速显示。

最后,您必须通过以下方式在 onCreate(...) 中初始化地图:

MapsInitializer.initialize(this);

使用我做过的框架布局...试试这个


xml代码

<FrameLayout
   android:id="@+id/mapView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_below="@+id/slider" >
</FrameLayout>
java onCreate 中的代码

 MapFragment fragment = new MapFragment();
        FragmentTransaction transaction =getActivity().getFragmentManager().beginTransaction();
        transaction.add(R.id.mapView, fragment).commit();

Manifest.xml 将这两个标签插入标签内。

<meta-data
     android:name="com.google.android.gms.version"
     android:value="@integer/google_play_services_version" />
<meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="YOUR GOOGLE MAP KEY" />