Android MapView 显示为空

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

list :

<?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>

主.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>

主要 Activity .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 中什么也没有。可以显示 map 的小网格,我认为是MapView自带的,但是没有加载 map 。有什么问题吗?我正在使用 Google API v3。但此代码来自使用 API v1 的教科书。

最佳答案

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

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()

这完全解决了我的问题。

这是在您自己的类(class)中的样子:

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

}

希望这对您有所帮助!

关于Android MapView 显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19043577/

相关文章:

c# - 如何在 .Net 的 GoogleWebAuthorizationBroker.AuthorizeAsync 方法中发送存储为 json 字符串(在数据库中)的 Google 驱动 token ?

Android 应用与旁加载 Google Play 的设备不兼容

Android AdMob 未在底部显示 LinearLayout

python - 自动将照片上传到特定的 Google 相册相册

google-maps - Google 地点自动完成的替代方案

google-maps - 如何不使用 app.json 存储 Google Maps API key ?

java - 如何一键正确地将数据保存在多个 fragment 中?

java - 检测 360 度转弯算法

android - 找不到处理 Intent { act=android.intent.action.VIEW dat=google.navigation :q=17. 399986,78.483137 pkg=com.google.android.apps.maps } 的 Activity

android - 更改地点自动完成的语言环境