15 Exp
15 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">
<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
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as
SupportMapFragment
mapFragment.getMapAsync(this)
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.