如何在Android中显示简单的地图(使用最新的API)?

时间:2014-01-21 10:33:14

标签: android api google-maps maps

我想要实现的目标只是显示地图。

我知道这个问题早先已被问过,我已经按照线程和教程进行了操作,但是当我尝试以下操作时,我什么也看不见。我的代码在某处出错了吗?

我的代码是否可以使用最新的Map API?

我的java启动器源文件:

package com.devmav.mapstest;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我的布局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"
class="com.google.android.gms.maps.SupportMapFragment"/>

我的AndroidManifest.xml:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<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"/>
  <!-- External storage for caching. -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <!-- My Location -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
  <!-- End of copy. -->


<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="<I ENTERED MY API KEY HERE FROM GOOGLE CLOUD CONSOLE: https://cloud.google.com/console/project"/>

    <activity
        android:name="com.devmav.mapstest.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>

</manifest>

更新1: 在AndroidManifest.xml中添加Google服务后,我看到地图的空白屏幕。

enter image description here

更新2: 我已将权限正确添加到我的应用中。我确信我已经为应用程序正确添加了SHA1指纹和应用程序命名空间。

enter image description here

4 个答案:

答案 0 :(得分:1)

你错过了

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

在清单的应用程序标记中。

确保您已正确引用了库项目。确保您已在google api控制台中启用了Android的地图,并且您有权使用。

看看您是否已按照以下链接中提到的所有步骤操作,您应该会看到地图。

最后在安装了Google Play服务的设备上进行测试。

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

将评论转换为答案

尝试重新生成API密钥。

答案 1 :(得分:1)

只需将其添加到清单文件中的应用程序标记中:

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

或者按照之前回复的this链接进行操作。

答案 2 :(得分:0)

尝试将Google Play服务添加为参考库...

在您的清单文件中

    <permission
        android:name="com.example.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
        <uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

答案 3 :(得分:0)

使用它:

public class MainActivity extends FragmentActivity {

 final int RQS_GooglePlayServices = 1;
 private GoogleMap myMap;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

   FragmentManager myFragmentManager = getSupportFragmentManager();
   SupportMapFragment mySupportMapFragment 
    = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
   myMap = mySupportMapFragment.getMap();

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
     case R.id.menu_legalnotices:
      String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
        getApplicationContext());
      AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
      LicenseDialog.setTitle("Legal Notices");
      LicenseDialog.setMessage(LicenseInfo);
      LicenseDialog.show();
         return true;
     }
  return super.onOptionsItemSelected(item);
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();

  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

  if (resultCode == ConnectionResult.SUCCESS){
   Toast.makeText(getApplicationContext(), 
     "isGooglePlayServicesAvailable SUCCESS", 
     Toast.LENGTH_LONG).show();
  }else{
   GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
  }

 }

}

这是主要活动:

<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"
    tools:context=".MainActivity" >

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

</RelativeLayout>
相关问题