【问题标题】:Android MapView display emptyAndroid MapView 显示为空
【发布时间】:2013-10-03 07:50:51
【问题描述】:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yu.lbs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name="com.yu.lbs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:apiKey=".........."
        android:clickable="true"
        android:enabled="true" />

</LinearLayout>

MainActivity.java:

import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity {

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

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

Activity 可以启动了。但是在 MapView 中什么都没有。可以显示地图的小网格,我认为是 MapView 自带的,但没有加载地图。有什么问题?我正在使用 Google API v3。但这段代码来自使用 API v1 的教科书。

【问题讨论】:

  • iU 是否选择了“Google API”项目构建目标?并添加这个
  • 是的,我的项目下的包资源管理器中有 Google API [Android 4.3]。
  • 您检查过您的互联网连接速度吗?
  • 速度还可以。我可以在 Google 地图中看到地图,但在我的应用中看不到。
  • @PadmaKumar 我试过了,但地图仍然没有显示..

标签: android google-maps google-api android-mapview


【解决方案1】:

我解决问题的方式,我必须这样做:

final MapView mapView = (MapView)fragmentView.findViewById(R.id.map_fieldLocation);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
        googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
        mapView.onResume();
    }
});

我缺少的重要部分是,您必须在调用 getMapAsync() 之前调用 onCreate() 方法,一旦调用回调,您需要在 MapView 对象上调用 onResume()

这完全解决了我的问题。

这是在您自己的班级中的样子:

public class MainActivity extends MapActivity {

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

        if (getView() != null) {

            final MapView mapView = (MapView)getView().findViewById(R.id.mapView);

            mapView.onCreate(savedInstanceState);
            mapView.getMapAsync(new OnMapReadyCallback() {

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
                    googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
                    mapView.onResume();
                }
            }
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

希望这会有所帮助!

【讨论】:

  • 感谢您回答这个问题,我没有调用 mapView.onResume()。
  • 根据本教程:inducesmile.com/android/android-mapview-example-tutorial onResume 和类似的生命周期调用应该在 Activity/Fragment 生命周期方法中。
  • "在全交互模式下使用 API 时,MapView 类的用户必须将以下活动生命周期方法转发到 MapView 类中的相应方法:onCreate()、onStart()、onResume() , onPause(), onStop(), onDestroy(), onSaveInstanceState(), and onLowMemory(). ... 当在 lite 模式下使用 API 时,转发生命周期事件是可选的。详情请参阅 lite 模式文档。来自:developers.google.com/maps/documentation/android-sdk/map 所以...我的解决方案是:mMapView.onCreate(Bundle.EMPTY); mmapView.onResume();
【解决方案2】:

我猜您正在尝试实施的 Google 地图版本弄得一团糟。 从您使用的代码看来,您正在尝试使用 Google Maps API V1。

问题在于,现在您无法为 Google Map API V1 生成新的 API 密钥,该版本已被弃用,Google 不会为其提供新密钥。

从 cmets 看来,在 API 控制台中您没有激活正确的 API。 查看这篇博文,了解如何为 Google Map API V2 for Android 生成 API 密钥:

Google Maps API V2 Key

接下来,请阅读以下指南以在您的应用程序中实施此版本:

Google Maps API V2

【讨论】:

    【解决方案3】:

    在 android 中使用 Map API V2 用于谷歌地图。访问此link 可能会对您有所帮助。

    它比旧版本更好,也更容易。

    【讨论】:

      【解决方案4】:

      根据documentation(截至今天),您需要将生命周期回调转发到 MapView。
      更简洁的方法是使用LifecycleObserver。 当地图放置在活动或片段中时,此方法有效。
      您不会收到 onLowMemory()onSaveInstanceState(),但您仍然可以将它们转发到 MapView。

      这是一个 Kotlin 示例。

      import android.content.Context
      import android.util.AttributeSet
      import androidx.lifecycle.Lifecycle
      import androidx.lifecycle.LifecycleObserver
      import androidx.lifecycle.LifecycleOwner
      import androidx.lifecycle.OnLifecycleEvent
      import com.google.android.gms.maps.MapView
      
      class MyMapView @JvmOverloads constructor(
          context: Context,
          attrs: AttributeSet? = null,
          defStyle: Int = 0
      ) : MapView(context, attrs, defStyle), LifecycleObserver {
      
          init {
              if(context is LifecycleOwner) {
                  context.lifecycle.addObserver(this)
              }
          }
      
          //Your code here
      
          @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
          fun create() {
              onCreate(null)
          }
      
          @OnLifecycleEvent(Lifecycle.Event.ON_START)
          fun start() {
              onStart()
          }
      
          @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
          fun resume() {
              onResume()
          }
      
          @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
          fun pause() {
              onPause()
          }
      
          @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
          fun stop() {
              onStop()
          }
      
          @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
          fun destroy() {
              onDestroy()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-14
        • 2013-11-15
        相关资源
        最近更新 更多