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

Practical 32

The document provides a practical guide to writing an Android application that draws a route between two locations using Google Maps. It includes XML layout code, Java code for the main activity, and a manifest file with necessary permissions and dependencies. The application marks the start and end points on the map and draws a polyline to represent the route between them.

Uploaded by

shaikhayaannn
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)
21 views3 pages

Practical 32

The document provides a practical guide to writing an Android application that draws a route between two locations using Google Maps. It includes XML layout code, Java code for the main activity, and a manifest file with necessary permissions and dependencies. The application marks the start and end points on the map and draws a polyline to represent the route between them.

Uploaded by

shaikhayaannn
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

Practical 32:-Write a program to draw a route between two locations

Activity:-
<?xml version="1.0" encoding="utf-8"?> android:id="@+id/map"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/re android:name="com.google.android.gms.maps.Supp
s/android" ortMapFragment"
xmlns:app="http://schemas.android.com/apk/res- android:layout_width="match_parent"
auto" android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools" app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/main" app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent" app:layout_constraintTop_toTopOf="parent" />
android:layout_height="match_parent"
tools:context=".MainActivity"> </androidx.constraintlayout.widget.ConstraintLayou
t>
<fragment

Java file:-
package com.example.sam017; super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity; SupportMapFragment mapFragment =
import (SupportMapFragment)
com.google.android.gms.maps.CameraUpdateFactor getSupportFragmentManager()
y; .findFragmentById(R.id.map);
import com.google.android.gms.maps.GoogleMap; if (mapFragment != null) {
import mapFragment.getMapAsync(this);
com.google.android.gms.maps.OnMapReadyCallbac }
k; }
import
com.google.android.gms.maps.SupportMapFragmen @Override
t; public void onMapReady(GoogleMap googleMap)
import com.google.android.gms.maps.model.LatLng; {
import mMap = googleMap;
com.google.android.gms.maps.model.MarkerOption
s; // Define two locations
import LatLng pointA = new LatLng(37.7749, -
com.google.android.gms.maps.model.Polyline; 122.4194); // San Francisco
import LatLng pointB = new LatLng(34.0522, -
com.google.android.gms.maps.model.PolylineOption 118.2437); // Los Angeles
s;
import java.util.ArrayList; // Add markers
import java.util.List; mMap.addMarker(new
MarkerOptions().position(pointA).title("Start
public class MainActivity extends FragmentActivity Point"));
implements OnMapReadyCallback { mMap.addMarker(new
MarkerOptions().position(pointB).title("End Point"));
private GoogleMap mMap;
// Move camera
@Override
protected void onCreate(Bundle mMap.moveCamera(CameraUpdateFactory.newLatL
savedInstanceState) { ngZoom(pointA, 5));
// Draw route android:roundIcon="@mipmap/ic_launcher_round"
drawRoute(pointA, pointB); android:supportsRtl="true"
} android:theme="@style/Theme.Sam017"
tools:targetApi="31">
private void drawRoute(LatLng start, LatLng end) { <activity
List<LatLng> path = new ArrayList<>(); android:name=".MainActivity"
path.add(start); android:exported="true">
<intent-filter>
// Generate intermediate points (linear <action
interpolation) android:name="android.intent.action.MAIN" />
int numPoints = 10;
for (int i = 1; i < numPoints; i++) { <category
double lat = start.latitude + i * (end.latitude - android:name="android.intent.category.LAUNCHER"
start.latitude) / numPoints; />
double lng = start.longitude + i * </intent-filter>
(end.longitude - start.longitude) / numPoints; </activity>
path.add(new LatLng(lat, lng)); <meta-data
}
android:name="com.google.android.geo.API_KEY"
path.add(end);
android:value="YOUR_API_KEY_GOES_HERE"/>
// Draw polyline </application>
Polyline polyline = mMap.addPolyline(new
PolylineOptions().addAll(path).width(5).color(0xFFFF </manifest>
0000)); Gradle script:-
} plugins {
} alias(libs.plugins.android.application)
manifest file:- }
<?xml version="1.0" encoding="utf-8"?>
<manifest android {
xmlns:android="http://schemas.android.com/apk/re namespace = "com.example.sam017"
s/android" compileSdk = 35
xmlns:tools="http://schemas.android.com/tools">
defaultConfig {
<uses-permission applicationId = "com.example.sam017"
android:name="android.permission.INTERNET"/> minSdk = 24
<uses-permission targetSdk = 35
android:name="android.permission.ACCESS_FINE_L versionCode = 1
OCATION"/> versionName = "1.0"
<uses-permission
android:name="android.permission.ACCESS_COARSE testInstrumentationRunner =
_LOCATION"/> "androidx.test.runner.AndroidJUnitRunner"
}
<application
android:allowBackup="true" buildTypes {
release {
android:dataExtractionRules="@xml/data_extractio isMinifyEnabled = false
n_rules" proguardFiles(
getDefaultProguardFile("proguard-android-
android:fullBackupContent="@xml/backup_rules" optimize.txt"),
android:icon="@mipmap/ic_launcher" "proguard-rules.pro"
android:label="@string/app_name" )
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}

dependencies {

implementation("com.google.android.gms:play-
services-maps:18.2.0")

implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
}
output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy