Create a Map Location App in Android Studio

In this blog you will learn how to​​ create a simple​​ Map Location App on android

Studio​​ using Google API Key to generate the program when the app is ran.​​ 

 

Download Android Studio from the link given blow according to your operating system Windows, Mac, Linux, Chrome OS etc.

Link:​​ https://developer.android.com/studio

More downloads are available here.​​ 

Link:​​ https://developer.android.com/studio/archive

 

Steps:

 

  • Start anew Android Studio Project, name it MyMaps(say) then choose Google Maps activity.

 

  • Go to Google clod platfoem, Login--> Create-->Select Project ---> Api Library---> Maps SDK for android--> managae-->Enable.

 

  • Credentials -->create Credintials, API key--->Restrict key.

 

  • App Restrictions--> Android Apps +Add packageName.

 

  • SHA-1 -secure Hash then Save.

 

  • Lastly, copy and add Api Key in the XML file then Build & Run the app.

 

 

D7MOXgTKGhCOAAAAAElFTkSuQmCC - Create a Map Location App in Android Studio

 

AndroiManifest.xml

 

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

 ​​ ​​ ​​​​ package="com.example.sukhman.mymaps">

 ​​ ​​ ​​​​ <!--

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ The ACCESS_COARSE/FINE_LOCATION permissions are not required to use

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Google Maps Android API v2, but you must specify either coarse or fine

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ location permissions for the 'MyLocation' functionality.​​ 

 ​​ ​​ ​​​​ -->

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

 

 ​​ ​​ ​​​​ <application

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:allowBackup="true"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:icon="@mipmap/ic_launcher"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:label="@string/app_name"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:roundIcon="@mipmap/ic_launcher_round"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:supportsRtl="true"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:theme="@style/AppTheme">

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <!--

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 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="@string/google_maps_key" />

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <activity

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:name=".GoogleMapsActivity"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ android:label="@string/title_activity_google_maps">

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <intent-filter>

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <action android:name="android.intent.action.MAIN" />

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ <category android:name="android.intent.category.LAUNCHER" />

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ </intent-filter>

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ </activity>

 ​​ ​​ ​​​​ </application>

 

</manifest>

GoogleMapsActivity.java

 

package com.example.sukhman.mymaps;

 

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.Marker;

import com.google.android.gms.maps.model.MarkerOptions;

 

public class GoogleMapsActivity extends FragmentActivity implements OnMapReadyCallback {

 

 ​​ ​​ ​​​​ /*private*/ GoogleMap mMap;

 

 ​​ ​​ ​​​​ @Override

 ​​ ​​ ​​​​ protected void onCreate(Bundle savedInstanceState) {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ super.onCreate(savedInstanceState);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ setContentView(R.layout.activity_google_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 ludhiana = new LatLng(30.900965, 75.857276);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.addMarker(new MarkerOptions().position(ludhiana).title("Marker in Ludhiana"));

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.moveCamera(CameraUpdateFactory.newLatLng(ludhiana));

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.getUiSettings().setZoomControlsEnabled(true);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.getUiSettings().setCompassEnabled(true);

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.getUiSettings().setMyLocationButtonEnabled(true);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.setTrafficEnabled(true);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ @Override

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public boolean onMarkerClick(Marker marker) {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return false;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ });

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

 ​​ ​​ ​​​​ }

}

google_maps_api.xml

 

<resources>

 ​​ ​​ ​​​​ <!--

 ​​ ​​ ​​​​ TODO: Before you run your application, you need a Google Maps API key.

 

 ​​ ​​ ​​​​ To get one, follow this link, follow the directions and press "Create" at the end:

 

 ​​ ​​ ​​​​ https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=6D:E0:9F:21:38:F4:89:2D:25:08:63:3D:6B:C8:01:27:CA:B4:6F:02%3Bcom.example.sukhman.mymaps

 

 ​​ ​​ ​​​​ You can also add your credentials to an existing key, using these values:

 

 ​​ ​​ ​​​​ Package name:

 ​​ ​​ ​​​​ 6D:E0:9F:21:38:F4:89:2D:25:08:63:3D:6B:C8:01:27:CA:B4:6F:02

 

 ​​ ​​ ​​​​ SHA-1 certificate fingerprint:

 ​​ ​​ ​​​​ 6D:E0:9F:21:38:F4:89:2D:25:08:63:3D:6B:C8:01:27:CA:B4:6F:02

 

 ​​ ​​ ​​​​ Alternatively, follow the directions here:

 ​​ ​​ ​​​​ https://developers.google.com/maps/documentation/android/start#get-key

 

 ​​ ​​ ​​​​ Once you have your key (it starts with "AIza"), replace the "google_maps_key"

 ​​ ​​ ​​​​ string in this file.

 ​​ ​​ ​​​​ -->

 ​​ ​​ ​​​​ <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAb4ArrNSgo---yHeYUFbINFFbmhLE2vdo </string>

</resources>

 

FINAL​​ OUTPUT​​ OF​​ THE​​ APP:

 

Z - Create a Map Location App in Android Studio

 

2Q== - Create a Map Location App in Android Studio

 

So Guys ​​ In this way you can create​​ and build​​ a simple Map Location Application in​​ Android​​ Studio. Hope you like this article. If you still need more help and assistance, please comment down below so that we can help you. Good Luck!!