我正在尝试使用 Google Maps Api V2 构建一个应用程序,但每次都只有一个带有缩放按钮的空白屏幕。我认为我的代码中一切都很好。我拥有所有必要的权限,我正确地获得了 key 。我已经删除了 debug.keystore 来 eclipse 生成另一个 keystore ,但我仍然得到带有缩放按钮的相同空白屏幕。谁能看看我的代码是否做错了什么?
我的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<permission
android:name="com.example.meu.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.meu.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<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="****My key here***" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".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>
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我的 Java 代码:
package com.example.meu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
// Loading map
setContentView(R.layout.activity_main);
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
我已经在这里读了很多东西,但没有人可以帮助我。
最佳答案
首先:由于您的 map 是一个 fragment (MAPS API V.2),您应该将 MainActivity 扩展为 FragmentActivity
public class MainActivity extends FragmentActivity {
而不是
public class MainActivity extends Activity {
其次:将 fragment 转换为 map 时,使用 SupportMapFragment
类(为了更好地兼容 Android 版本)和 getSupportFragmentManager()
找到您的 map fragment 。
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
而不是
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
另外:根据问题“asker”(!?)的评论:如果您发现您的应用程序在模拟器中运行但不在实际设备上运行,请确保在构建应用程序时使用 Debug.Keystore .
注意:始终确保您在整个应用程序中使用向后兼容的支持库(当然,如果您的目标是多个版本的 Android,但我不明白为什么有人会将其应用程序限制为仅几个版本) ?)并且您的所有 key 、凭据、身份验证等都已在 Google 开发人员控制台中正确设置。 https://console.developers.google.com/
关于java - 为什么我只有 Google Maps Android API V2 的空白 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24600095/