Get GPS Location - Android Hardware

Android examples for Hardware:Gps

Description

Get GPS Location

Demo Code


//package com.java2s;

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

public class Main {
    /**/* w  w w. ja  v a2s  .  co m*/
     * <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