0% found this document useful (0 votes)
103 views

Android - GPS Location App

This document provides instructions for creating a simple GPS location application in Android. It describes downloading the Android development environment, designing the application layout, configuring the Android manifest, coding a GPS locator class to get location updates, and writing the main activity code to display the latitude and longitude coordinates. The application uses Android permissions and APIs to access the device's GPS sensor and display the user's current location when they click a button.

Uploaded by

Ashfaq Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Android - GPS Location App

This document provides instructions for creating a simple GPS location application in Android. It describes downloading the Android development environment, designing the application layout, configuring the Android manifest, coding a GPS locator class to get location updates, and writing the main activity code to display the latitude and longitude coordinates. The application uses Android permissions and APIs to access the device's GPS sensor and display the user's current location when they click a button.

Uploaded by

Ashfaq Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Android - Simple GPS Location

Submitted by razormist on Saturday, April 7, 2018 - 14:03.

Operating System
 ANDROID

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. <Button
12. android:id="@+id/btn_locate"
13. android:layout_height="wrap_content"
14. android:layout_width="wrap_content"
15. android:layout_centerInParent="true"
16. android:text="Locate"/>
17.  
18. <TextView
19. android:id="@+id/textView"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content"
22. android:text="Latitude:"
23. android:textSize="20sp"
24. android:layout_marginRight="100dp"
25. android:layout_marginEnd="100dp"
26. android:layout_marginBottom="63dp"
27. android:layout_above="@+id/btn_locate"
28. android:layout_alignRight="@+id/btn_locate"
29. android:layout_alignEnd="@+id/btn_locate" />
30.  
31. <TextView
32. android:id="@+id/tv_latitude"
33. android:textSize="20sp"
34. android:layout_width="wrap_content"
35. android:layout_height="wrap_content"
36. android:layout_alignLeft="@+id/btn_locate"
37. android:layout_alignStart="@+id/btn_locate"
38. android:layout_alignTop="@+id/textView" />
39.  
40.  
41. <TextView
42. android:id="@+id/textView1"
43. android:layout_width="wrap_content"
44. android:layout_height="wrap_content"
45. android:text="Longhitud:"
46. android:textSize="20sp"
47. android:layout_below="@+id/tv_latitude"
48. android:layout_alignRight="@+id/textView"
49. android:layout_alignEnd="@+id/textView"
50. android:layout_marginTop="16dp" />
51.  
52. <TextView
53. android:id="@+id/tv_longhitud"
54. android:layout_width="wrap_content"
55. android:layout_height="wrap_content"
56. android:textSize="20sp"
57. android:layout_alignBaseline="@+id/textView1"
58. android:layout_alignBottom="@+id/textView1"
59. android:layout_alignLeft="@+id/tv_latitude"
60. android:layout_alignStart="@+id/tv_latitude" />
61.  
62.  
63. </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_PROVI
DER, 6000, 10, this);
34. Location location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDE
R);
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!!!
Ad
1/2
00:09
Ngỡ Ngàng Trước Khung Cảnh Của Bình Liêu Quảng Ninh - Thiên Đường Vùng Biên
Giới Việt Trung
Download Code

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.

You might also like