Close

Integrating Google Maps in Android App

Integrating Google Maps in Android App using Android Studio

 

In this blog post I will explain how you can integrate Google Maps in your Android project. Follow the below steps:

 

1. Add the Google Play services package to Android Studio.

2. Create Android Studio Google Map Project. For Creating Android Studio Project follows these given steps:

  • Follow these steps to create a new app project including a map activity:
  • Start Android Studio.
  • Create a new project as follows:
  • If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
  • Otherwise, click File in the Android Studio menu bar, then NewNew Project.
  • Enter your app name, company domain, and project location, as prompted. Then click Next.
  • Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.
  • Select Google Maps Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.
  • Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.

 

Now Android Studio will start gradle and build your project. This may take a few of second to build your project. When the build is finished then Android Studio will opens the google_maps_api.xml and MapsActivity.java files in the Editor.(Activity name can have different name but it will be only what name you are given in setup wizard).

 

3. How to Google Maps API Key: Now your application will require an API key to access the Google Map servers. This key is free and you can use this key in your any of Google map api project. You can get Google Maps API key in following steps.

 

  1. Open the Google Developer Console.
  2. Follow the instructions to create a new project on the console. If you have already created an API project you can select that.
  3. Create an Android API key for your console project. You can create API key by using as given steps:       

           a. Click on Credential, It will list out all your created credential and Create credential option.

           b. Choose Create Credential option, when you click on Create credential it will prompt to select                API key option from given options.
           c. When you click on API key, it will further prompt you for choose appropriate option.
           d. Choose Android key option in given prompt dialog.
           e. As soon as you click on Android key it will ask you the package name of project and sh1 key                of your pc that is created from you keystore file with signed or debug keystore file.
               You can get SH1 key by using given command from command prompt or terminal.
                keytool -list -v -keystore mystore.keystore

       4. Copy the resulting API key, go back to Android Studio, and paste the API key into the <string> element in the xml file.

      Note: For using the service of Google Map into you Android project you must have enabled the Google Map Api from Google API Console page. Your app will not be able to access Google Map until you did not enabled the Google Map API for Android.

 

      5. Now take a look at the Code.

 

  • XML layout file(activity_map) will be look as follows.
       <fragment xmlns:android=”http://schemas.android.com/apk/res/android”
        xmlns:tools=”http://schemas.android.com/tools”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
        android:id=”@+id/map”
        tools:context=”.MapsActivity”
        android:name=”com.google.android.gms.maps.SupportMapFragment” 
       />
  • The Map Activity java file(MapsActivity.java) : By default it takes MapsActivity.java file name. It should contain the following code after your package name:
       import android.os.Bundle;
       import android.support.v4.app.FragmentActivity;
       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.MarkerOptions;

 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

 

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

 

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

 

// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title(“Marker in Sydney”));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

 

If in any case your .java file does not contain above code the you can replace your code with above of the  code in Your MapsActivity.java file.

 

       6. Manifest File(permissions needed for your app)

 

Manifest file contains all the information about your app. In manifest file you must have to declared following part of code. Manifest file should contain following permission.

<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION” /> 

Manifest file should contain information about your google map api key and google play services version in MetaData like as follows: 

<meta-data
android:name=“com.google.android.gms.version”
android:value=“@integer/google_play_services_version” />                               
<meta-data
android:name=“com.google.android.geo.API_KEY”
android:value=“@string/google_maps_key” />

 

       7. Connect an Android device

 

Best way to see your app in action is Run your app in Android device. You can also use Android Emulator as alternate of it. Your android phone should have developer option enabled.

 

        8. Build and Run your app

 

In Android Studio you can run your app by using Run menu option. You can also use play button icon to Run your app. When you play or run your app it will prompt you to select device. You can choose one of the following option:

  • Select your Android phone that you have connected with Computer or
  • You can select Android Emulator radio button and choose available or created virtual Android emulator.
  • Click on Ok, Android Studio will start gradle to build your app and display to corresponding results on a device or in Emulator. It can take couple of minute before open the app. You should able to see corresponding result on Google map with a pin.

 

 Still facing any issue with Google Maps? Or want to be in touch with us? Contact us at  support@triffort.com

 

 

 

Triffort Technologies Pvt Ltd
www.triffort.com

 

 

Posted in Android | Tagged androidgoogle mapsgoogle studiomaps

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

Comment

Name

Email

Social Comments
Go top