Android谷歌地图与片段显示空白谷歌地图

时间:2016-01-05 19:47:39

标签: java android google-maps android-fragments

我采用了一种非常简单的方法来在我的Android手机上显示地图片段。我从来没有机会看到地图片段。我总是得到一个带有谷歌水印的空白屏幕,空着。我已按照以下所有步骤操作:

  
      
  1. 我的FragmentActivity类
  2.   
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.maps.CameraUpdate;
    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.MapView;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.UiSettings;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    public class MapsActivity extends FragmentActivity  {
        MapView mapView;
        GoogleMap map;
        LatLng ll;
        UiSettings mapSettings;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) !=                          ConnectionResult.SUCCESS) {
             //change to an alternative layout without fragment
                 setContentView(R.layout.activity_main);
            }
            else
            setContentView(R.layout.activity_maps);
            setUpMapIfNeeded();


        }

        private void setUpMapIfNeeded() 
        {
            if (map == null) 
            {
                map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                if (map != null) 
                {
                    setUpMap();
                }
            }

        }
            private void setUpMap() {
                map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
            }

    }
  

2.我将activity_maps.xml作为我的简单布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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


</LinearLayout>
  

3.我的AndroidManifest.xml

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
    <uses-permission android:name="com.alpha.routefinder.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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.READ_PHONE_STATE" />

<permission 
android:name="com.alpha.routefinder.MAPS_RECEIVE" 
android:protectionLevel="signature"></permission>

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <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" >
        <activity
            android:name=".MapsActivity"
            android:label="GPS route calculator" >
            <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.gms.version"
        android:value="8115000"/>
        <meta-data android:name="com.google.android.maps.v2.API_KEY" 
        android:value="AIzaSyCR_A5-rG4THDJfvzUHck8EhCK358xMa7I"/>
        <uses-library android:name="com.google.android.maps"/>

    </application>


</manifest>
  
      
  1. 我的debug.keystore由我的Java / bin目录下的keytool生成
  2.   
C:\Program Files\Java\jdk1.8.0_60\bin>keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validi
ty 14000
  

5,获取指纹SHA1

C:\Program Files\Java\jdk1.8.0_60\bin>keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android
Certificate fingerprint (SHA1): 1D:FE:FA:5E:C9:74:CF:C1:AD:F9:4B:CC:25:8B:E9:A8:7F:04:64:9D

  
      
  1. 使用导航到
  2. 的SHA1指纹创建Android Api密钥   

html页面:     https://console.developers.google.com/apis/credentials/key/0?project=&#34; myproject的&#34;

enter image description here

我有什么问题? 对你们所有人来说,请给我一个提示!

1 个答案:

答案 0 :(得分:2)

您可能只是忘记在项目中启用GoogleMaps API,如果是这样,请转到您的Google Developers Console&gt;选择你的项目&gt;转到API Manager&gt;在上面的“概览”标签中,点击 Google Maps Android API &gt;然后点击顶部的启用API 按钮