Get Latitude from Location - Android Hardware

Android examples for Hardware:Gps

Description

Get Latitude from Location

Demo Code

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

public class Main {
  private static Location _location;

  public static String GetLatitude(Context context) {

    return Double.toString(GetLastKnownLocation(context).getLatitude());
  }//from   w w w.  jav a 2s  . c  o  m

  private static Location GetLastKnownLocation(Context context) {

    if (_location == null) {
      LocationManager loctionManager;
      String contextService = Context.LOCATION_SERVICE;
      loctionManager = (LocationManager) context.getSystemService(contextService);

      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);// 
      criteria.setAltitudeRequired(false);// 
      criteria.setBearingRequired(false);// 
      criteria.setCostAllowed(true);// 
      criteria.setPowerRequirement(Criteria.POWER_LOW);// 
      String provider = loctionManager.getBestProvider(criteria, true);

      _location = loctionManager.getLastKnownLocation(provider);
    }
    return _location;
  }
}

Related Tutorials