简体   繁体   中英

android google maps with fragment display blank google map

I have adopted a very simple approach to display a map segment on my android phone. I never had the chance to see a map segment. I always get a blank screen with google watermark on it, empty. I have followed all the steps as below:

  1. My FragmentActivity class
    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.My activity_maps.xml as my simple layout

<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.My 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. My debug.keystore generation by the keytool under my Java/bin directory
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, Get the fingerprint 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

and

  1. Create Android Api key with the SHA1 fingerprint navigating to

html page: https://console.developers.google.com/apis/credentials/key/0?project= "myproject"

在此处输入图片说明

What is wrong with me? To all of you out there pleeeeeease give me a hint!

I have adopted a very simple approach to display a map segment on my android phone. I never had the chance to see a map segment. I always get a blank screen with google watermark on it, empty. I have followed all the steps as below:

  1. My FragmentActivity class
    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.My activity_maps.xml as my simple layout

<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.My 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. My debug.keystore generation by the keytool under my Java/bin directory
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, Get the fingerprint 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

and

  1. Create Android Api key with the SHA1 fingerprint navigating to

html page: https://console.developers.google.com/apis/credentials/key/0?project= "myproject"

在此处输入图片说明

What is wrong with me? To all of you out there pleeeeeease give me a hint!

I've just been struggling with this and found a away to solve it. Assuming you're like me and you're certain your code, api key and SHA-1 are all set up well, you probably have forgot to enable the "Maps SDK for Android" on the Google console. Here's how to enable it:

Go to Google Cloud console https://console.cloud.google.com/google/maps-apis/overview

Make sure you're logged in to the account you've created your project with.

Open your project.

Click "Credentials"

Click on "Maps SDK for Android" then hit "Enable".

And voila, your app should properly load Google Maps instead of displaying a blanked map now.

https://youtu.be/lDpOkpIPlts观看本视频教程,您将获得最佳解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM