Android Google Mapview Activity not opening in Android Studio

2.9k Views Asked by At

I am new to Android and am trying to display a Google Map on an Android Phone. I created the API Key that should work for any Android App ( I did not give any specific SHA for my app). I am guessing that should not matter. Using the Google Android Tutorials, I am doing all the steps, I get the google watermark on the left bottom, but no map. I will post all the code being used. I have tried to use the Google Maps Activity shipped in Android Studio 1.2.1.1. I don't get any errors, but don't get the map either.

I am using a MapFragment as it seems to be easier than the MapView.

I also viewed the developers console for this API - I dont have any traffic - could it be that the API key is not being used and so my app is not getting the report? I suspect that - how can I test that?

I am trying to follow the steps here - https://developers.google.com/maps/documentation/android/map

Another related query is, I am assuming I can create mutiple layouts. I started with a Blank Activity that acts as my Splash screen, then I am trying to go to a Map Activity - I am not able to create a Map Activity inside Android Studio. I can create it as the first activity through the wizard, but not as a second activity. Am I missing something?

Any help is appreciated

AndroidManifest.xml



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

    <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" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API_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>

</manifest>

I have also used File->Project Structure ->Dependencies - App-> and found that Google Play Services is included.

MapsActivity.java

    package com.nbapl.avapps.mapscreen;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


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

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }



    @Override
    public void onMapReady(GoogleMap map) {
            map.addMarker(new MarkerOptions()
               .position(new LatLng(0, 0))
               .title("This is the center"));
        }

    }

Here is the Layout xml

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame"
    android:background="#ffe2ff0e"
    android:clickable="false"
    android:foreground="#fffff9c5">

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

</FrameLayout>

Gradle file with dependencies enetered at the module level build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    defaultConfig {
        applicationId "com.nbapl.avapps.mapscreen"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
}
2

There are 2 best solutions below

3
On BEST ANSWER

Your activity creates the layout, but you need some additional code to actually start the map:

In MapsActivity.java:

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

    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

Also, your activity should implement OnMapReadyCallback for more map logic. See the documentation at https://developers.google.com/maps/documentation/android/

0
On

After a couple of days of trying all the stuff, I created another project with MapActivity. I followed just the instructions in the google-mp-api.xml that came with the MapActivity. With that, I created new Google Maps credentials and API Key for the App. I replaced that and everything worked. All dependencies, google play services everything was automatically working. The Map with the marker on (0,0) was setup. This works effortlessly. John D's pointer to revisit the API key requirements was useful.