Google Map APi v.2无效

时间:2013-06-10 05:49:11

标签: android android-maps

我是初学者,尝试使用Google Map API v.2,我按照以下示例,

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

我知道它不会在模拟器上运行,因此我将应用程序打包为签名应用程序并将其安装在我的CellPhone上。但该应用程序不会显示地图。 请帮忙

这是我的清单文件

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
    <permission
            android:name="com.example.maptest.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.maptest.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-feature
        android:glEsVersion="0x00020000"
        android:required="false"/>
    <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.example.maptest.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>
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="MAP_KEK"/>
    </application>
</manifest>

我的布局XML

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

我的JAVA代码

package com.example.maptest;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends Activity {

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog = new ProgressDialog(MainActivity.this);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        if (map!=null){
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
       Marker kiel = map.addMarker(new MarkerOptions().position(KIEL).title("Kiel").snippet("Kiel is cool").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
            this.dialog.setMessage("Loading Map Please Wait...");
            this.dialog.show();
          }
        else if(map == null)
        {
            this.dialog.setMessage("Unable to fetch map");
            this.dialog.show();
        }
    }
}

如您所见,我添加了对话框以指示Map Object是否可用。

4 个答案:

答案 0 :(得分:1)

  

我将应用程序打包为签名应用程序并将其安装在我的CellPhone上。   但该应用程序不会显示地图

确保您已设置API密钥,该密钥来自您用于签署应用程序的同一密钥库。

答案 1 :(得分:0)

您缺少Google地图APi密钥。有关如何获取密钥并使用密钥的详细信息,请参阅此页面。 https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

模拟器将永远不会工作(只是不能使用地图)所以请确保如果您要对应用程序进行签名,则会根据签名密钥库而不是调试密钥库生成密钥。

答案 2 :(得分:0)

尝试修复清单:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
    <permission
            android:name="com.example.maptest.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.maptest.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-feature
        android:glEsVersion="0x00020000"
        android:required="false"/>
    <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="MAP_KEK"/>
       <!-- <uses-library
        android:name="com.google.android.maps" />-->
        <activity
            android:name="com.example.maptest.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>

并在谷歌控制台上转换谷歌地图api v2服务,如this

答案 3 :(得分:0)

您似乎缺少API密钥。为此,您需要使用SHA1指纹(数字证书),该指纹必须用于为您的应用程序检索API密钥,以便您可以使用Google地图。 API密钥如下所示:                                        的 AIzaSyDrav17-m1w7W17Gq19gOGvodfbnhyWQRF 它长40个字符,适合您的应用。检索此密钥后,将其置于 android:value 属性中。请参阅文档here.希望这会有所帮助。