0

I am trying to connect Maps SDK to my application. I have generated api key and restricted it for android, added package name, SHA-1 fingerprint and restricted it to Maps SDK. Bellow are my dependecies and plugins. When I look at logs i can see that application has been connected to the API, permissions have been granted and map has been initialized. At the end I am getting warning

"Unable to update local snapshot for com.google.android.libraries.consentverifier#com.example.travelnotes"

When I go to google cloud and look at Maps SDK API, requests have been made. I have my api key, permissions, package name, google play services version in AndroidManifest.xml. The fragment for maps is defined in activity_google_maps.xml. When I click on button which is supposed to open the maps I get blank screen with color of my backround and when i move two fingers across the screen a compass is shows up in the upper left corner.

Module level build.gradle

plugins {
    id 'com.android.application'
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    id 'com.google.gms.google-services'

}
android {
    namespace 'com.example.travelnotes'
    compileSdk 33

defaultConfig {
    applicationId "com.example.travelnotes"
    minSdk 19
    targetSdk 33
    versionCode 1
    versionName "1.1"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
        'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

buildToolsVersion '33.0.2'
ndkVersion '25.2.9519653'
buildFeatures {
    viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'com.google.android.gms:play-services-maps:18.1.0'
    implementation 'com.google.android.gms:play-services-basement:18.2.0'
    implementation platform('com.google.firebase:firebase-bom:31.5.0')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

}

Project level build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()

    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'

    }
}

plugins {
    id 'com.android.application' version '7.4.2' apply false
    id 'com.android.library' version '7.4.2' apply false
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' 
    apply false}


task clean(type: Delete) {
    delete rootProject.buildDir
}

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "TravelNotes"
include ':app'

activity_google_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.fragment.app.FragmentContainerView
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".GoogleMapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

GoogleMapsActivity.java

package com.example.travelnotes;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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;

public class GoogleMapActivity extends AppCompatActivity implements OnMapReadyCallback {
    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();

        mMap = googleMap;
        if (mMap == null){
            Log.d(TAG, "map is null");
        }
        else {
            Log.d(TAG, "map is not null");
        }
        LatLng location = new LatLng(37.7749, -122.4194); // San Francisco
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(location)
                .zoom(10) // Zoom level
                .build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
    private static final String TAG = "GoogleMapActivity";
    private Boolean locationPermissionStatus = false;
    private static final int locationPermissionRequestCode = 1234;
    private GoogleMap mMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);
        Log.d(TAG, "Map activity created");
        getLocationPermission();
    }



    private void initializeMap(){
        SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        assert supportMapFragment != null;
        Log.d(TAG, "Map has been initialized");
        supportMapFragment.getMapAsync(GoogleMapActivity.this);

    }

    private void getLocationPermission(){
        Log.d(TAG, "location permission will be given");

        String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION};
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            Log.d(TAG, "location permission will be given");

            if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                Log.d(TAG, "location permission granted");
                locationPermissionStatus = true;
                //initializeMap();
            }
            else {
                ActivityCompat.requestPermissions(this, permissions, locationPermissionRequestCode);
            }
        }
        else {
            ActivityCompat.requestPermissions(this, permissions, locationPermissionRequestCode);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        locationPermissionStatus = false;
        Log.d(TAG, "onRequestPermissionsResult");

        switch (requestCode) {
            case locationPermissionRequestCode: {

                if (grantResults.length > 0) {

                    for (int grantResult : grantResults) {
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, "location not granted");
                            locationPermissionStatus = false;
                            return;
                        }
                    }
                    Log.d(TAG, "location permission granted true");
                    locationPermissionStatus = true;
                    initializeMap();
                }
            }
        }
    }


}

  • Can you please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) by also providing the java activity file? I can't run your code with only these. – Yrll Apr 24 '23 at 03:33
  • @Yrll hi i have added the activity in which i am running the fragment. Sorry for late response – Majerník Tomáš Apr 24 '23 at 19:30
  • I can't seem to run your code in my environment. Can you provide a github repo that I can clone? – Yrll Apr 25 '23 at 04:11
  • [Update] I was able to run your app, and things seems to be working fine. Can you double check how you were implementing your API key? And I think it would be good for you to File a Tech Support case so that they can take a look how you were restricting or how you set up your API key. – Yrll Apr 25 '23 at 06:30
  • @Yrll thank you very much I will check everything again – Majerník Tomáš Apr 25 '23 at 20:07
  • No problem! With regards to `unable to update local snapshot` error, please see this [question](https://stackoverflow.com/questions/71737816/googlecertificatesrslt-not-allowed-in-kotlin-android-application/75915360#75915360) because I think this is relevant and the answer mentioned there that the aforementioned error is actually harmless. That's why I suggested that you check how you were implementing your API keys. – Yrll Apr 26 '23 at 01:40

0 Answers0