Android - Simple GPS Location

Submitted by: 
Operating System: 
Visitors have accessed this post 4229 times.

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplegpslocation.MainActivity">
  11.  
  12.  
  13.  
  14. android:id="@+id/btn_locate"
  15. android:layout_height="wrap_content"
  16. android:layout_width="wrap_content"
  17. android:layout_centerInParent="true"
  18. android:text="Locate"/>
  19.  
  20. <TextView
  21. android:id="@+id/textView"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="Latitude:"
  25. android:textSize="20sp"
  26. android:layout_marginRight="100dp"
  27. android:layout_marginEnd="100dp"
  28. android:layout_marginBottom="63dp"
  29. android:layout_above="@+id/btn_locate"
  30. android:layout_alignRight="@+id/btn_locate"
  31. android:layout_alignEnd="@+id/btn_locate" />
  32.  
  33. <TextView
  34. android:id="@+id/tv_latitude"
  35. android:textSize="20sp"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_alignLeft="@+id/btn_locate"
  39. android:layout_alignStart="@+id/btn_locate"
  40. android:layout_alignTop="@+id/textView" />
  41.  
  42.  
  43. <TextView
  44. android:id="@+id/textView1"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="Longhitud:"
  48. android:textSize="20sp"
  49. android:layout_below="@+id/tv_latitude"
  50. android:layout_alignRight="@+id/textView"
  51. android:layout_alignEnd="@+id/textView"
  52. android:layout_marginTop="16dp" />
  53.  
  54. <TextView
  55. android:id="@+id/tv_longhitud"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:textSize="20sp"
  59. android:layout_alignBaseline="@+id/textView1"
  60. android:layout_alignBottom="@+id/textView1"
  61. android:layout_alignLeft="@+id/tv_latitude"
  62. android:layout_alignStart="@+id/tv_latitude" />
  63.  
  64.  
  65. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplegpslocation">
  5.  
  6. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  7.  
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity"
  16. android:configChanges="orientation"
  17. android:screenOrientation="portrait">
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20.  
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.