Enter your keyword

Introduction Android Google Maps

Introduction Android Google Maps

Introduction Android Google Maps

Create a Google Maps project in Android studio following these steps;

  1. Start Android Studio.
  2. 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 New, New Project.
  3. Enter your app name, company domain, and project location, as prompted. Then click Next.
  4. 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.
  5. Select Google Maps Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.
    Create Google Maps Project

    ‘Add an activity to Mobile’ dialog

  1. Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.

Android Studio starts Gradle and builds your project. This may take a few seconds.

Get a Google Maps API key

Your application needs an API key to access the Google Maps servers. The type of key you need is an API key with Android restrictions. The key is free. You can use it with any of your applications that call the Google Maps Android API, and it supports an unlimited number of users.

Here is a complete tutorial on acquiring an API key.  There after copy the API key, and paste the API key into the <string name="google_maps_key"> element in the google_maps_api.xml file.

Add the API Key meta-data to the manifest.

Permissions

No permissions are needed to use Google Play Services Maps library, but of course the device must have Internet to load the library.

Add Google Play Services to Your Project

To develop an app that uses Google Maps, it has to make use of the Google Play services APIs. Make the Google Play services APIs available to your app by adding the following dependencies to your build.gradle file inside your application module directory.

Note: In some cases compiling the entire package of APIs into your app will make it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit. If you encounter this build errors, try using --multi-dex option.

From version of Google Play services 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Maps API, replace the following line in your build.gradle file:

Setup the layout

The simplest way to place a map in an application is using the SupportMapFragment. It’s a wrapper around a view of a map to automatically handle the necessary life cycle needs.

The maps activity Java file

The Java file that defines the maps activity is named MapsActivity.java. It extends FragmentActivity because our layout is a fragment and implements onMapReadyCallback; a callback interface for when the map is ready to be used.

In order to use GoogleMap , you have to create an object of GoogleMap. The GoogleMap is acquired using getMapAsync(OnMapReadyCallback). This class automatically initializes the maps system and the view.

 

google-maps-fragment

Android Google Maps