1

I've wanted to add a map to my app and I have followed all the instructions on the google maps api website. 1. I have made the project and added a new google maps activity. 2. I have gotten an api key from the link provided in google_maps_api.xml and entered it. 3. I have added the permissions:

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

4. I have added the meta data : <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> But when I open the activity, the google logo is there but it shows an empty map. Why is this happening and what's the solution? Here's my code:

Androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.barlificent.transport">

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

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


    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <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>
</application>

MapActivity.java

package com.barlificent.transport;
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 {
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);
}


@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));
}

}

birukhimself
  • 193
  • 3
  • 14
  • Seems your code is ok. Please double check your api key. – Andrii Omelchenko Dec 03 '17 at 11:11
  • I agree with u @Andrii Omelchenko. may be the issue is api key. Plz search to youtube. First time was in trouble too. But watching that i solved my problem... – Md . Sojib Ahmed Dec 03 '17 at 11:23
  • @AndriiOmelchenko I already double checked my api key but nothing seems to work. I even used the link in the google_maps_api.xml instead of manually getting one. – birukhimself Dec 03 '17 at 12:48
  • 1
    You should generate it via [Google APIs Console](https://code.google.com/apis/console) for you application package, not get from `google_maps_api.xm`. Take a look at [this](http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html) tutorial. Seems your issue is in wrong key. – Andrii Omelchenko Dec 03 '17 at 13:59
  • @birukster741 - did you find a solution to this problem? – J E Carter II Feb 18 '19 at 19:32

1 Answers1

0

Comments above seem to be correct. I ran into this same apparent issue and when I looked further into the developers console, you may indeed need to create a new key or project. As your key can also be restricted to Android, iOS, or certain Http Referers and more, the key you are attempting to use might be restricted in a way that prevents it from working with an Android app. In my case, I created a new project and just create a key for it. You could also try changing the key restrictions on the current API key, but it seems best to me to have a separate key for separate activities.

  1. Login to your developer console: https://console.developers.google.com

  2. Create a project and make sure it is selected in the drop down to the right of the Google APIs logo.

  3. Click on the drop down menu to the left of the GoogleAPIs logo.

  4. Select API's & Services > Credentials

  5. Click Create credentials > API Key

  6. Optionally restrict your key.

J E Carter II
  • 1,436
  • 1
  • 22
  • 39