Google Maps is showing a blank map
Asked Answered
T

10

15

I am currently stuck with this problem: Google Maps seems not to be loading at all. Please see the image below: enter image description here

This is really driving me crazy. I already have done the following:

  1. Added my package name + SHA1 (debug keystore) + SHA1 (release keystore) to Google Developer's console.
  2. Maps SDK for Android is enabled. The API key has Android app restriction
  3. Added google-services.json to my app from Firebase console
  4. Added necessary permissions:
  5. Added API Key to the Manifest file - from Google Developer Console
  6. Added meta data for gms version

This is the code of my fragment:

import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.ErrorDialogFragment;
import com.google.android.gms.common.GoogleApiAvailability;
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.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends Fragment implements OnMapReadyCallback {

    private MapFragment mapFragment;
    protected boolean readyToGo() {
        GoogleApiAvailability checker=
                GoogleApiAvailability.getInstance();

        int status=checker.isGooglePlayServicesAvailable(getActivity());

        if (status == ConnectionResult.SUCCESS) {
            if (getVersionFromPackageManager(getActivity())>=2) {
                return(true);
            }
            else {
                Toast.makeText(getActivity(), "no maps", Toast.LENGTH_LONG).show();

            }
        }
        else if (checker.isUserResolvableError(status)) {

        }
        else {
            Toast.makeText(getActivity(),"no maps", Toast.LENGTH_LONG).show();

        }

        return(false);
    }
    private static int getVersionFromPackageManager(Context context) {
        PackageManager packageManager=context.getPackageManager();
        FeatureInfo[] featureInfos=
                packageManager.getSystemAvailableFeatures();
        if (featureInfos != null && featureInfos.length > 0) {
            for (FeatureInfo featureInfo : featureInfos) {
                // Null feature name means this feature is the open
                // gl es version feature.
                if (featureInfo.name == null) {
                    if (featureInfo.reqGlEsVersion != FeatureInfo.GL_ES_VERSION_UNDEFINED) {
                        return getMajorVersion(featureInfo.reqGlEsVersion);
                    }
                    else {
                        return 1; // Lack of property means OpenGL ES
                        // version 1
                    }
                }
            }
        }
        return 1;
    }
    private static int getMajorVersion(int glEsVersion) {
        return((glEsVersion & 0xffff0000) >> 16);
    }
    public static MapsActivity newInstance() {
        MapsActivity fragment = new MapsActivity();
        return fragment;
    }

    public MapsActivity() {
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng augsburg = new LatLng(48.348527, 10.915952);

        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        googleMap.setMyLocationEnabled(true);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));

        googleMap.addMarker(new MarkerOptions()
                .title("Augsburg Zoo")
                .snippet("Der coolste Zoo der Welt")
                .position(augsburg));
    }
}

And this is the code for my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Coming soon!"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"
        android:visibility="invisible" />
</RelativeLayout>

Can anyone tell what still I'm missing?

LOGCAT:

10-17 15:33:25.917 1637-1648/system_process I/ActivityManager: Killing 2335:com.google.android.apps.maps/u0a39 (adj 15): empty #17
10-17 15:33:43.441 3591-3591/com.apptechgateway.AppTech I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:220
    Selected remote version of com.google.android.gms.maps_dynamite, version >= 220
10-17 15:33:43.464 3800-3800/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/x86_64/data@[email protected]@app_chimera@m@[email protected]@classes.dex: Permission denied
10-17 15:33:43.465 3591-3591/com.apptechgateway.AppTech W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86_64 --instruction-set-features=smp,ssse3,sse4.1,sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86_64 --instruction-set-features=default --dex-file=/data/data/com.google.android.gms/app_chimera/m/00000006/MapsDynamite.apk --oat-file=/data/dalvik-cache/x86_64/data@[email protected]@app_chimera@m@[email protected]@classes.dex) because non-0 exit status
10-17 15:33:43.514 3591-3591/com.apptechgateway.AppTech I/Google Maps Android API: Google Play services client version: 12451000
10-17 15:33:43.517 3591-3591/com.apptechgateway.AppTech I/Google Maps Android API: Google Play services package version: 12874027
10-17 15:33:56.802 3591-3591/com.apptechgateway.AppTech I/Google Maps Android API: Google Play services package version: 12874027

Please note that when I use the same API_KEY to other map projects, it is perfectly working. It seems that the map is not showing on my app but working on other projects.

These are my dependecies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'

    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.mikhaellopez:circularimageview:3.2.0'
    implementation 'gr.pantrif:easy-android-splash-screen:0.0.1'
    implementation 'com.github.GrenderG:Toasty:1.3.0'
    implementation 'com.github.adrielcafe:AndroidAudioRecorder:0.3.0'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
    implementation 'com.android.volley:volley:1.1.0'
    compile 'com.github.ybq:Android-SpinKit:1.1.0'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    compile 'com.android.support:cardview-v7:28.0.0'
    compile 'com.github.bumptech.glide:glide:3.6.0'
    compile 'com.github.clans:fab:1.6.4'
    compile 'com.github.blikoon:QRCodeScanner:0.1.2'
    compile 'com.github.droidbyme:DroidDialog:c6decc7167'
    implementation 'com.android.support:multidex:1.0.3'
    compile project(':library')

}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

And this is the content of my AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="MY_API_KEY" />

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.com.vansuita.pickimage.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/picker_provider_paths"
                tools:replace="android:resource" />
        </provider>

        <activity
            android:name=".SplashscreenActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MapsMarkerActivity"
            android:label="MAPS" />
        <activity
            android:name=".MapsActivity2"
            android:label="@string/title_activity_maps2"></activity>
    </application>

</manifest>
Tko answered 17/10, 2018 at 6:38 Comment(18)
whats your logcat says ?Frulla
Provide logcat when map activity shownBicephalous
Show tour logcat there should be exception.Eleen
I already added the logcat @FaizMirTko
have you tried by updating device's play service version ?Konya
I think it has the latest version @TejasPandyaTko
On cordova apps that make use of google maps, when the api key is not correct the map shows like that.Achromatous
@SimãoGarcia The API Key is correct as I have used it on other projectsTko
I think you send wrong logcat partWake
How many build types you have?Publea
You said, you added the package name, not the application id. Are you maybe adding a suffix to it with Gradle?Tipi
What do you mean @TipiTko
It's possible that you're defining applicationIdSuffix in your Gradle file. If you restricted access to your package name, this will not apply to your applicationId given to the project. These two then don't correspond.Tipi
you should try to use mapview instead o Map fragment when using map inside fragmentBerdichev
try using a different api keyProcaine
are you use SHA1 Certificate fingerprints??Wake
Try to enable location in your device, and go your device application manager open the permission section and allow application to have access device location since some device require manually configurationPeduncle
could you please add your main activity where you replacing this fragmentDepurative
H
10

I had this problem yesterday in designing a customer in the company. it was two crazy days to work it out with the customer behind me. What I did was:

  1. Delete the current key (API Key) from the Google Dashboard APIs. Delete the firebase web key from the Google dashboard APIs. Create a new Key API for android, register the SHA-1 and package name. and add in project android studio;

  2. In the FIrebase project delete the SHA-1 code to add again. Download google-services.json, and add in the project;

  3. Generate the app in release mode, publish in the store, and download on some device, when running the app should show the Map normally;

  4. If not, it will show you the error in LogCat, saying that you must register the following SHA-1 key: XXXXXXXXXXXXXXXXX and the package name: com.your.package-name;

  5. Just register that key in Google APIs, and it will work in a few seconds, without even having to update the app in the store.

NOTE: Another thing, consider upgrading the dependencies of Google Maps, and Google Services.

Hope this helps.

Hercule answered 20/10, 2018 at 12:8 Comment(0)
D
9

I am assuming that this may due to mismatch SHA 1 Fingerprint. Double check that you are using the same keystore for getting SHA1.

or Let's check your API key is valid or not. For that add following MAP_API as your key

AIzaSyDztCvpv3PVBxzWwY7mWqCMnGfDeOiGxwY

add key directly to manifest

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyDztCvpv3PVBxzWwY7mWqCMnGfDeOiGxwY" />

Add following permission in your AndroidManifest.xml (Optional/not necessary)

 <permission
    android:name="your_pakage_name.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

Build your apk. And test it.

If the map is showing then we can fix that your API key is the problem.

Otherwise

Maybe due to child fragment manager, So Initialize your fragment in view pager like below. then test it

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="yourpackage.MapsActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />
Depurative answered 26/10, 2018 at 12:7 Comment(0)
E
5

Have you tried these methods?

Enable Multidex:

https://mcmap.net/q/822007/-app-crashed-wiht-classes-dex-permission-denied-error

https://developer.android.com/studio/build/multidex

Note: If your project is configured for multidex with minSdkVersion 20 or lower, and you deploy to target devices running Android 4.4 (API level 20) or lower, Android Studio disables Instant Run.

Disable Instant Run:

https://mcmap.net/q/101086/-how-to-disable-instant-run-for-android-studio-3-0

Edile answered 21/10, 2018 at 12:13 Comment(0)
B
5

try adding this piece of Code in your onMapReady method

MapsInitializer.initialize(Objects.requireNonNull(getContext()));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
googleMap.addMarker(new MarkerOptions()
            .title("Augsburg Zoo")
            .snippet("Der coolste Zoo der Welt")
            .position(augsburg));
Behn answered 23/10, 2018 at 4:35 Comment(0)
M
5

Solution:

 I had the same problem in my App so you can try this open your Android studio and
 make sure you are login with the same user id of what you had created 
 `console.developer` account and after that you should do 



 SupportMapFragment mapFragment;
 GoogleMap mMap;
 Marker marker;
 LatLng origin = new LatLng(30.5595, 22.9375);
 LatLng dest = new LatLng(30.5595, 22.9375);

  @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions()
            .position(origin)
            .title("title")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

    googleMap.addMarker(new MarkerOptions()
            .position(dest));

    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin, 15));
}

enter image description here

Marola answered 26/10, 2018 at 13:20 Comment(0)
P
4

As you are using map in a fragment change your code like below:-

  // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mMapFragment = SupportMapFragment.newInstance();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.mapContainer, mMapFragment).commit();
    mMapFragment.getMapAsync(this);

Change the fragment tag in layout to FrameLayout like below:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout 
      android:id="@+id/mapContainer"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    />


    .....
</RelativeLayout>

Upgrade your map sdk:-

implementation 'com.google.android.gms:play-services-maps:16.0.0'
Pelorus answered 26/10, 2018 at 6:3 Comment(0)
B
3

A few things to note.

  1. Checking runtime permissions (details) - your logcat says permission denied on one of the lines
  2. Try uploading the app to playstore in alfa or beta testing channel (to prevent access to targeted users before app passes all the tests.) - some APIs does not work unless your app is registered.
  3. If still the problem persists, try deleting and re-registering your app in firebase.

  4. Also, please note that google maps APIs are no longer available for free as before. You need to subscribe. Follow this link for more info. Hope this helps.

Burton answered 24/10, 2018 at 9:31 Comment(0)
C
2

I had this problem. In my case, I changed the API key and everything started working correctly. Check the logcat for the issue. If it is the same problem then change the key and it should work.

Clincher answered 26/10, 2018 at 17:31 Comment(0)
F
0

I did a simple fix and Google Maps is working perfectly now: Settings-Apps-Maps-Click on 3 dots at top right corner-Uninstall all updates. It will give you a warning that Maps will be reset to factory settings. Just ignore it and press Ok. Restart the phone.

Flathead answered 6/3, 2021 at 3:59 Comment(0)
Z
0

It is happen because of you may have changed your system. So update with your current system sha1 key and generate new API. It will work.

Zoarah answered 20/11, 2021 at 6:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bristol

© 2022 - 2024 — McMap. All rights reserved.