如何使用GPS获取位置?

时间:2014-11-16 09:05:56

标签: android gps location

您好我正在使用以下代码来获取GPS位置 我已经在AndroidManifest.xml中提供了所有必需的权限

if (DeviceUtilities.isNetworkAvailable(context))
        {
            Toast.makeText(HomeScreen.this, "Getting location from GPS",Toast.LENGTH_SHORT).show();
            LocationManager lm;
            lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            String bestProvider = lm.getBestProvider(c, true);
            Location location = lm.getLastKnownLocation(bestProvider);

            if(location!=null)
            {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }

但是获得纬度和经度的空值。 我如何获得位置? 请建议。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

1)创建位置监听器类TrackLocation

    public class TrackLocation extends Service implements LocationListener {

            private final Context mContext;

            // we can get location from gps or network

            // Flag for GPS status
            boolean isGPSEnabled = false;

            // Flag for network status
            boolean isNetworkEnabled = false;

            // Flag for GPS status
            boolean canGetLocation = false;

            Location location; // Location
            double latitude; // Latitude
            double longitude; // Longitude

            // distance to change Updates in meters
            private static final long MIN_DISTANCE = 10; // 10 meters

            // The minimum time between updates in milliseconds
            private static final long MIN_TIME = 1000 * 60 * 1; // 1 minute

            // Declaring a Location Manager
            protected LocationManager locationManager;

            public TrackLocation(Context context) {
                this.mContext = context;
                getLocation();
            }

            public Location getLocation() {
                try {
                    locationManager = (LocationManager) mContext
                            .getSystemService(LOCATION_SERVICE);

                    // Getting GPS status
                    isGPSEnabled = locationManager
                            .isProviderEnabled(LocationManager.GPS_PROVIDER);

                    // Getting network status
                    isNetworkEnabled = locationManager
                            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                    if (!isGPSEnabled && !isNetworkEnabled) {
                        // No network provider is enabled
                    } else {
                        this.canGetLocation = true;
                        if (isNetworkEnabled) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.NETWORK_PROVIDER,
                                    MIN_TIME,
                                    MIN_DISTANCE, this);
                            Log.d("Network", "Network");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                        // If GPS enabled, get latitude/longitude using GPS Services
                        if (isGPSEnabled) {
                            if (location == null) {
                                locationManager.requestLocationUpdates(
                                        LocationManager.GPS_PROVIDER,
                                        MIN_TIME,
                                        MIN_DISTANCE, this);
                                Log.d("GPS Enabled", "GPS Enabled");
                                if (locationManager != null) {
                                    location = locationManager
                                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                    if (location != null) {
                                        latitude = location.getLatitude();
                                        longitude = location.getLongitude();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

                return location;
            }


            /**
             * Stop using GPS listener
             * Calling this function will stop using GPS in your app.
             * */
            public void stopUsingGPS(){
                if(locationManager != null){
                    locationManager.removeUpdates(GPSTracker.this);
                }
            }


            /**
             * Function to get latitude
             * */
            public double getLatitude(){
                if(location != null){
                    latitude = location.getLatitude();
                }

                // return latitude
                return latitude;
            }


            /**
             * Function to get longitude
             * */
            public double getLongitude(){
                if(location != null){
                    longitude = location.getLongitude();
                }

                // return longitude
                return longitude;
            }

            /**
             * Function to check GPS/Wi-Fi enabled
             * @return boolean
             * */
            public boolean canGetLocation() {
                return this.canGetLocation;
            }


            @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;
            }
    }  

2)然后在大班做这个

TrackLocation gps = new TrackLocation(this);  
//Check if GPS enabled
if(gps.canGetLocation()) {
    // now get get coordinates
    double latitude = gps.getLatitude();
    double longitude = gps.getLongitude();

} else {
    // Can't get location.
}

答案 1 :(得分:0)

如果手机没有最后一个位置,

“getLastKnownLocation”可以返回null,这意味着其他应用最近没有请求或获取该位置。

如果使用getLastKnowLocation获取null,则必须获取询问gps或网络的位置,并在侦听器中等待响应。

一些代码片段:

LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER OR LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

您必须实现locationListener才能获得该职位。

更多信息: http://developer.android.com/reference/android/location/LocationManager.html

注意:这种方式是使用LocationManager,您也可以使用播放服务,但我更喜欢使用位置管理器。