简体   繁体   中英

Google Map API v2 is not showing Map on device

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"/>

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

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

可能是 API 密钥的问题,或者您只是在项目 api 控制台( https://console.developers.google.com/project )中检查您的 SHA1 指纹,并且您的 Eclipse Windows -> Preferences -> Android -> Build 是相同的,否则,只需从控制台更新 SHA1 即可构建路径,反之亦然。

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.

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

万一其他人像我一样愚蠢...确保您正在测试的设备已连接到 WiFi...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM