Tom Finet
Tom Finet

Reputation: 2136

Google map not showing in android activity?

In my android app I want to have a MainActivity with a google map occupying the entire screen.

My problem is that the google map does not display a map, only the google logo in the bottom left corner:

enter image description here

Here are the relevant parts of the main activity where the map should be displayed:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
        NavigationView.OnNavigationItemSelectedListener {

    private GoogleMap mMap;

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        setupLayout();

        mFragmentManager = getSupportFragmentManager();
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

Here is the xml for the content of the main activity which holds the google map fragment:

<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="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.purringcat.stray.view.activity.MainActivity"/>

Here is the manifest file:

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:name=".Stray"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".view.activity.MainActivity"
            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=".view.activity.LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity android:name=".view.activity.SignUpActivity">
        </activity>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key"/>

    </application>

</manifest>

Upvotes: 6

Views: 33864

Answers (12)

Nikhil S Marathe
Nikhil S Marathe

Reputation: 781

All Coding things done. check properly. then must to do TWO Things with APp.

  1. add SHA -1 Key in Firebase console.

  2. Dowanload and add this json file in your code. THis PC -> "Your project folder" -> app -> "here Put and Update json file"

  3. Go to Google cloud Console. API & Services -> Credentials -> Android key (auto created by Firebase) -> copy "API Key". enter image description here

  4. put this API Key in Manifest file.

Upvotes: 0

Nikhil S Marathe
Nikhil S Marathe

Reputation: 781

  1. The Google Console have Keys like our projects and also auto created.

  2. But for the map Related task use only following key.

  3. But, Further keep in mind, Enable all the Google map API.

  4. The Google Console Have the Key which Name is

        **"Browser key (auto created by Firebase)"**
    

enter image description here

  1. Put this key in your project file in build.app as an "SERVER_KEY"

Ex.. enter image description here

  1. Check the "Package Name and SHA-1 Key" added in a Key which we used for MAP function. enter image description here

and your Map is Enabled.

enter image description here

Upvotes: 0

Deepti
Deepti

Reputation: 954

I was also facing similar issue after updating my app to Android-13 and just found the solution.

After following the standard steps of setting-up google cloud project, I created API key and made it secure by restricting it to my project package with my signing-key fingerprint.

Here was the major problem. Initially, I just added the SHA-1 fingerprint of the signing key which I am using to create the app-bundle. But missed to add the Google generated signing key, which I found in the google-play console (Google play console > Setup (in left drawer menu) > App signing).

As soon as you will add the SHA-1 certificate fingerprint of App signing key certificate in the Android restrictions list of your MAP_API_KEY, the map starts loading.

I hope, this helps. Thanks!

Upvotes: 0

Akhilesh Chaudhary
Akhilesh Chaudhary

Reputation: 43

click on Restrict key and then in API restrictions->don't Restrict key while creating api

Upvotes: 1

manojkragy
manojkragy

Reputation: 19

If the Activity is Opening and the map is not visible even if you are giving Google_map_key , then one solution that worked for me is that i created a new Google map API KEY by copying the link in the google_maps_api.xml and pasting in the browser. Select a project or create a project it will land you on the page where google API KEY is ready copy it and paste it in the place given in the instruction .it will work.

Upvotes: 0

Adesh Gaikwad
Adesh Gaikwad

Reputation: 1

Make sure that you have given these permisssions

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

and check if the WiFi of AVD is on and your PC is connected to internet.

I was getting the same problems, and following the above mentioned steps I was able to resolve my problem.

Upvotes: 0

1) Please update google play services

2) Please append your api key in the url

Ex:-"https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + MY_API_KEY;

Upvotes: 0

georgij
georgij

Reputation: 2200

Google maps likes to get the different activity lifecycle states. So you should send it: map_view.onCreate(savedInstanceState) and map_view.onResume(). It also likes onPause(), onLowMemory(), onSaveInstanceState(outState), onDestroy() if they occur.

Upvotes: 1

Abhijeet Khangarot
Abhijeet Khangarot

Reputation: 1567

Include the following code in AndroidManifest.xml file, within the application part:

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

See documentation here.

Upvotes: 1

10zin
10zin

Reputation: 95

I just checked with google map like yours.

Just compare your code with mine.

MainActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

And Manifest

<?xml version="1.0" encoding="utf-8"?>

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <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>
</application>

Note: You have to create a project in google

click here to visit the google console

Replace your Key value in the google_maps_api

<resources>


<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>

Hope this works for you.

PS: you can generate google map activity from android studio.

Upvotes: 2

Vidhi Dave
Vidhi Dave

Reputation: 5684

You need to enable google map api from google console for your project. And also you need to download json file from the console.

1. Go to google console

2. click on your project.

3. Click on library section from left menu panel. see below image

enter image description here

  1. In google maps api, click on Google maps android api

  2. Than enable that.

  3. Now Goto firebase console

  4. Click on your project and goto project settings and download the json file.

  5. Add that json file in your application.

Upvotes: 9

Preet
Preet

Reputation: 13

Add this permissions to menifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />

OR Check in google console is API ENABLED

OR Check your google_api_key

Upvotes: 1

Related Questions