0% found this document useful (0 votes)
7 views3 pages

15 Exp

Experiment in WT

Uploaded by

dinesh2004.10.12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

15 Exp

Experiment in WT

Uploaded by

dinesh2004.10.12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exp.

No :
ANDROID APPLICATION FOR LOCATION BASED SERVICE
Date :

Aim :
To create a simple Android application that displays the user's current location on a
map using Google Maps and the device's GPS.
Procedure :
1. Set Up Development Environment:
a. Install Android Studio and create a new project.
2. Add Required Permissions:
a. Update the AndroidManifest.xml to request permissions for location access.
3. Integrate Google Maps:
a. Enable the Google Maps API and obtain an API key.
b. Add the Maps dependency to the build.gradle file.
4. Create User Interface:
a. Design a layout that includes a MapFragment to display the map.
5. Implement Location Services:
a. Use the Fused Location Provider API to get the user's location.
6. Display User Location:
a. Mark the user’s location on the map when the app starts.
7. Testing:
a. Run the app on a physical device or emulator with location services enabled.

Program :
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationbasedservice">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.kt
import android.Manifest
import android.content.pm.PackageManager
import android.location.Location
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.location.*
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

class MainActivity : AppCompatActivity(), OnMapReadyCallback {


private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var map: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as
SupportMapFragment
mapFragment.getMapAsync(this)

// Request location permission


requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
}

override fun onMapReady(googleMap: GoogleMap) {


map = googleMap
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
location?.let {
val userLocation = LatLng(it.latitude, it.longitude)
map.addMarker(MarkerOptions().position(userLocation).title("You are here"))
map.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15f))
}
}
}
}
}
Sample Output
1. Initial Map Display:
o When the app is launched, the map will display a prompt for location access.
2. User Location:
o Once permission is granted, a marker will appear on the map indicating the user's
current location.

Result:
The study of the app successfully retrieves and displays the user's current location on a
Google Map, showcasing the essential features of a location-based service application. was
studied successfully.

You might also like