我知道有很多关于这个主题的问题,但答案对我来说并没有用。我已经生成了密钥,添加了片段代码,引用了api密钥,但屏幕只是灰色,正如其他人所描述的那样,缺少地图问题。我的代码如下。
activity_main.xml中
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flymap_awc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<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 following two 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"/>
<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.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.flymap_awc.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="AIzaSyADeLIgFS42YPAPjUjODWj5nH4y_ZKVfBM"/>
</application>
</manifest>
主要活动Java文件
> package com.example.flymap_awc;
>
> import android.app.Activity; import android.os.Bundle; import
> android.view.Menu;
>
> 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;
>
> 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;
>
> @Override protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
> map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
> .getMap();
> 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)));
>
> // Move the camera instantly to hamburg with a zoom of 15.
> map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
>
> // Zoom in, animating the camera.
> map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); }
>
> @Override public boolean onCreateOptionsMenu(Menu menu) {
> getMenuInflater().inflate(R.menu.main, menu);
> return true; }
>
> }
最终的目标是使用传感器侦听器进行旋转以在地图上移动,但首先我需要让地图显示出来。如果有人可以帮助指出我做错了什么,我将非常感激。谢谢
答案 0 :(得分:0)
请找到可能对您有所帮助的以下考虑因素:
1 . Have you turned on Google Maps Android API V2 in developer console? 2 . Are you using any new device where map has not been loaded yet, This might work as I had similar issue on a samsung device. This might not be a concrete step but you can try
由于