1

I'm trying to display Google Map's map.

I followed this tutorial.

I Fixed some problems and the application runs, but I could not display the map.

Here what I did:

  • Created an API project on the google console
  • Created an ID
  • Activated "Google Maps Android API v2" service
  • Generated new Android key
  • Imported this key in manifest
  • Followed this video to install the library

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);
              }
          }
     }
 }

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>

And my manifest:

<?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>
4

2 回答 2

1

在您的应用程序标签内(在清单中)添加:

<uses-library android:name="com.google.android.maps" />

将您的片段 XML 替换为:

<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 调用此代码:

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 变量:

private GoogleMap mMap;
于 2013-07-09T13:59:36.910 回答
0

基于 nsvir 的早期编辑生成一个新的 debug.keystore 和一个新的密钥解决了这个问题。

本文中的信息有助于了解有关证书的更多信息。

于 2013-10-24T17:17:27.357 回答