Google Maps [API] view is gray on my mobile device

喜夏-厌秋 提交于 2019-12-14 03:51:32

问题


Hi guyes I really need your help.

I've read the instructions on how to use Google API on this link: https://developers.google.com/maps/documentation/android/start

I've followed all seven steps but I couldn't display Google Map in my app.

[I have already got tired from trying to get the Emulator to work so I've decided to generate an APK file instead and send it to my smartphone] After intalling the app, once I open it I can only see a grey screen and 'Google' signeture at the bottem left of the screen. that's all, nothing happens.

The app works fine when I connect my device and use the debug mode. I've tried installing it Via the adb but it still didn't work.

I've been following many articles for the last three days regarding this issue but no matter what I've tried it just didn't work.

What am I doing wrong?

Here is my code:

Manifest:

        <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="josh.com.googlemapslast" >

    <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" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application


        android:allowBackup="true"
        android:icon="@mipmap/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=".MapsActivity"
            android:label="@string/title_activity_maps" >
            <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.geo.API_KEY"
            android:value="AIza**********"/>
    </application>

</manifest>

Main Activity Java:

package josh.com.googlemap2;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

}

Main Activity Layout:

<fragment 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"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

Goole_Maps_api.xml

 <string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
        AIzaSyAnvkHr4CGYzmROT*******
    </string>
</resources>

回答1:


Alright, There are two things that you should note if you encounter a similar situation:

1) There are two google maps api.xml files, one for release and one for debug. you can find them on these paths:

\app\src\release\res\values

\app\src\debug\res\values

Make sure to add your Google Map Key on both files!

2) I didn't find this information on official source but you need to generate another SHA1 key for the release file in addition to your regular SHA1 key.

You must have two SHA1 keys, one for the debug version and one for the release version.

Once you have the new key for the release file, add it to your Google API Console exisiting key at the second row.



来源:https://stackoverflow.com/questions/31904024/google-maps-api-view-is-gray-on-my-mobile-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!