Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Finding Current Location in Android

    • 0
    • 2
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 469
    Comment on it

    To find the current location, follow the steps below:

    1. Create a dialog box to display loading message while finding the location

      ProgressDialog dialog=new ProgressDialog(this);
      dialog.setMessage("Please wait while getting your location information.");
    2. Create a class MyLocationListener that implements LocationListener class

    /**
     * Listener class to get coordinates
     * after finding coordinates AlertBox will be shown
     */
    public class MyLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location loc) 
        {
    
            dialog.dismiss();
            /*----------to get Address from coordinates ------------- */
    
            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());                  
            List
    addresses; String myAddress=null; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) System.out.println(addresses.get(0).getLocality()); Address address = addresses.get(0); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append(" "); sb.append(address.getCountryName()); myAddress="Location:\n"+sb.toString(); System.out.println("Address:::: "+myAddress); } catch (IOException e) { e.printStackTrace(); } mlocManager.removeUpdates(mlocListener); mlocListener=null; } @Override public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Override public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); initLocation(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }

    3.Write the code to check GPS is enabled or not as below:

    /**
     * Check GPS is enable or disable
     * @return networkStatus
     */
    private Boolean displayGpsStatus() {
        boolean network_enabled=mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (network_enabled) {
            return true;
    
        } else {
            return false;
        }
    }
    
    


    4.Create the following method and call MyLocationListener class inside that as below:

    private LocationManager mlocManager=null;
    private LocationListener mlocListener=null;
    /**
     * Find my current location
     */
    private void initLocation()
    {
        try
        {
            mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            boolean network_enabled=displayGpsStatus();
            if(network_enabled)
            {
                dialog.show();
                mlocListener = new MyLocationListener();
                mlocManager.requestLocationUpdates( mlocManager.getBestProvider(new Criteria() , true), 2000,0, mlocListener);
    
            }
            else
            {
                Toast.makeText( getApplicationContext(),"Please enable your Network Location Setting.", Toast.LENGTH_SHORT ).show();
            }
    
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }
    

    Hope this will help you.Please share the experiences you had while finding current location in Android through code as this will help other people who read the post.

 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: