MAD Experiment 32
1. Write a program to draw a route between two locations.
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp32">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp32">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
strings.xml:
<resources>
<string name="app_name">Exp31</string>
<string name="google_api_key">AIzaSyD4KfDcLVjVPatpIKtrPXot024Z4UHVZQ8</string>
</resources>
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"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_source"
android:hint="Enter Source Location"
android:padding="12dp"
android:background="@android:drawable/editbox_background"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_destination"
android:hint="Enter Destination Location"
android:padding="12dp"
android:layout_marginTop="8dp"
android:background="@android:drawable/editbox_background"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_track"
android:text="Display Track"
android:layout_marginTop="16dp"/>
</LinearLayout>
MainActivity.java:
package com.example.exp32;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etSource,etDestination;
Button btTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSource = findViewById(R.id.et_source);
etDestination = findViewById(R.id.et_destination);
btTrack = findViewById(R.id.bt_track);
btTrack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = etSource.getText().toString().trim();
String sDestination = etDestination.getText().toString().trim();
if(sSource.equals("") && sDestination.equals("")){
Toast.makeText(getApplicationContext(),"Enter both
location",Toast.LENGTH_SHORT).show();
}else {
DisplayTrack(sSource,sDestination);
}
}
});
}
private void DisplayTrack(String sSource, String sDestination) {
try {
Uri uri =
Uri.parse("https://www.google.co.in/maps/dir/"+sSource+"/"+sDestination);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (ActivityNotFoundException e){
Uri uri =
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.m
aps");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
}
Output: