Android谷歌地图没有显示

时间:2014-01-02 14:36:19

标签: android api map

我跟着This在Android应用程序中创建地图,但我不知道为什么,但是当我启动应用程序时,我看不到地图,这正是我得到的:  检查图片Image1

就像我说的那样,我完全按照教程,但以防万一...这是我的代码:

MainActivity.java:

   public class MainActivity extends Activity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    }
   }

MainActivity.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <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"/>

清单文件(xml):

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Poox2.andtestmap4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="16" />

    <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"/>
    <!-- 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"/>

    <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.gms.version"
             android:value="@integer/google_play_services_version" />
        <activity
            android:name="com.Poox2.andtestmap4.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="AIzaSyDmVLuYyZce...-Rest of the API key -"/>
       </application>

    </manifest>

到目前为止,谢谢你的答案,但它仍然无效

5 个答案:

答案 0 :(得分:0)

更改此

 public class MainActivity extends FragmentActivity {

 public class MainActivity extends Activity {

确保您已在google api控制台中为Android启用了地图。确保清单中的api密钥正确。确保您在Android地图项目中引用了Google Play服务库项目。

最好在真实设备上进行测试。

答案 1 :(得分:0)

尝试通过转到Google API console生成api密钥,甚至尝试添加Google Play服务库..

Select Window > Android SDK Manager or run android at the command line.
Scroll to the bottom of the package list and select Extras > Google Play services. The Google Play services SDK is downloaded to your computer and installed in your Android SDK environment at <android-sdk-folder>/extras/google/google_play_services/

Add the Google Play Services project into your Eclipse workspace.

Click File -> Import..., select Android -> Existing Android Code into Workspace
Browse to and select <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib

To add the dependency to Google Play Services into your project

Project -> Properties -> Android -> Library, Add -> google-play-services_lib

答案 2 :(得分:0)

我遇到了地图显示问题,结果发现我的解决方案是在设备上卸载并重新安装应用程序(而不仅仅是安装在它上面)。我找不到帮助我的SO,但它看起来像API密钥或其他东西被缓存在设备上。

答案 3 :(得分:0)

好的,所以我发现了问题,就是使用Google控制台中的包名...当我生成API密钥时,SHA1密钥是正确的,当我添加包名称时,名称错误了为什么地图没有出现。非常感谢你的帮助,我真的很感激它:)。

答案 4 :(得分:0)

您必须使用片段活动

public class Map extends FragmentActivity {

    GoogleMap map;
    double lati;
    double long;
    boolean flag = false;
    // private LocationManager lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapptry);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
                 }
  }

地图的XML就在这里

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

也这样做

import android.support.v4.app.FragmentActivity;

在您执行此操作之前右键单击project-&gt; properties-&gt; buildpath-&gt; java build path - &gt;库..然后点击添加外部罐子 然后添加所需的jar

如果API密钥错误,那么它也只显示白屏