EX.
NO : 6 DATE:
Develop an android application to implement a Location Based Services
Aim:
To Develop a Map Application with user current location.
Algorithm:
1. Open android studio and then select New Project from file.
2. Then choose the Empty Activity to develop a new application.
3. Then enter a name for the application as Map and other details in that page and click
finish.
4. Then gradle will build.
5. After finishing gradle building, start the design for the Location page.
6. Write a suitable code in activity_main.xml with necessary constraints for Map.
7. After designing the Action Bar page.
8. If there is no error then connect a physical device like phone or tablet to debug the app.
9. Then run the project. After installation process the app will open on phone with your
current Location page.
Program:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/locationText"
android:text="Location will appear here"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"/>
</LinearLayout>
MainActivity.java
package com.example.ex6;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
public class MainActivity extends AppCompatActivity {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private FusedLocationProviderClient fusedLocationClient;
private TextView locationText;
private MapView mapView;
// Default coordinates for Sethu Institute of Technology
private final double DEFAULT_LATITUDE = 9.651998;
private final double DEFAULT_LONGITUDE = 77.952162;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration.getInstance().setUserAgentValue(getPackageName());
setContentView(R.layout.activity_main);
locationText = findViewById(R.id.locationText);
mapView = findViewById(R.id.map);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setMultiTouchControls(true);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
// Always show default location first
updateMapAndText(DEFAULT_LATITUDE, DEFAULT_LONGITUDE, "Default: Sethu Institute of
Technology");
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else {
getLastLocation();
}
}
private void getLastLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, location -> {
if (location != null) {
updateMapAndText(location.getLatitude(), location.getLongitude(),
"Current Location");
} else {
Toast.makeText(this, "Using default location. Unable to get
current location.", Toast.LENGTH_SHORT).show();
}
});
}
private void updateMapAndText(double latitude, double longitude, String label) {
String locationString = label + " - Lat: " + latitude + ", Long: " + longitude;
locationText.setText(locationString);
GeoPoint point = new GeoPoint(latitude, longitude);
mapView.getController().setZoom(15.0);
mapView.getController().setCenter(point);
Marker marker = new Marker(mapView);
marker.setPosition(point);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
marker.setTitle(label);
mapView.getOverlays().clear();
mapView.getOverlays().add(marker);
mapView.invalidate();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
getLastLocation();
} else {
Toast.makeText(this, "Location permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.EX6"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle (EX6)
// Top-level build file where you can add configuration options common to all sub-
projects/modules.
buildscript {
ext.kotlin_version = '1.8.22'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
}
build.gradle(:app)
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.ex6'
compileSdk 34
defaultConfig {
applicationId "com.example.ex6"
minSdk 24
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22" // 👈 Add this
implementation 'org.osmdroid:osmdroid-android:6.1.16' // Latest as of 2025
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
Output: