Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get current device location using of GPS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 616
    Comment on it

    This android tutorial will help you to get current device location based on GPS service. GPS is a technology except device internet and WiFi that provide device current location. GPS location provider will give our location accurately. To use of GPS provider follow these steps.

    Step-1- Create XML file

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".AndroidLocationActivity" >
    
        <Button
            android:id="@+id/btnGPSShowLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/btnNWShowLocation"
            android:layout_alignParentTop="true"
            android:layout_marginTop="36dp"
            android:minWidth="100dp"
            android:text="Show Location\n(GPS)" />
    
    
    
    </RelativeLayout>
    

    Step-2- Create Activity class

    public class LocationActivity extends Activity {
    
        Button btnGPSShowLocation;
        Button btnNWShowLocation;
    
        AppLocationService appLocationService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            appLocationService = new AppLocationService(
                    AndroidLocationActivity.this);
    
            btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);
            btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
    
                    Location gpsLocation = appLocationService
                            .getLocation(LocationManager.GPS_PROVIDER);
    
                    if (gpsLocation != null) {
                        double latitude = gpsLocation.getLatitude();
                        double longitude = gpsLocation.getLongitude();
                        getCurrentAddress(latitude,longitude);
                        Toast.makeText(
                                getApplicationContext(),
                                "Mobile Location (GPS): \nLatitude: " + latitude
                                        + "\nLongitude: " + longitude,
                                Toast.LENGTH_LONG).show();
                    } else {
                        showSettingsAlert("GPS");
                    }
    
                }
            });
    
    
        }
    
        protected void getCurrentAddress(double latitude, double longitude) {
            Log.d("callingggggmethod","..............");
            Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(this, Locale.getDefault());
    
            try {
                addresses = geocoder.getFromLocation(latitude, longitude, 1);
    
            String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // Here 1 represent max location result to returned, by documents it recommended 1 to 5
    
    
        }
    
    }
    

    Step-3- Create location service class

    public class AppLocationService extends Service implements LocationListener {
    
        protected LocationManager locationManager;
        Location location;
    
        private static final long MIN_DISTANCE_FOR_UPDATE = 10;
        private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
    
        public AppLocationService(Context context) {
            locationManager = (LocationManager) context
                    .getSystemService(LOCATION_SERVICE);
        }
    
        public Location getLocation(String provider) {
            if (locationManager.isProviderEnabled(provider)) {
                locationManager.requestLocationUpdates(provider,
                        MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(provider);
                    return location;
                }
            }
            return null;
        }
    
        @Override
        public void onLocationChanged(Location location) {
        }
    
        @Override
        public void onProviderDisabled(String provider) {
        }
    
        @Override
        public void onProviderEnabled(String provider) {
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: