Maps Android Studio is not displayed
I've been learning how to use Maps in android studio. The project compiles me OK, but when I open the app on the mobile nothing is seen, only the word google at the bottom.
Already creates and aggregates the api key. what may be going on ?
<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" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--
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="AIzaSyDAEkJrbbqVfGLD7tWhksHRceh8Wqi-j8o" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"></activity>
</application>
3 answers
If your map is not displayed correctly, it is important to check within LogCat
, for example you might have this message:
Ensure that the" Google Maps Android API v2 " is enabled. Ensure that the following Android Key exists:
API Key:
Android Application (;): 10:6D:D9:34:96:A6:1C:5A:45:5F:2C:C5:F7:FA:A5:E0:A2:D3:58: E0;com.chicks.testapplication
To enable the LogCat :
from the menu: Tools
-> Android
-> Android Device Monitor
and there you select the tab " logCat " where information related to your application is displayed.
The main reason the map is not displayed is because it does not have the correct API KEY or the api is not enable.
Going over again the steps to properly configure Google Maps in an Android app:
Go to the development console to enable the API de Google Maps
Https://console.developers.google.com
We look for Google Maps Android API
and go to the option "Credentials
"
Select :
Create Credentials
> API KEY
> Android Key
We add a description and then add the application package and certificate SHA-1
that appears in the LogCat
, it is important to add the correct package since the permissions to display the map are assigned depending on the package and keystore used (certificate SHA-1
).
When creating the API Key with these values you will get a dialog with the value of your API KEY to be configured in your AndroidManifest.xml
When setting this value within your Manifest.xml
<!--Google MAP API key-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAx7OiP9d4py5lWMOFfIrR63a9K-ikLTtw" />
</application>
You can easily load Google Maps in your app:
It is also very important to comment, that the API KEY
depends on the Keystore
with which the application is generated. In my case I have an API KEY generated from the SHA-1
of the debug Kesytore
and another API KEY
generated from the Keystore
with which the application is signed in production.
- if you are working in development get the SHA-1 from the Development Keystore located at:
C:\Users\<user>\.android\debug.keystore
Register it, get the API KEY and configure it in your project.
- in production you must obtain the SHA-1 from the Keystore with which the application is signed for the Google Playstore, register it, obtain the API KEY and configure it in the project.
Android Studio preparations
Following the steps in the Android Maps guide I have been able to extract the prerequisites to use the Google Maps service.
Install the Google play Services
from the SDK Manager
Create apikey from Google Places API for Android
in console: console developer
Add dependencies in File app.graddle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
}
Create values/google_maps_api.xml
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
<!-- código de la APIKEY
https://developers.google.com/maps/documentation/android/start#get-key
-->
</string>
</resources>
Apply permission ACCESS_FINE_LOCATION
in file Manifest.xml
to being able to use google maps
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
And define new activity
<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"></activity>
Create the layout layout/activity_maps.xml
<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.testsettings.app.testgooglemaps.MapsActivity" />
The activity where you want to use the Google maps element has to inherit from FragmentActiity
and load the interface OnMapReadyCallback
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 myLocation = new LatLng(13.0810, 80.2740);
mMap.addMarker(new MarkerOptions().position(myLocation).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(myLocation.latitude, myLocation.longitude), 11.0f), 1500, null);
}
}
I have the same problem, but to me it does not load me when the device has only GPS enabled, when the " Location Mode "is in" high precision mode " with networks and Wifi, the map loads perfectly.. I know that wasn't your problem, but I'll leave it in case someone's having the same thing as me.