本文介绍了Android Google API“加载地图失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示 Google Map 的地图.
I'm trying to display Google Map's map.
我遵循了这个教程.
我修复了一些问题,应用程序运行,但我无法显示地图.
I Fixed some problems and the application runs, but I could not display the map.
这是我所做的:
- 在谷歌控制台上创建了一个 API 项目
- 创建了一个 ID
- 激活Google Maps Android API v2"服务
- 生成新的 Android 密钥
- 在清单中导入此密钥
- 按照此视频安装库
这里是我的 .java:
And here my .java:
public class MainActivity extends FragmentActivity {
private static GoogleMap mMap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS){
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
UiSettings settings = mMap.getUiSettings();
settings.setZoomControlsEnabled(true);
settings.setCompassEnabled(true);
settings.setRotateGesturesEnabled(true);
settings.setTiltGesturesEnabled(true);
settings.setScrollGesturesEnabled(true);
settings.setZoomControlsEnabled(true);
settings.setZoomGesturesEnabled(true);
settings.setMyLocationButtonEnabled(false);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(false);
}
}
}
}
protected void onResume() {
super.onResume();
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS){
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
UiSettings settings = mMap.getUiSettings();
settings.setZoomControlsEnabled(true);
settings.setCompassEnabled(true);
settings.setRotateGesturesEnabled(true);
settings.setTiltGesturesEnabled(true);
settings.setScrollGesturesEnabled(true);
settings.setZoomControlsEnabled(true);
settings.setZoomGesturesEnabled(true);
settings.setMyLocationButtonEnabled(false);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(false);
}
}
}
}
我的activity_main.xml:
my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
还有我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<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-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<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.map.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="<MyKey>"/>
</application>
</manifest>
推荐答案
在您的应用程序标签内(在清单中)添加:
Inside your application tag (in manifest) add:
<uses-library android:name="com.google.android.maps" />
用这个替换你的片段 XML:
Replace your fragment XML by this:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
让您的活动扩展 FragmentActivity 和 onCreate 和 onResume 调用此代码:
Let your activity extend FragmentActivity and onCreate and onResume call this code:
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS){
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
UiSettings settings = mMap.getUiSettings();
settings.setZoomControlsEnabled(true);
settings.setCompassEnabled(true);
settings.setRotateGesturesEnabled(true);
settings.setTiltGesturesEnabled(true);
settings.setScrollGesturesEnabled(true);
settings.setZoomControlsEnabled(true);
settings.setZoomGesturesEnabled(true);
settings.setMyLocationButtonEnabled(false);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(false);
}
}
}
不要忘记像这样声明你的 mMap 变量:
Don't forget to declare your mMap variable like this:
private GoogleMap mMap;
这篇关于Android Google API“加载地图失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!