• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple GPS Location

katarinagod

Subscription Box Specialist
Divine
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
119
Likes
38
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <

    Button


  9. android:

    id=

    "@+id/btn_locate"
  10. android:

    layout_height=

    "wrap_content"
  11. android:

    layout_width=

    "wrap_content"
  12. android:

    layout_centerInParent=

    "true"
  13. android:

    text=

    "Locate"

    />

  14. <

    TextView
  15. android:

    id=

    "@+id/textView"
  16. android:

    layout_width=

    "wrap_content"
  17. android:

    layout_height=

    "wrap_content"
  18. android:

    text=

    "Latitude:"
  19. android:

    textSize=

    "20sp"
  20. android:

    layout_marginRight=

    "100dp"
  21. android:

    layout_marginEnd=

    "100dp"
  22. android:

    layout_marginBottom=

    "63dp"
  23. android:

    layout_above=

    "@+id/btn_locate"
  24. android:

    layout_alignRight=

    "@+id/btn_locate"
  25. android:

    layout_alignEnd=

    "@+id/btn_locate"

    />

  26. <

    TextView
  27. android:

    id=

    "@+id/tv_latitude"
  28. android:

    textSize=

    "20sp"
  29. android:

    layout_width=

    "wrap_content"
  30. android:

    layout_height=

    "wrap_content"
  31. android:

    layout_alignLeft=

    "@+id/btn_locate"
  32. android:

    layout_alignStart=

    "@+id/btn_locate"
  33. android:

    layout_alignTop=

    "@+id/textView"

    />


  34. <

    TextView
  35. android:

    id=

    "@+id/textView1"
  36. android:

    layout_width=

    "wrap_content"
  37. android:

    layout_height=

    "wrap_content"
  38. android:

    text=

    "Longhitud:"
  39. android:

    textSize=

    "20sp"
  40. android:

    layout_below=

    "@+id/tv_latitude"
  41. android:

    layout_alignRight=

    "@+id/textView"
  42. android:

    layout_alignEnd=

    "@+id/textView"
  43. android:

    layout_marginTop=

    "16dp"

    />

  44. <

    TextView
  45. android:

    id=

    "@+id/tv_longhitud"
  46. android:

    layout_width=

    "wrap_content"
  47. android:

    layout_height=

    "wrap_content"
  48. android:

    textSize=

    "20sp"
  49. android:

    layout_alignBaseline=

    "@+id/textView1"
  50. android:

    layout_alignBottom=

    "@+id/textView1"
  51. android:

    layout_alignLeft=

    "@+id/tv_latitude"
  52. android:

    layout_alignStart=

    "@+id/tv_latitude"

    />


  53. </

    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. <

    uses-

    permission android:

    name=

    "android.permission.ACCESS_FINE_LOCATION"

    />

  5. <

    application
  6. android:

    allowBackup=

    "true"
  7. android:

    icon=

    "@mipmap/ic_launcher"
  8. android:

    label=

    "@string/app_name"
  9. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  10. android:

    supportsRtl=

    "true"
  11. android:

    theme=

    "@Style/AppTheme"

    >
  12. <

    activity android:

    name=

    ".MainActivity"
  13. android:

    configChanges=

    "orientation"
  14. android:

    screenOrientation=

    "portrait"

    >
  15. <

    intent-

    filter>
  16. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  17. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  18. </

    intent-

    filter>
  19. </

    activity>
  20. </

    application>
  21. </

    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. import

    android.Manifest

    ;
  3. import

    android.content.Context

    ;
  4. import

    android.content.pm.PackageManager

    ;
  5. import

    android.location.Location

    ;
  6. import

    android.location.LocationListener

    ;
  7. import

    android.location.LocationManager

    ;
  8. import

    android.os.Bundle

    ;
  9. import

    android.support.v4.content.ContextCompat

    ;
  10. import

    android.widget.Toast

    ;

  11. /**
  12. * Created by Arvin on 4/5/2018.
  13. */

  14. public

    class

    GPSLocator implements

    LocationListener {
  15. Context

    context;

  16. public

    GPSLocator(

    Context

    c)

    {
  17. context =

    c;
  18. }

  19. public

    Location GetLocation(

    )

    {
  20. if

    (

    ContextCompat.checkSelfPermission

    (

    context, Manifest

    .permission

    .ACCESS_FINE_LOCATION

    )

    !=

    PackageManager.PERMISSION_GRANTED

    )

    {
  21. Toast.makeText

    (

    context, "Permission not granted"

    , Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  22. return

    null

    ;
  23. }

  24. LocationManager locationManager =

    (

    LocationManager)

    context.getSystemService

    (

    Context

    .LOCATION_SERVICE

    )

    ;
  25. boolean

    isGPSEnabled =

    locationManager.isProviderEnabled

    (

    LocationManager.GPS_PROVIDER

    )

    ;
  26. if

    (

    isGPSEnabled)

    {
  27. locationManager.requestLocationUpdates

    (

    LocationManager.GPS_PROVIDER

    , 6000

    , 10

    , this

    )

    ;
  28. Location location =

    locationManager.getLastKnownLocation

    (

    LocationManager.GPS_PROVIDER

    )

    ;
  29. return

    location;
  30. }

    else

    {
  31. Toast.makeText

    (

    context, "No GPS Detected"

    , Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  32. }

  33. return

    null

    ;
  34. }

  35. @Override
  36. public

    void

    onLocationChanged(

    Location location)

    {

  37. }

  38. @Override
  39. public

    void

    onStatusChanged(

    String

    provider, int

    status, Bundle extras)

    {

  40. }

  41. @Override
  42. public

    void

    onProviderEnabled(

    String

    provider)

    {

  43. }

  44. @Override
  45. public

    void

    onProviderDisabled(

    String

    provider)

    {

  46. }


  47. }

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. ActivityCompat.requestPermissions

    (

    MainActivity.this

    , new

    String

    [

    ]

    {

    Manifest

    .permission

    .ACCESS_FINE_LOCATION

    }

    , 123

    )

    ;
  5. btn_locate.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  6. @Override
  7. public

    void

    onClick(

    View

    v)

    {
  8. GPSLocator gpsLocator =

    new

    GPSLocator(

    getApplicationContext(

    )

    )

    ;
  9. Location location =

    gpsLocator.GetLocation

    (

    )

    ;
  10. if

    (

    location !=

    null

    )

    {
  11. double

    latitude =

    location.getLatitude

    (

    )

    ;
  12. double

    longhitud =

    location.getLongitude

    (

    )

    ;
  13. tv_latitude.setText

    (

    String

    .valueOf

    (

    latitude)

    )

    ;
  14. tv_longhitud.setText

    (

    String

    .valueOf

    (

    longhitud)

    )

    ;
  15. }
  16. }
  17. }

    )

    ;

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

439,009

316,008

316,017

Top