我是 Android 新手,正在尝试在 Android 手机上显示 Google 地图。我创建了适用于任何 Android 应用程序的 API 密钥(我没有为我的应用程序提供任何特定的 SHA)。我想这应该不重要。使用 Google Android 教程,我正在执行所有步骤,我在左下角看到谷歌水印,但没有地图。我将发布所有正在使用的代码。我尝试使用 Android Studio 1.2.1.1 中附带的 Google Maps Activity。我没有收到任何错误,但也没有得到地图。
我正在使用 MapFragment,因为它似乎比 MapView 更容易。
我还查看了此 API 的开发人员控制台 - 我没有任何流量 - 是否可能是 API 密钥没有被使用,所以我的应用程序没有收到报告?我怀疑 - 我该如何测试?
我正在尝试按照此处的步骤进行操作 -https://developers.google.com/maps/documentation/android/map
另一个相关的查询是,我假设我可以创建多个布局。我从一个充当启动画面的空白活动开始,然后我尝试转到地图活动 - 我无法在 Android Studio 内创建地图活动。我可以通过向导将其创建为第一个活动,但不能创建为第二个活动。我错过了什么吗?
任何帮助表示赞赏
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nbapl.avapps.mapscreen" >
<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" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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="API_KEY" />
<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>
</manifest>
我还使用了“文件”->“项目结构”->“依赖项”->“应用程序”-> 并发现包含 Google Play 服务。
地图活动.java
package com.nbapl.avapps.mapscreen;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("This is the center"));
}
}
这是布局 xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame"
android:background="#ffe2ff0e"
android:clickable="false"
android:foreground="#fffff9c5">
<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" />
</FrameLayout>
在模块级别 build.gradle 中输入具有依赖项的 Gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
defaultConfig {
applicationId "com.nbapl.avapps.mapscreen"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}