Practical No. 32,31,29 New
Practical No. 32,31,29 New
Practical No. 32,31,29 New
32
Write a program to draw a route between two location
Java Code: package com.example.pr32;
import static android.content.Intent.ACTION_VIEW;
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 source, destination;
Button track;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
source = findViewById(R.id.et_source);
destination = findViewById(R.id.et_destination);
track = findViewById(R.id.track);
track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = source.getText().toString();
String sDestination = destination.getText().toString();
if (sSource.equals("") && sDestination.equals("")) {
Toast.makeText(MainActivity.this,
"Please enter your Source/Destination",
Toast.LENGTH_SHORT).show();}
else{
DisplayTrack(sSource, sDestination);}}});}
private void DisplayTrack(String sSource, String sDestination) {
try {
// Construct the Google Maps URL with source and destination addresses
Uri uri = Uri.parse("https://www.google.com/maps/dir/?api=1&origin="
+ sSource + "&destination=" + sDestination);
Intent intent = new Intent(ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);}
catch (ActivityNotFoundException e){
// If Google Maps app is not installed, open the Google Play Store link
Uri uri=
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
Intent intent = new Intent(ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);}}}
XML File: <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECECEC"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Source:"
android:textSize="36sp"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:id="@+id/et_source"
android:textColor="#212121"
android:textColorHint="#575757"
android:backgroundTint="#575757"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Destination:"
android:textSize="36sp"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/et_destination"
android:textColor="#212121"
android:textColorHint="#575757"
android:backgroundTint="#575757"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DRAW ROUTE"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:id="@+id/track" />
</RelativeLayout>
Output-
Practical no. 31
Write a program to locate a user’s current location.
Java Code: package com.example.pr31;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
FusedLocationProviderClient mFusedLocationClient;
TextView latitudeTextView, longitTextView;
int PERMISSION_ID = 44;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTextView = findViewById(R.id.latitude);
longitTextView = findViewById(R.id.longitude);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
getLastLocation();}
@SuppressLint("MissingPermission")
private void getLastLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(this, new
OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
latitudeTextView.setText(location.getLatitude() + "");
longitTextView.setText(location.getLongitude() + "");}}
});} else {
Toast.makeText(this, "Please turn on your location...",
Toast.LENGTH_LONG).show();
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}} else {
requestPermissions();}}
@SuppressLint("MissingPermission")
private void requestNewLocationData() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5);
locationRequest.setFastestInterval(0);
locationRequest.setNumUpdates(1);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.myLooper());}
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
latitudeTextView.setText("Latitude: " + location.getLatitude() + "");
longitTextView.setText("Longitude: " + location.getLongitude() + "");}};
private boolean checkPermissions() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
return true;}
return false;}
private void requestPermissions() {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ID);}
private boolean isLocationEnabled() {
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
return
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == PERMISSION_ID) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
getLastLocation();}}}
@Override
public void onResume() {
super.onResume();
if (checkPermissions()) {
getLastLocation();}}}
Output-