0% found this document useful (0 votes)
24 views4 pages

Mad 32

The document outlines the implementation of a map-based application that allows users to draw a route between two locations using Android. It includes the XML layout for the user interface, Java code for the main activity to handle map functionality, and necessary permissions in the AndroidManifest.xml. Additionally, it specifies dependencies required for building the application in the Build.gradle file.
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)
24 views4 pages

Mad 32

The document outlines the implementation of a map-based application that allows users to draw a route between two locations using Android. It includes the XML layout for the user interface, Java code for the main activity to handle map functionality, and necessary permissions in the AndroidManifest.xml. Additionally, it specifies dependencies required for building the application in the Build.gradle file.
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/ 4

Name: Rohit Deshpande Enrolment No.

: 2215770085
Roll No.: 14 Batch No.: T1

Practical No. 32: Deploy map-based application. Part II.

1. Write a program to draw a route between two locations.


activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<EditText
android:id="@+id/origin_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="@string/enter_origin"
android:imeOptions="actionDone"
android:inputType="text"
android:minHeight="48dp"
android:textColorHint="#546E7A"
android:autofillHints=""
tools:ignore="VisualLintTextFieldSize" />

<EditText
android:id="@+id/destination_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="@string/enter_destination"
android:imeOptions="actionDone"
android:inputType="text"
android:minHeight="48dp"
android:textColorHint="#546E7A"
android:autofillHints=""
tools:ignore="VisualLintTextFieldSize" />

<Button
android:id="@+id/draw_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/draw_route"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"/>

</LinearLayout>

MainActivity.java:
package com.example.route;

import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.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.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.List;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;


private EditText originEditText, destinationEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()


.findFragmentById(R.id.map);
assert mapFragment != null;
mapFragment.getMapAsync(this);

originEditText = findViewById(R.id.origin_edittext);
destinationEditText = findViewById(R.id.destination_edittext);
Button drawRouteButton = findViewById(R.id.draw_route_button);

drawRouteButton.setOnClickListener(v -> drawRoute());


}

private void drawRoute() {


String originAddress = originEditText.getText().toString();
String destinationAddress = destinationEditText.getText().toString();
LatLng originLatLng = getLocationFromAddress(originAddress);
LatLng destinationLatLng = getLocationFromAddress(destinationAddress);

if (originLatLng != null && destinationLatLng != null) {


mMap.clear();
mMap.addMarker(new MarkerOptions().position(originLatLng).title("Origin"));
mMap.addMarker(new MarkerOptions().position(destinationLatLng).title("Destination"));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(originLatLng, 10));

mMap.addPolyline(new PolylineOptions()
.add(originLatLng, destinationLatLng)
.width(5)
.color(Color.RED));
}
}

private LatLng getLocationFromAddress(String strAddress) {


Geocoder coder = new Geocoder(this);
List<Address> address;
LatLng latLng = null;

try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
latLng = new LatLng(location.getLatitude(), location.getLongitude());
} catch (Exception ex) {
ex.printStackTrace();
}
return latLng;
}

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
}
}

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.INTERNET" />


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

<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.Route"
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>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBAzlVZYxBjtWHImkF3tQB_VTgGmK0wclo" />
</application>

</manifest>

Build.gradle:
dependencies {

implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout

implementation libs.play.services.maps
implementation libs.play.services.location

testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
}:

You might also like