Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Google Map Fixed Location
Google Map displays the current location, navigate location direction, search location etc. To place the Google Map in an application, we need to create the Google Map API key and integrate it in our application.
The Google Map tutorial using Java code is implemented at Android Google Map.
In this tutorial, we will integrate the Google Maps in our Android application. To place the Google Map in application, selects the activity type as Google Maps Activity. By default this activity generates the required configuration and setting which are required for Google map.
To implement the Google Map, we need to generate the Google Map API key and integrate it into our application.
Copy the URL from res/values/google_map_api.xml file and paste it at the browser or we can directly visit at Console Google Developer to generates the Google Map API key.


Click on Create API key to generate API key.

After clicking on Create API key, it will generate our API key displaying the following screen.
Paste the generated API key in our res/values/google_map_api.xml file.
activity_maps.xml
Add the following code in activity_maps.xml layout file.
< 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="example.mcatutorials.com.kotlingooglemap.MapsActivity" />
< 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="example.mcatutorials.com.kotlingooglemap.MapsActivity" />
build.gradle
Add the Google Map Service and Google Location Service dependencies in build.gradle file.
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
}
strings.xml
< resources>
< string name="app_name">Kotlin Google Map< /string>
< string name="title_activity_maps">Map Fixed Location< /string>
< /resources>
< resources>
< string name="app_name">Kotlin Google Map< /string>
< string name="title_activity_maps">Map Fixed Location< /string>
< /resources>
strings.xml
Place the Google Map API key in res/values/google_map_api.xml file.
< resources>
< !--
Follow the directions here:
https://developers.google.com/maps/documentation/android/signup
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">AIzaSyCmTF-n-REPLACE-WITH-YOUR-KEY< /string>
< /resources>
< resources>
< !--
Follow the directions here:
https://developers.google.com/maps/documentation/android/signup
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">AIzaSyCmTF-n-REPLACE-WITH-YOUR-KEY< /string>
< /resources>
MapsActivity.kt
To get the GoogleMap object in our MapsActivity.kt class, we need to implement the OnMapReadyCallback interface and override the onMapReady() callback method. To display the fix location on map, place the latitude and longitude point in the LatLng(latitude, longitude).
GoogleMap.addMarker() points the location of given location.
package example.javatpoint.com.kotlingooglemap
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
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
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
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 fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in India and move the camera
val myLocation = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(myLocation).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation))
mMap.uiSettings.isZoomControlsEnabled = true
}
override fun onMarkerClick(p0: Marker?) = false
}
package example.javatpoint.com.kotlingooglemap
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
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
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
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 fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in India and move the camera
val myLocation = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(myLocation).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation))
mMap.uiSettings.isZoomControlsEnabled = true
}
override fun onMarkerClick(p0: Marker?) = false
}
AndroidManifest.xml
Add the in AndroidManifest.xml file.
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.javatpoint.com.kotlingooglemap">
< 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>
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.javatpoint.com.kotlingooglemap">
< 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>
Output:
MapsActivity.kt
The application built on version Marshmallow facilitates run time user permission. In this class, we create the above example by providing the run time permission to access the device fine location ACCESS_FINE_LOCATION.
package example.javatpoint.com.kotlingooglemap
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.app.FragmentActivity
import android.widget.Toast
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
class MapsActivity : FragmentActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
companion object {
private val MY_PERMISSION_FINE_LOCATION = 101
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in India and move the camera
val india = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(india).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(india))
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_FINE_LOCATION)
}
}
mMap.setOnMarkerClickListener(this)
}
override fun onMarkerClick(p0: Marker?) = false
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array< String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
MY_PERMISSION_FINE_LOCATION -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission to access location grant
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
}
//permission to access location denied
else {
Toast.makeText(applicationContext, "This app requires location permissions to be granted", Toast.LENGTH_LONG).show()
finish()
}
}
}
}
package example.javatpoint.com.kotlingooglemap
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.app.FragmentActivity
import android.widget.Toast
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
class MapsActivity : FragmentActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
companion object {
private val MY_PERMISSION_FINE_LOCATION = 101
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in India and move the camera
val india = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(india).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(india))
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_FINE_LOCATION)
}
}
mMap.setOnMarkerClickListener(this)
}
override fun onMarkerClick(p0: Marker?) = false
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array< String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
MY_PERMISSION_FINE_LOCATION -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission to access location grant
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
}
//permission to access location denied
else {
Toast.makeText(applicationContext, "This app requires location permissions to be granted", Toast.LENGTH_LONG).show()
finish()
}
}
}
}
