无法使用Google Map API V2获取地图

时间:2015-04-13 22:45:53

标签: android google-maps

我试图通过Google Map API V2在我的应用上获得一个简单的地图;我按照所有步骤进行操作,但是当我启动应用程序时,我只会在左下角显示带有Google徽标的白页。

如果有人可以帮助我,那就太棒了..

这是XML文件:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

这是文件Java:

 package com.example.ismail.location_test;

 import android.support.v4.app.FragmentActivity;
 import android.os.Bundle;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;
 import com.google.android.gms.maps.OnMapReadyCallback;
 import com.google.android.gms.maps.SupportMapFragment;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}


private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}

最后是Manifest:

<?xml version="1.0" encoding="utf-8"?>

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

<uses-permission android:name="com.example.ismail.location_test.permission.MAPS_RECEIVE"/>
<permission      android:name="com.example.ismail.location_test.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<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" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBbHBnsC_YhHKQhhsnaahYiT3GltK52It0" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2 个答案:

答案 0 :(得分:0)

我没有发现您的代码有任何问题。您描述的症状指向无效的API密钥或gps已关闭,或者即使您正在运行这是一个未安装Google Play的模拟器。检查build.gradle下安装和使用的google play版本。例如:

dependencies { compile 'com.google.android.gms:play-services:6.5.87'

我建议您发布您的debuger日志,如果是api错误,它会在那里说明。

答案 1 :(得分:0)

我怀疑代码中有3个错误:

(1)在使用前声明许可:

    <permission android:name="com.example.ismail.location_test.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <!-- after declare use this -->
    <uses-permission android:name="com.example.ismail.location_test.permission.MAPS_RECEIVE"/>

(2)缺少Declare gms版本变量:

  <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

(3)缺少声明API_KEY:

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

首先请检查以上3位嫌疑人:如果仍有错误,您可以重新确认从here启用Google地图的步骤