get GPS Location - Android android.location

Android examples for android.location:GPS

Description

get GPS Location

Demo Code

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

public class Main {

  /**/*from w  ww . ja  v a 2 s.  c  om*/
   * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
   * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
   * 
   * @param context
   * @return
   */
  public static Location getGPSLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setCostAllowed(false);
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);

    String providerName = locationManager.getBestProvider(criteria, true);

    if (providerName != null) {
      Location location = locationManager.getLastKnownLocation(providerName);
      return location;
    } else {
      return null;
    }
  }

}

Related Tutorials