Android MapView显示为空 [英] Android MapView display empty

查看:74
本文介绍了Android MapView显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

清单:

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

}

可以启动活动.但是在MapView中什么也没有.可以显示地图的小网格,我认为MapView附带了该网格,但未加载任何地图.有什么事吗我正在使用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().

那对我来说完全解决了.

这是您自己的课堂上的样子:

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

}

希望这会有所帮助!

Manifest:

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

}

The Activity can be started. But in the MapView there is nothing. The small grid of map can be show, I think that comes with MapView, but no map is loaded. What could be wrong? I am using Google API v3. But this code is from a text book using API v1.

解决方案

The way I got mine resolved, I had to do this:

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

The important part that I was missing is that you have to call the onCreate() method before you call getMapAsync() and once the callback is called, you need to call onResume() on the MapView object.

That totally solved it for me.

Here's what it would look like in your own 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;
    }

}

Hope this helps!

这篇关于Android MapView显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆