Android - Simple GPS Location

Operating System

In this tutorial we will try to create a Simple GPS Location using Android. This simple application can be used to any system that needed the information about the location of the user. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplegpslocation.MainActivity">
  8.  
  9.  
  10.  
  11. android:id="@+id/btn_locate"
  12. android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:layout_centerInParent="true"
  15. android:text="Locate"/>
  16.  
  17. <TextView
  18. android:id="@+id/textView"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="Latitude:"
  22. android:textSize="20sp"
  23. android:layout_marginRight="100dp"
  24. android:layout_marginEnd="100dp"
  25. android:layout_marginBottom="63dp"
  26. android:layout_above="@+id/btn_locate"
  27. android:layout_alignRight="@+id/btn_locate"
  28. android:layout_alignEnd="@+id/btn_locate" />
  29.  
  30. <TextView
  31. android:id="@+id/tv_latitude"
  32. android:textSize="20sp"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignLeft="@+id/btn_locate"
  36. android:layout_alignStart="@+id/btn_locate"
  37. android:layout_alignTop="@+id/textView" />
  38.  
  39.  
  40. <TextView
  41. android:id="@+id/textView1"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="Longhitud:"
  45. android:textSize="20sp"
  46. android:layout_below="@+id/tv_latitude"
  47. android:layout_alignRight="@+id/textView"
  48. android:layout_alignEnd="@+id/textView"
  49. android:layout_marginTop="16dp" />
  50.  
  51. <TextView
  52. android:id="@+id/tv_longhitud"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:textSize="20sp"
  56. android:layout_alignBaseline="@+id/textView1"
  57. android:layout_alignBottom="@+id/textView1"
  58. android:layout_alignLeft="@+id/tv_latitude"
  59. android:layout_alignStart="@+id/tv_latitude" />
  60.  
  61.  
  62. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplegpslocation">
  4.  
  5. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  6.  
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/AppTheme">
  14. <activity android:name=".MainActivity"
  15. android:configChanges="orientation"
  16. android:screenOrientation="portrait">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19.  
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </manifest>

Creating the GPS Locator

This code contains the script for the GPS. This code will access the device GPS to get the information about the actual location of the device. To do that create a new java class called GPSLocator then write these block of codes inside of it.
  1. package com.razormist.simplegpslocation;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.support.v4.content.ContextCompat;
  11. import android.widget.Toast;
  12.  
  13. /**
  14.  * Created by Arvin on 4/5/2018.
  15.  */
  16.  
  17. public class GPSLocator implements LocationListener {
  18. Context context;
  19.  
  20. public GPSLocator(Context c){
  21. context = c;
  22. }
  23.  
  24. public Location GetLocation(){
  25. if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
  26. Toast.makeText(context, "Permission not granted", Toast.LENGTH_SHORT).show();
  27. return null;
  28. }
  29.  
  30. LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  31. boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  32. if(isGPSEnabled){
  33. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
  34. Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  35. return location;
  36. }else{
  37. Toast.makeText(context, "No GPS Detected", Toast.LENGTH_SHORT).show();
  38. }
  39.  
  40. return null;
  41. }
  42.  
  43. @Override
  44. public void onLocationChanged(Location location) {
  45.  
  46. }
  47.  
  48. @Override
  49. public void onStatusChanged(String provider, int status, Bundle extras) {
  50.  
  51. }
  52.  
  53. @Override
  54. public void onProviderEnabled(String provider) {
  55.  
  56. }
  57.  
  58. @Override
  59. public void onProviderDisabled(String provider) {
  60.  
  61. }
  62.  
  63.  
  64. }

The Main Function

This code contains the main function of the application. This code will start gathering the data that has been locate through the device GPS and display the result when the button is clicked. To start with first locate your MainActivity java file and open it, then write these variable inside the MainActivity class.
  1. Button btn_locate;
  2. TextView tv_latitude, tv_longhitud;
Finally, initialize the require methods inside the onCreate method to run the application.
  1. btn_locate = (Button)findViewById(R.id.btn_locate);
  2. tv_latitude = (TextView)findViewById(R.id.tv_latitude);
  3. tv_longhitud = (TextView)findViewById(R.id.tv_longhitud);
  4.  
  5. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
  6. btn_locate.setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. GPSLocator gpsLocator = new GPSLocator(getApplicationContext());
  10. Location location = gpsLocator.GetLocation();
  11. if(location != null){
  12. double latitude = location.getLatitude();
  13. double longhitud = location.getLongitude();
  14. tv_latitude.setText(String.valueOf(latitude));
  15. tv_longhitud.setText(String.valueOf(longhitud));
  16. }
  17. }
  18. });
Try to run the app and see if it worked. There you have it we have created a Simple GPS Location using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byRay-Tray (not verified)on Fri, 03/03/2023 - 05:14

Everything runs very nice, but how to make this app to work next time I run it again, after first use? Does it need to close GPS connection after onDestroy() method?

Add new comment