- Android - Intents/Filters
- Android - Fragments
- Android - Content Providers
- Android - Broadcast Receivers
- Android - Services
- Android - Activities
- Android - Resources
- Android - Hello World Example
- Android - Application Components
- Android - Architecture
- Android - Environment Setup
- Android - Overview
- Android - Home
Android - User Interface
- Android - Custom Components
- Android - Styles and Themes
- Android - Event Handling
- Android - UI Controls
- Android - UI Layouts
Android Advanced Concepts
- Publishing Android Application
- Android - Phone Calls
- Android - Sending SMS
- Android - Sending Email
- Location Based Services
- Android - Notifications
- Android - Drag and Drop
Android Useful Examples
- Android - XML Parsers
- Android - Widgets
- Android - Wi-Fi
- Android - WebView Layout
- Android - UI Testing
- Android - UI Patterns
- Android - UI Design
- Android - Twitter Integration
- Android - TextureView
- Android - Text to Speech
- Android - Testing
- Android - Support Library
- Android - SQLite Database
- Android - Spelling Checker
- Android - SIP Protocol
- Android - Shared Preferences
- Android - Session Management
- Android - Sensors
- Android - SDK Manager
- Android - Screen Cast
- Android - RSS Reader
- Android - RenderScript
- Android - Push Notification
- Android - ProgressBar
- Android - Progress Circle
- Android - PHP/MySQL
- Android - NFC Guide
- Android - Network Connection
- Android - Navigation
- Android - Multitouch
- Android - MediaPlayer
- Android - Login Screen
- Android - Localization
- Android - Loading Spinner
- Android - Linkedin Integration
- Android - JSON Parser
- Android - JetPlayer
- Android - Internal Storage
- Android - ImageSwitcher
- Android - Image Effects
- Android - Google Maps
- Android - Gestures
- Android - Facebook Integration
- Android - Emulator
- Android - Developer Tools
- Android - Data Backup
- Android - Custom Fonts
- Android - Clipboard
- Android - Camera
- Android - Bluetooth
- Android - Best Practices
- Android - Auto Complete
- Android - AudioManager
- Android - Audio Capture
- Android - Animations
- Android - Alert Dialoges
Android Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Android - Google Maps
Android allows us to integrate google maps in our apppcation. You can show any location on the map , or can show different routes on the map e.t.c. You can also customize the map according to your choices.
Google Map - Layout file
Now you have to add the map fragment into xml layout file. Its syntax is given below −
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/>
Google Map - AndroidManifest file
The next thing you need to do is to add some permissions along with the Google Map API key in the AndroidManifest.XML file. Its syntax is given below −
<!--Permissions--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.google.android.providers.gsf.permission. READ_GSERVICES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--Google MAP API key--> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
Customizing Google Map
You can easily customize google map from its default view , and change it according to your demand.
Adding Marker
You can place a maker with some text over it displaying your location on the map. It can be done by via addMarker() method. Its syntax is given below −
final LatLng TutorialsPoint = new LatLng(21 , 57); Marker TP = googleMap.addMarker(new MarkerOptions() .position(TutorialsPoint).title("TutorialsPoint"));
Changing Map Type
You can also change the type of the MAP. There are four different types of map and each give a different view of the map. These types are Normal,Hybrid,Satelpte and terrain. You can use them as below
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
Enable/Disable zoom
You can also enable or disable the zoom gestures in the map by calpng the setZoomControlsEnabled(boolean) method. Its syntax is given below −
googleMap.getUiSettings().setZoomGesturesEnabled(true);
Apart from these customization, there are other methods available in the GoogleMap class , that helps you more customize the map. They are psted below −
Sr.No | Method & description |
---|---|
1 |
addCircle(CircleOptions options) This method add a circle to the map |
2 |
addPolygon(PolygonOptions options) This method add a polygon to the map |
3 |
addTileOverlay(TileOverlayOptions options) This method add tile overlay to the map |
4 |
animateCamera(CameraUpdate update) This method Moves the map according to the update with an animation |
5 |
clear() This method removes everything from the map. |
6 |
getMyLocation() This method returns the currently displayed user location. |
7 |
moveCamera(CameraUpdate update) This method repositions the camera according to the instructions defined in the update |
8 |
setTrafficEnabled(boolean enabled) This method Toggles the traffic layer on or off. |
9 |
snapshot(GoogleMap.SnapshotReadyCallback callback) This method Takes a snapshot of the map |
10 | stopAnimation() This method stops the camera animation if there is one in progress |
Example
Here is an example demonstrating the use of GoogleMap class. It creates a basic M apppcation that allows you to navigate through the map.
To experiment with this example , you can run this on an actual device or in an emulator.
Create a project with google maps activity as shown below −
![Google Maps](/android/images/google_maps.jpg)
It will open the following screen and copy the console url for API Key as shown below −
![Google Maps](/android/images/google_maps1.jpg)
Copy this and paste it to your browser. It will give the following screen −
![Google Maps](/android/images/google_maps2.jpg)
Cpck on continue and cpck on Create API Key then it will show the following screen
![Google Maps](/android/images/google_maps3.jpg)
Here is the content of activity_main.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.example.tutorialspoint7.myapppcation.MapsActivity" />
Here is the content of MapActivity.java.
In the below code we have given sample latitude and longitude details
package com.example.tutorialspoint7.myapppcation; 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; pubpc 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 pnes, add psteners or move the camera. * In this case, we just add a marker near Sydney, Austrapa. * If Google Play services is not installed on the device. * This method will only be triggered once the user has installed Google Play services and returned to the app. */ @Override pubpc void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng TutorialsPoint = new LatLng(21, 57); mMap.addMarker(new MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com")); mMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint)); } }
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tutorialspoint7.myapppcation"> <!-- 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 functionapty. --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <apppcation android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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 pnked 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 pubpshing. 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="AIzaSyAXhBdyKxUo_cb-EkSgWJQTdqR0QjLcqes" /> <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> </apppcation> </manifest>
Output should be pke this −
![Google Maps](/android/images/google_maps4.jpg)