Welcome to MAGISTER AKUNTANSI - The Perfect Partner For Your Business
Contact : Phone 0821-2566-2195 Wa 0821-2566-2195 Android GPS, Peletakan Lokasi | Magister Akuntansi

Labels

Android GPS, Peletakan Lokasi

Android GPS, Peletakan Lokasi - Hallo sahabat Magister Akuntansi , Pada Artikel yang anda baca kali ini dengan judul Android GPS, Peletakan Lokasi , kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel android , yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Android GPS, Peletakan Lokasi
link : Android GPS, Peletakan Lokasi

Baca juga


Android GPS, Peletakan Lokasi

anda dapat membuat aplikasi anda lebih smart dengan menambahkan fitur yang dapat melacak lokasi pengguna, ditutorial kali ini saya akan mengintegrasikan modul GPS dengan Location API.

buat project baru pada eclipse :

1. Untuk mengakses gps pada Gadget anda membutuhkan permisi yang kita buat menggunakan AndroidManifest.xml, disini kita akan menambahkan ACCESS_FINE_LOCATION (yang mana telah tersedia pada ACCESS_FINE_LOCATION dan ACCESS_COARSE_LOCATION). dan juga kita harus menambahkan permisi INTERNET juga.
buka AndroidManifest.xml dan tambahkan kode berikut :

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. AndroidManifest.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.     package="com.example.gpstracking"
  5.     android:versionCode="1"
  6.     android:versionName="1.0" >
  7.  
  8.     <uses-sdk android:minSdkVersion="8" />
  9.  
  10.     <application
  11.         android:icon="@drawable/ic_launcher"
  12.         android:label="@string/app_name" >
  13.         <activity
  14.             android:name=".AndroidGPSTrackingActivity"
  15.             android:label="@string/app_name" >
  16.             <intent-filter>
  17.                 <action android:name="android.intent.action.MAIN" />
  18.  
  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.             </intent-filter>
  21.         </activity>
  22.     </application>
  23.      
  24.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  25.     <uses-permission android:name="android.permission.INTERNET" />
  26.  
  27. </manifest>

2. Buat Class baru dan beri nama dengan GPSTracker.java atau terserah anda


3. Buka GPSTracker.java tadi dan ubah sesuai kode berikut ini untuk mengakses dan meng-extends dari servisnya


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class GPSTracker extends Service implements LocationListener{
4. Tambahkan variable global dan constructor untuk classnya

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class GPSTracker extends Service implements LocationListener {
  2.  
  3.     private final Context mContext;
  4.  
  5.     // flag for GPS status
  6.     boolean isGPSEnabled = false;
  7.  
  8.     // flag for network status
  9.     boolean isNetworkEnabled = false;
  10.  
  11.     boolean canGetLocation = false;
  12.  
  13.     Location location; // location
  14.     double latitude; // latitude
  15.     double longitude; // longitude
  16.  
  17.     // The minimum distance to change Updates in meters
  18.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  19.  
  20.     // The minimum time between updates in milliseconds
  21.     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  22.  
  23.     // Declaring a Location Manager
  24.     protected LocationManager locationManager;
  25.  
  26.     public GPSTracker(Context context) {
  27.         this.mContext = context;
  28.         getLocation();
  29.     }
5. Disini tambahkan fungsi geoLocation() pada konstruktor

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. GPSTracker.java
  2. public Location getLocation() {
  3.         try {
  4.             locationManager = (LocationManager) mContext
  5.                     .getSystemService(LOCATION_SERVICE);
  6.  
  7.             // getting GPS status
  8.             isGPSEnabled = locationManager
  9.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
  10.  
  11.             // getting network status
  12.             isNetworkEnabled = locationManager
  13.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  14.  
  15.             if (!isGPSEnabled && !isNetworkEnabled) {
  16.                 // no network provider is enabled
  17.             } else {
  18.                 this.canGetLocation = true;
  19.                 // First get location from Network Provider
  20.                 if (isNetworkEnabled) {
  21.                     locationManager.requestLocationUpdates(
  22.                             LocationManager.NETWORK_PROVIDER,
  23.                             MIN_TIME_BW_UPDATES,
  24.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  25.                     Log.d("Network", "Network");
  26.                     if (locationManager != null) {
  27.                         location = locationManager
  28.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  29.                         if (location != null) {
  30.                             latitude = location.getLatitude();
  31.                             longitude = location.getLongitude();
  32.                         }
  33.                     }
  34.                 }
  35.                 // if GPS Enabled get lat/long using GPS Services
  36.                 if (isGPSEnabled) {
  37.                     if (location == null) {
  38.                         locationManager.requestLocationUpdates(
  39.                                 LocationManager.GPS_PROVIDER,
  40.                                 MIN_TIME_BW_UPDATES,
  41.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  42.                         Log.d("GPS Enabled", "GPS Enabled");
  43.                         if (locationManager != null) {
  44.                             location = locationManager
  45.                                     .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  46.                             if (location != null) {
  47.                                 latitude = location.getLatitude();
  48.                                 longitude = location.getLongitude();
  49.                             }
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.  
  55.         } catch (Exception e) {
  56.             e.printStackTrace();
  57.         }
  58.  
  59.         return location;
  60.     }
  61.     @Override
  62.     public void onLocationChanged(Location location) {
  63.     }
  64.  
  65.     @Override
  66.     public void onProviderDisabled(String provider) {
  67.     }
  68.  
  69.     @Override
  70.     public void onProviderEnabled(String provider) {
  71.     }
  72.  
  73.     @Override
  74.     public void onStatusChanged(String provider, int status, Bundle extras) {
  75.     }
  76.  
  77.     @Override
  78.     public IBinder onBind(Intent arg0) {
  79.         return null;
  80.     }

Mendapatkan Lokasi Pengguna

6. Tambahkan fungsi berikut ini  pada class tadi (fungsi ini akan mengembalikan 0.00 apabila gagal)

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. GPSTracker.java
  2. /**
  3.      * Function to get latitude
  4.      * */
  5.     public double getLatitude(){
  6.         if(location != null){
  7.             latitude = location.getLatitude();
  8.         }
  9.          
  10.         // return latitude
  11.         return latitude;
  12.     }
  13.      
  14.     /**
  15.      * Function to get longitude
  16.      * */
  17.     public double getLongitude(){
  18.         if(location != null){
  19.             longitude = location.getLongitude();
  20.         }
  21.          
  22.         // return longitude
  23.         return longitude;
  24.     }

Menanyakan pengguna untuk mengaktifkan GPS.

7. Jika pengguna menonaktifkan GPS kita dapat menggunakannya untuk mempersilahkan pengguna mengaktifkan GPS terlebih dahulu
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.      * Function to check if best network provider
  3.      * @return boolean
  4.      * */
  5.     public boolean canGetLocation() {
  6.         return this.canGetLocation;
  7.     }
  8.      
  9.     /**
  10.      * Function to show settings alert dialog
  11.      * */
  12.     public void showSettingsAlert(){
  13.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  14.      
  15.         // Setting Dialog Title
  16.         alertDialog.setTitle("GPS is settings");
  17.  
  18.         // Setting Dialog Message
  19.         alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  20.  
  21.         // Setting Icon to Dialog
  22.         //alertDialog.setIcon(R.drawable.delete);
  23.  
  24.         // On pressing Settings button
  25.         alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  26.             public void onClick(DialogInterface dialog,int which) {
  27.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  28.                 mContext.startActivity(intent);
  29.             }
  30.         });
  31.  
  32.         // on pressing cancel button
  33.         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  34.             public void onClick(DialogInterface dialog, int which) {
  35.             dialog.cancel();
  36.             }
  37.         });
  38.  
  39.         // Showing Alert Message
  40.         alertDialog.show();
  41.     }


Stop penggunaan GPS

tambahkan beberapa fungsi berikut ini : 


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public void stopUsingGPS(){
  2.         if(locationManager != null){
  3.             locationManager.removeUpdates(GPSTracker.this);
  4.         }      
  5.     }
Dan kode akhir
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. GPSTracker.java
  2. package com.example.gpstracking;
  3.  
  4. import android.app.AlertDialog;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.IBinder;
  14. import android.provider.Settings;
  15. import android.util.Log;
  16.  
  17. public class GPSTracker extends Service implements LocationListener {
  18.  
  19.     private final Context mContext;
  20.  
  21.     // flag for GPS status
  22.     boolean isGPSEnabled = false;
  23.  
  24.     // flag for network status
  25.     boolean isNetworkEnabled = false;
  26.  
  27.     // flag for GPS status
  28.     boolean canGetLocation = false;
  29.  
  30.     Location location; // location
  31.     double latitude; // latitude
  32.     double longitude; // longitude
  33.  
  34.    
  35.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  36.  
  37.    
  38.     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  39.  
  40.     // Declaring a Location Manager
  41.     protected LocationManager locationManager;
  42.  
  43.     public GPSTracker(Context context) {
  44.         this.mContext = context;
  45.         getLocation();
  46.     }
  47.  
  48.     public Location getLocation() {
  49.         try {
  50.             locationManager = (LocationManager) mContext
  51.                     .getSystemService(LOCATION_SERVICE);
  52.  
  53.             // getting GPS status
  54.             isGPSEnabled = locationManager
  55.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
  56.  
  57.             // getting network status
  58.             isNetworkEnabled = locationManager
  59.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  60.  
  61.             if (!isGPSEnabled && !isNetworkEnabled) {
  62.                 // no network provider is enabled
  63.             } else {
  64.                 this.canGetLocation = true;
  65.                 // First get location from Network Provider
  66.                 if (isNetworkEnabled) {
  67.                     locationManager.requestLocationUpdates(
  68.                             LocationManager.NETWORK_PROVIDER,
  69.                             MIN_TIME_BW_UPDATES,
  70.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  71.                     Log.d("Network", "Network");
  72.                     if (locationManager != null) {
  73.                         location = locationManager
  74.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  75.                         if (location != null) {
  76.                             latitude = location.getLatitude();
  77.                             longitude = location.getLongitude();
  78.                         }
  79.                     }
  80.                 }
  81.                 // if GPS Enabled get lat/long using GPS Services
  82.                 if (isGPSEnabled) {
  83.                     if (location == null) {
  84.                         locationManager.requestLocationUpdates(
  85.                                 LocationManager.GPS_PROVIDER,
  86.                                 MIN_TIME_BW_UPDATES,
  87.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  88.                         Log.d("GPS Enabled", "GPS Enabled");
  89.                         if (locationManager != null) {
  90.                             location = locationManager
  91.                                     .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  92.                             if (location != null) {
  93.                                 latitude = location.getLatitude();
  94.                                 longitude = location.getLongitude();
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.  
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.  
  105.         return location;
  106.     }
  107.      
  108.     /**
  109.      * Stop using GPS listener
  110.      * Calling this function will stop using GPS in your app
  111.      * */
  112.     public void stopUsingGPS(){
  113.         if(locationManager != null){
  114.             locationManager.removeUpdates(GPSTracker.this);
  115.         }      
  116.     }
  117.      
  118.     /**
  119.      * Function to get latitude
  120.      * */
  121.     public double getLatitude(){
  122.         if(location != null){
  123.             latitude = location.getLatitude();
  124.         }
  125.          
  126.         // return latitude
  127.         return latitude;
  128.     }
  129.      
  130.     /**
  131.      * Function to get longitude
  132.      * */
  133.     public double getLongitude(){
  134.         if(location != null){
  135.             longitude = location.getLongitude();
  136.         }
  137.          
  138.         // return longitude
  139.         return longitude;
  140.     }
  141.      
  142.     /**
  143.      * Function to check GPS/wifi enabled
  144.      * @return boolean
  145.      * */
  146.     public boolean canGetLocation() {
  147.         return this.canGetLocation;
  148.     }
  149.      
  150.     /**
  151.      * Function to show settings alert dialog
  152.      * On pressing Settings button will lauch Settings Options
  153.      * */
  154.     public void showSettingsAlert(){
  155.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  156.      
  157.         // Setting Dialog Title
  158.         alertDialog.setTitle("GPS is settings");
  159.  
  160.         // Setting Dialog Message
  161.         alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  162.  
  163.         // On pressing Settings button
  164.         alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  165.             public void onClick(DialogInterface dialog,int which) {
  166.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  167.                 mContext.startActivity(intent);
  168.             }
  169.         });
  170.  
  171.         // on pressing cancel button
  172.         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  173.             public void onClick(DialogInterface dialog, int which) {
  174.             dialog.cancel();
  175.             }
  176.         });
  177.  
  178.         // Showing Alert Message
  179.         alertDialog.show();
  180.     }
  181.  
  182.     @Override
  183.     public void onLocationChanged(Location location) {
  184.     }
  185.  
  186.     @Override
  187.     public void onProviderDisabled(String provider) {
  188.     }
  189.  
  190.     @Override
  191.     public void onProviderEnabled(String provider) {
  192.     }
  193.  
  194.     @Override
  195.     public void onStatusChanged(String provider, int status, Bundle extras) {
  196.     }
  197.  
  198.     @Override
  199.     public IBinder onBind(Intent arg0) {
  200.         return null;
  201.     }
  202.  
  203. }

Penggunaan aplikasi GPS pada menggunakan DDMS

kita dapat mencoba aplikasi dengan beberapa cara, salah satunya dengan menggunakan DDMS EClipse Windows ⇒ Show Perspective ⇒ DDMS, setelah membuka DDMS dapat kita temukan emulatornya dan masukkan latitude dan longitude kita secara manual pada emulator




Demikianlah Artikel Android GPS, Peletakan Lokasi

Sekianlah artikel Android GPS, Peletakan Lokasi kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Android GPS, Peletakan Lokasi dengan alamat link https://magisterakutansi.blogspot.com/2014/04/android-gps-peletakan-lokasi.html

Related Posts :

0 Response to " Android GPS, Peletakan Lokasi "