SlideShare a Scribd company logo
2
Most read
6
Most read
8
Most read
Chapter-6
Notes By Mr. C. P.Divate
Location Based Services
Android Google Maps API with Examples
By using Google Maps Android API we can integrate google maps in android applications to show
the location details on map based on our requirements.
To use google maps in our android applications we need to install Google Play Services SDK in
our Android Studio because google made Google Mas API as a part of Google Play Services SDK.
To install Google Play Services, open Android Studio  Go to Tools menu  Android  click SDK
Manager, then new window will open in that select SDK Tools tab  Select Google Play
Services  click OK like as shown below.
Once we are done with Google Play Services installation in android studio, now we will see how to
integrate google map in android app with examples.
Android Google Maps API Example
Following is the example of adding or integrating a google map in android application.
Create a new android application using android studio and give names as GoogleMapExample like
as shown below.
Now we need to select the form factors which we need for our app. In case if you're not sure what
you need, just select Phone and Tablet and then click Next like as shown below.
Now select the Google Maps Activity in 'Add an activity to Mobile' dialog and click Next like as
shown below.
Customize the activity by entering activity name, layout name and title as prompted. In case if default
values are fine, then click Finish like as shown below.
Once the project created, Android Studio will
open google_maps_api.xml and MapsActivity.java files in the editor.
The google_maps_api.xml file will contains instructions to generate a Google Maps API key to
access Google Maps servers. Copy the link provided in the google_maps_api.xml file like as shown
below.
Create a Project in Google Console
Copy and paste the console URL in browser and it will take you to Google API Console like as
shown below. Follow the instructions to create a new project on Google API Console like as shown
below.
Once we click on continue it will create a project and Google Maps Android API will be enabled.
Now we need to create an API key to call the API for that click on Create API Key like as shown
below.
Once we click on Create API Key, it will create an API key to use it in our applications like as shown
below.
Now copy the API Key, go back to android studio and paste the API key into the <string> element
in google_maps_api.xml file like as shown below.
<string name="google_maps_key" templateMergeStrategy="preserve" translatab
le="false">AIzaSyCKPTaBv41DKqr9qxMPWOQAsqp0Q4NHMER</string>
Activity_maps.xml
By default, the XML file (activity_maps.xml) that defines the app's layout is at res/layout/ contains
the following code.
<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.tutlane.googlemapexample.MapsActivity" />
AndroidManifest.xml
Our application manifest file (AndroidManifest.xml) will contain the code like as shown below with
required our Google Maps API Key and permissions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.googlemapexample">
<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">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER
" />
</intent-filter>
</activity>
</application>
</manifest>
If you observe above code it contains a user permission to access location and Meta tag to get
google maps API key.
MapsActivity.java
By default, the Java file (MapsActivity.java) that defines the maps activity will contain the following
code.
package com.tutlane.googlemapexample;
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.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCa
llback {
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) getSupportFr
agmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in hyderabad and move the camera
LatLng hyderadbad = new LatLng(17, 78);
mMap.addMarker(new MarkerOptions().position(hyderadbad).title("Tut
lane in India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hyderadbad));
}
}
Generally, during the launch of our activity, onCreate() callback method will be called by android
framework to get the required layout for an activity.
Output of Android Google Maps Example
When we run the above example using an android virtual device (AVD) we will get a result like as
shown below.
If you observe above result, we are able to see a map with a marker positioned over Hyderabad,
India in our application.
We can customize the appearance of a map based on our requirements in the android application.
Android Google Map Types
The Google Maps Android API provides a map in different types such as Normal, Hybrid, Satellite,
Terrain and None.
Map
Type Description
Normal Typical road map. Shows roads, some features built by humans, and important natural features li
Map
Type Description
rivers. Road and feature labels are also visible.
Hybrid Satellite photograph data with road maps added. Road and feature labels are also visible.
Satellite Satellite photograph data. Road and feature labels are not visible.
Terrian Topographic data. The map includes colors, contour lines and labels, and perspective shading. Som
roads and labels are also visible.
None No tiles. The map will be rendered as an empty grid with no tiles loaded.
In android, we can change the type of a map by calling the GoogleMap
object’s setMapType() method, by passing the type of constants defined in GoogleMap.
Following is the example of displaying the map type as Satellite in the android application.
GoogleMap map;
......
// Set Map type as Satellite
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// Same way we can set other type maps also like as below.
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMapType(GoogleMap.MAP_TYPE_NONE);
This is how we can add or integrate google maps in android applications and use different map
types based on our requirements.

More Related Content

PPTX
Google Location Services
PDF
Basics and different xml files used in android
PPTX
Android MapView and MapActivity
PPTX
Maps in android
PPTX
Mobile Application Development-Components and Layouts
PDF
Create Location Sharing apps using the Ionic framework
PPTX
Google Map Code
PPTX
Location based services 10
Google Location Services
Basics and different xml files used in android
Android MapView and MapActivity
Maps in android
Mobile Application Development-Components and Layouts
Create Location Sharing apps using the Ionic framework
Google Map Code
Location based services 10

Similar to Location Based Services Android Google Maps API with Examples (20)

PDF
Intro To Google Maps
PPTX
Code to Add Google Map to Websites
PPTX
Android ui with xml
PDF
Android app development guide for freshers by ace web academy
PDF
Angular google maps tutorial quick guide
PDF
Android Quiz App – Test Your IQ.pdf
PPT
Beginning Native Android Apps
PPTX
Creating the first app with android studio
PPT
Android tutorial
PPT
Android tutorial
PPT
Android tutorial
PPT
Android tutorial
ODP
Geekcamp Android
PDF
android level 3
PPT
Android-Tutorial.ppt
PPT
Android tutorial
PDF
How to use data binding in android
PDF
Android tutorial
DOC
Unit5 Mobile Application Development.doc
PDF
Ap quiz app
Intro To Google Maps
Code to Add Google Map to Websites
Android ui with xml
Android app development guide for freshers by ace web academy
Angular google maps tutorial quick guide
Android Quiz App – Test Your IQ.pdf
Beginning Native Android Apps
Creating the first app with android studio
Android tutorial
Android tutorial
Android tutorial
Android tutorial
Geekcamp Android
android level 3
Android-Tutorial.ppt
Android tutorial
How to use data binding in android
Android tutorial
Unit5 Mobile Application Development.doc
Ap quiz app
Ad

More from Dr. Chandrakant Divate (20)

PDF
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
PPTX
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
PPTX
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
PPTX
Experiment -7 Performing Asanas In Standing Position
PPTX
Experiment -6 Performing Asanas In Sitting Position
PPTX
Performing Supine Position Asanas- Sleeping on your back.
PPTX
Performing Prone Position Asanas- Sleeping on your back.
PPTX
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
PPTX
Perform warming up exercises to prepare the body from head to toe for Yoga.
PPTX
An introduction to yoga - Brief History and Publicity of Yoga in Universe
PDF
Web Technology LAB MANUAL for Undergraduate Programs
PPTX
UNIVERSAL HUMAN VALUES- Harmony in the Nature
PPTX
Study of Computer Hardware System using Block Diagram
PPTX
Computer System Output Devices Peripherals
PPTX
Computer system Input Devices Peripherals
PPTX
Computer system Input and Output Devices
PPTX
Introduction to COMPUTER’S MEMORY RAM and ROM
PPTX
Introduction to Computer Hardware Systems
PPTX
Fundamentals of Internet of Things (IoT) Part-2
PPTX
Fundamentals of Internet of Things (IoT)
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Experiment -7 Performing Asanas In Standing Position
Experiment -6 Performing Asanas In Sitting Position
Performing Supine Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform warming up exercises to prepare the body from head to toe for Yoga.
An introduction to yoga - Brief History and Publicity of Yoga in Universe
Web Technology LAB MANUAL for Undergraduate Programs
UNIVERSAL HUMAN VALUES- Harmony in the Nature
Study of Computer Hardware System using Block Diagram
Computer System Output Devices Peripherals
Computer system Input Devices Peripherals
Computer system Input and Output Devices
Introduction to COMPUTER’S MEMORY RAM and ROM
Introduction to Computer Hardware Systems
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT)
Ad

Recently uploaded (6)

DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
PPTX
ASMS Telecommunication company Profile
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
PDF
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
ASMS Telecommunication company Profile
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证

Location Based Services Android Google Maps API with Examples

  • 1. Chapter-6 Notes By Mr. C. P.Divate Location Based Services Android Google Maps API with Examples By using Google Maps Android API we can integrate google maps in android applications to show the location details on map based on our requirements. To use google maps in our android applications we need to install Google Play Services SDK in our Android Studio because google made Google Mas API as a part of Google Play Services SDK. To install Google Play Services, open Android Studio  Go to Tools menu  Android  click SDK Manager, then new window will open in that select SDK Tools tab  Select Google Play Services  click OK like as shown below.
  • 2. Once we are done with Google Play Services installation in android studio, now we will see how to integrate google map in android app with examples. Android Google Maps API Example Following is the example of adding or integrating a google map in android application. Create a new android application using android studio and give names as GoogleMapExample like as shown below. Now we need to select the form factors which we need for our app. In case if you're not sure what you need, just select Phone and Tablet and then click Next like as shown below.
  • 3. Now select the Google Maps Activity in 'Add an activity to Mobile' dialog and click Next like as shown below.
  • 4. Customize the activity by entering activity name, layout name and title as prompted. In case if default values are fine, then click Finish like as shown below.
  • 5. Once the project created, Android Studio will open google_maps_api.xml and MapsActivity.java files in the editor. The google_maps_api.xml file will contains instructions to generate a Google Maps API key to access Google Maps servers. Copy the link provided in the google_maps_api.xml file like as shown below.
  • 6. Create a Project in Google Console Copy and paste the console URL in browser and it will take you to Google API Console like as shown below. Follow the instructions to create a new project on Google API Console like as shown below.
  • 7. Once we click on continue it will create a project and Google Maps Android API will be enabled. Now we need to create an API key to call the API for that click on Create API Key like as shown below. Once we click on Create API Key, it will create an API key to use it in our applications like as shown below. Now copy the API Key, go back to android studio and paste the API key into the <string> element in google_maps_api.xml file like as shown below. <string name="google_maps_key" templateMergeStrategy="preserve" translatab le="false">AIzaSyCKPTaBv41DKqr9qxMPWOQAsqp0Q4NHMER</string> Activity_maps.xml By default, the XML file (activity_maps.xml) that defines the app's layout is at res/layout/ contains the following code.
  • 8. <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.tutlane.googlemapexample.MapsActivity" /> AndroidManifest.xml Our application manifest file (AndroidManifest.xml) will contain the code like as shown below with required our Google Maps API Key and permissions. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutlane.googlemapexample"> <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"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER " /> </intent-filter> </activity> </application> </manifest> If you observe above code it contains a user permission to access location and Meta tag to get google maps API key.
  • 9. MapsActivity.java By default, the Java file (MapsActivity.java) that defines the maps activity will contain the following code. package com.tutlane.googlemapexample; 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.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCa llback { 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) getSupportFr agmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in hyderabad and move the camera LatLng hyderadbad = new LatLng(17, 78); mMap.addMarker(new MarkerOptions().position(hyderadbad).title("Tut lane in India")); mMap.moveCamera(CameraUpdateFactory.newLatLng(hyderadbad)); } } Generally, during the launch of our activity, onCreate() callback method will be called by android framework to get the required layout for an activity. Output of Android Google Maps Example
  • 10. When we run the above example using an android virtual device (AVD) we will get a result like as shown below. If you observe above result, we are able to see a map with a marker positioned over Hyderabad, India in our application. We can customize the appearance of a map based on our requirements in the android application. Android Google Map Types The Google Maps Android API provides a map in different types such as Normal, Hybrid, Satellite, Terrain and None. Map Type Description Normal Typical road map. Shows roads, some features built by humans, and important natural features li
  • 11. Map Type Description rivers. Road and feature labels are also visible. Hybrid Satellite photograph data with road maps added. Road and feature labels are also visible. Satellite Satellite photograph data. Road and feature labels are not visible. Terrian Topographic data. The map includes colors, contour lines and labels, and perspective shading. Som roads and labels are also visible. None No tiles. The map will be rendered as an empty grid with no tiles loaded. In android, we can change the type of a map by calling the GoogleMap object’s setMapType() method, by passing the type of constants defined in GoogleMap. Following is the example of displaying the map type as Satellite in the android application. GoogleMap map; ...... // Set Map type as Satellite map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); // Same way we can set other type maps also like as below. map.setMapType(GoogleMap.MAP_TYPE_NORMAL); map.setMapType(GoogleMap.MAP_TYPE_HYBRID); map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); map.setMapType(GoogleMap.MAP_TYPE_NONE); This is how we can add or integrate google maps in android applications and use different map types based on our requirements.