Android Google Mapview not opening in Android Studio
I am new to Android and am trying to display a google map on an android phone. I created an API key that should work for any android app (I didn't provide any specific SHA for my app). I guess it doesn't matter. Using google google tutorials i do all the steps i get google watermark at bottom left but no map. I will post all the code used. I tried to use Google Maps Activity downloaded in Android Studio 1.2.1.1. I am not getting any errors, but I am not getting the map.
I am using MapFragment as it seems to be simpler than MapView.
I also looked at the developer console for this API - I have no traffic - could it be that the API key is not being used and therefore my application is not getting the report? I suspect that - how can I verify this?
I am trying to follow the steps here - https://developers.google.com/maps/documentation/android/map
Another related request: I am assuming I can create multiple layouts. I started with Blank Activity which acts as my Splash screen, then I am trying to navigate to the Activity Map Activity - I cannot create the Map Activity inside Android Studio. I can create it as the first activity using the wizard, but not as the second activity. Did I miss 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 also used File-> Project Structure -> Dependencies - App-> and found that Google Play Services is enabled.
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 xml layout
<?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 installed at 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'
}
source to share
Your activity creates a layout, but you need some extra code to actually run 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 needs to implement OnMapReadyCallback for more map logic. See the documentation at https://developers.google.com/maps/documentation/android/
source to share
After a couple of days trying to find everything, I created another project with MapActivity. I only followed the instructions in google-mp-api.xml that came with MapActivity. With this, I created new Google Maps credentials and an API key for the app. I replaced this and it worked. All dependencies, google play services all worked automatically. The map with marker (0,0) has been customized. It works effortlessly. John D's pointer for revisiting the API requirements was helpful.
source to share