1

I am currently building an android app. I understand the basics and I did a number of apps but now my project is bigger. My app include a number of screens with NavigationDrawer included which forces me to use an extension of Fragment class on every class I want to be displayed on my application. Here is my problem -some time ago I create an app a simple one page google maps screen and it works just fine. I want to do the same thing on the app I am currently working on but all it gives me when I go to the page is the google log at the bottom left screen and the rest is grey like view (no map is displayed).I searched for solution in a lot of places including this site and I could get as an answer was that it might have to do with my API key given to me by google.I ckecked that and I am pretty sure it is not from the key because I applied it on my one page google map app and it worked perfect.I also have all the permissions needed I checked that like 6 times.So my conclusion is there is something in the code that is preventing the map to be displayed.

Here is my code:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
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 guide extends Fragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.test, container,
                false);

        return v;
    }

    class HoldFragmentPlace extends FragmentActivity{

        private GoogleMap mMap;

        public HoldFragmentPlace(){

        }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.test, container,
                false);

        return v;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

        private void setUpMap() {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }


    }

}

I need that Fragment extension for my NavigationDrawer to work so I create an inner class and applied the code for the map there.

Here is what I have in my manifest:

     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

<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_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And here is my xml for the map:

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

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_marginTop="-470dp" />

</LinearLayout>

I also tried a few other variations of the java code some of them does not include inner class and the result is still the same.I am either missing something very fundamental or I am misunderstanding some conception about Android.If you have read all that thank you for the spared time!

3 Answers3

0

I suspect some part missing :

(1) Declare permission to use MAPS_RECEIVE :

<permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<!-- after declare use this -->
<uses-permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE"/>

(2) Missing Declare gms version variable :

  <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

(3) The API_KEY may have some problem with generation :

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_key" />

Please check with above 3 suspects first : then if still error is there you can re-confirm steps to enable Google Maps from here

Check things related to API_KEY carefully

Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Check the APK signing. You have to assign a debug API key in testing and a Release API key when releasing APK. Insert the correct API key, clean and rebuild your project.

I've been there a year a go and I can assure that this was the problem. Double check your key and if it's a debug or release key.

Another thing to check is the package name of your app... Let me know if you did and still get the grey view instead of the

UPDATE:

I was trying to include everything when I realized that your problem is only in the signing phase.

You only need to get the SHA-1 fingerprint of the debug keystore and put it in your API Key dialog. And trust me there's nothing I can write that can be more clear and informative than the Google documentation.

Follow the steps in this link to get the debug certificate and the SHA-1 fingerprint.

UPDATE 2:

In order to get the SHA-1 of the keystore you might check this thread .

Community
  • 1
  • 1
Owehbeh
  • 579
  • 1
  • 5
  • 16
  • 04-15 12:42:28.960 1961-2088/********************E/Google Maps Android API﹕ Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map. 04-15 12:42:28.960 1961-2088/****************E/Google Maps Android API﹕ In the Google Developer Console (https://console.developers.google.com) Ensure that the "Google Maps Android API v2" is enabled. Ensure that the following Android Key exists: API Key: ***************** Android Application (;): ******************************** – user2961062 Apr 15 '15 at 16:49
  • and also E/Google Maps Android API﹕ Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors). – user2961062 Apr 15 '15 at 16:49
  • So it's telling you in the error message, there's an authorization problem, just as I said earlier check your package name, a missing letter might be the problem. Also make sure that the API key you already generated is a debug key. Can you please confirm doing this and see what happens? – Owehbeh Apr 15 '15 at 16:52
  • so right now I should create a debug key store right?why I can't simply create a release one? and if I will be needing my SHA1 I have a little problem I don't have a debug filder in my android folder – user2961062 Apr 15 '15 at 16:54
  • For release key you have to generate a Release APK file to be uploaded to the play store, that's another phase of development. And yes you have to generate a debug API key. Do it and let me know what happens. – Owehbeh Apr 15 '15 at 16:57
  • 1
    Right well my package name is correct as for the already generated API key when I was building the app I created a map activity which generated xml file in which there was some SHA1 code.I used that to generate my key and I assume that this is a debug key,other than that I don't know how to check if it really is one.Also when I go to gradle.app on signingConfigs{} the method is empty .I found in my .android folder a debug.keystore file.Do you know how can I access it by any chance to see the key? – user2961062 Apr 15 '15 at 17:50
  • I'll post a a detailed answer of how to setup a working android map, from a to z. – Owehbeh Apr 15 '15 at 19:01
  • 1
    Thank you! In the mean time I created a new debug keystore from File->Project Structure and now my gradle have the following signingConfigs { map { keyAlias 'map' keyPassword '*******' storeFile file('C:/Android Key Store/key_store.jks') storePassword '*****' } } – user2961062 Apr 15 '15 at 19:11
  • 1
    I also set it in BuildTypes to start automatically every time.But now when I run it I have this error at run time Error:Execution failed for task ':app:packageDebug'. > Failed to read key from keystore – user2961062 Apr 15 '15 at 19:13
  • I'm editing my answer, and regarding the new error here's a thread were someone had the same problem and got it solved http://stackoverflow.com/questions/20453249/apk-signing-error-failed-to-read-key-from-keystore – Owehbeh Apr 15 '15 at 19:16
  • @user2961062 check the edit, and if I could help please dedicate a little bit of your time to up-vote my answer. – Owehbeh Apr 15 '15 at 21:26
  • right when I run my cmd and direct it to my newly created keystore and type this keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android I get this message: keytool is not recognized as internal or external command – user2961062 Apr 15 '15 at 22:06
  • I've updated the answer, it's okay to fail with this several time, as most of us do fail at the beginning – Owehbeh Apr 15 '15 at 22:11
0

You can use SupportMapFragment to display map in your fragment.

The xml should be changed to

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


<fragment
 android:id="@+id/mapView"
android:layout_marginTop="-470dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

and your class would be like below

public class guide extends Fragment {

private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // inflat and return the layout
    View v = inflater.inflate(R.layout.test, container, false);

    ((SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.mapView)).getMapAsync (new OnMapReadyCallback(){
                @Override
                public void onMapReady(final GoogleMap map) {
                    this.mMap = map;
                    mMap.setMyLocationEnabled(true);
                    setUpMap();
                }
            });


    return v;
}

private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

}

Note: Please update your google play services if you dont find the getMapAsync method for your SupportMapFragment.

dora
  • 2,047
  • 3
  • 18
  • 20