Google Map API v2 is not showing Map on device

2019-02-04 09:20发布

问题:

I am running sample code that is provided on Google Map Documentation for Google Map Api v2 (https://developers.google.com/maps/documentation/android/start#specify_settings_in_the_application_manifest).

The code run successfully but Map is not loaded on the device. Only white screen is shown on the device. I am using 4.0.3 version Android device.

1) I enabled service for the project.

2) Generated key:

3) "google-play-services_lib" as Library in project:

4) Android Manifest:

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

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

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

    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

        <activity
            android:name="com.example.mapdemo.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>

5) Activity:

package com.example.mapdemo;

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

public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

6) Layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

回答1:

Use Fragment activity

eg:

public class Maps extends FragmentActivity {
    GoogleMap map;
    double lat;
    double lan;
    boolean flag = false;

    // private LocationManager lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapptry);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
                 }
  }

Change ur map.xml to

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

EDIT

Got to do this also

import android.support.v4.app.FragmentActivity;

Before you do this Right click project->properties->buildpath->java build path -> libraries .. then click on add external jars

the go to

user\android-sdks\extras\android\support\v4

and select android-support-v4.jar

PS: Do all this provided your API key is correct . If you API key is wrong then also it shows only a white screen



回答2:

try to add

GoogleMap googleMap =    ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

and replace your xml with

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
/>

you have added the following code outside application tag instead put it inside the tag

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

<permission
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

hope this might help someone who is facing the same problem



回答3:

May be this problem with API key or you just check your SHA1 fingerprint in your project api console(https://console.developers.google.com/project) and your eclipse Windows -> Preferences -> Android -> Build are same, other wise just update the SHA1 from console to build path or vice versa.



回答4:

Most probably your problem will be solved when you make sure below mentioned things:

  1. Activate Google Map API

  2. Use correct package name while generating API Key (in my case this was the mistake) .. and this seems problem to your case also as package name appears to be wrong (com.example.mapdemo)

  3. Make Sure you are using the correct keystore file for generating the SHA1

Hope this will be helpful.



回答5:

To maintain security for the system and users, Android requires apps to request permission before the apps can use certain system data and features. Depending on how sensitive the area is, the system may grant the permission automatically, or it may ask the user to approve the request.

https://developer.android.com/guide/topics/permissions/index.html