Design an android based application to demostrate GPS services using Google Maps

Code:-

MainActivity.java:-

package com.example.mapservices;

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 view) {

                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.maps");

                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);

                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);

                }

            }

        });

    }}


Activity_main.XML:-

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    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:orientation="vertical"

    android:padding="16dp"

    android:gravity="center_horizontal"

    android:background="@color/black"

    tools:context=".MainActivity">

<TextView

        android:id="@+id/textView2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:fontFamily="sans-serif-black"

        android:layout_marginTop="160dp"

        android:layout_marginBottom="35dp"

        android:text="Google Map"

        android:textAlignment="center"

        android:textColor="@color/white"

        android:textSize="38sp" />


<TextView

        android:id="@+id/textView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

         android:layout_marginBottom="10dp"

        android:fontFamily="sans-serif-black"

        android:text="Use Google Maps Services Here:-"

        android:textColor="@color/white"

        android:textSize="20sp" />


<EditText

        android:id="@+id/et_source"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@color/white"

        android:hint="Enter Source Location"

        android:padding="12dp"

        android:textColorHint="@color/black" />


<EditText

        android:id="@+id/et_destination"

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:layout_marginTop="8dp"

        android:background="@color/white"

        android:hint="Enter Destination Location"

        android:padding="12dp"

        android:textColorHint="@color/black" />


<Button

        android:id="@+id/bt_track"

        android:layout_width="wrap_content"

        android:layout_height="63dp"

        android:layout_marginTop="16dp"

        android:fontFamily="sans-serif-black"

        android:text="Find Route"

        android:textColor="@color/black" />


</LinearLayout>


Preview:-





Here is the code and result of that app:-

Click Here for Code

Click Here for App