无法获取GPS当前位置的纬度和经度Android [英] Can't Get GPS Current Location's Latitude and Longitude Android

查看:112
本文介绍了无法获取GPS当前位置的纬度和经度Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GPS Network Provider 获取当前位置.我的设备的 GPS 已启用,但没有获得 Latitude Longitude .

I am trying to get Current Location using either GPS or Network Provider. My device's GPS is enabled but I'm not getting Latitude and Longitude.

GpsTracker.java:

public class GpsTracker extends Service implements LocationListener {

        private final Context mContext;
        // flag for GPS Status
        boolean isGPSEnabled = false; 
        // flag for network status
        boolean isNetworkEnabled = false;
        boolean canGetLocation = false; 
        Location location;
        public double latitude;
        public double longitude;

        // The minimum distance to change updates in metters
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 
         // metters

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

                    // Declaring a Location Manager
                    protected LocationManager locationManager;

                    public GpsTracker(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 Network Provider Enabled get lat/long using GPS Services
                                if (isNetworkEnabled) {
                                    if (location == null) {
                                        locationManager.requestLocationUpdates(
                                                LocationManager.NETWORK_PROVIDER,
                                                MIN_TIME_BW_UPDATES,
                                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);


                                        if (locationManager != null) {
                                            location = locationManager
                                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                            updateGPSCoordinates();
                                        }
                                    }   
                                }

                                 // if GPS Enabled get lat/long using GPS Services
                                if (isGPSEnabled) {
                                    if (location == null) {
                                        locationManager.requestLocationUpdates(
                                                LocationManager.GPS_PROVIDER,
                                                MIN_TIME_BW_UPDATES,
                                                MIN_DISTANCE_CHANGE_FOR_UPDATES, 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();
                            Log.e("Error : Location",
                                    "Impossible to connect to LocationManager", e); 
                        }

                        return location;
                    }

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

                    public double getLatitude() {
                        if (location != null) {
                            latitude = location.getLatitude();
                        }

                        return latitude;
                    }

                    public double getLongitude() { 
                        if (location != null) {
                            longitude = location.getLongitude();
                        }

                        return longitude;
                    }
                }

Service.Java

Service.Java

    public class WifiScaningService extends IntentService {

        GpsTracker gpsTracker;
        double latitude;
            double longitude;
        public WifiScaningService() {
                super("WifiScaningService"); 

            }

            @Override 
            public IBinder onBind(Intent intent) {    
                // TODO: Return the communication channel to the service.
                throw new UnsupportedOperationException("Not yet implemented");
            }

            @Override
            public void onCreate() {
                Log.e("service call","service call");
            }  


            @Override
            protected void onHandleIntent(Intent intent) {

            } 
            @Override


            public int onStartCommand(Intent intent, int flags, int startId) { 
                    Log.i("LocalService", "Received start id " + startId + ": " + intent);

                    gpsTracker = new GpsTracker(this); 
                    String stringLatitude = String.valueOf(gpsTracker.latitude);
                    latitude = Double.parseDouble(stringLatitude);
                    Log.e("Latitude",stringLatitude);
                    String stringLongitude = String.valueOf(gpsTracker.longitude);
                    longitude = Double.parseDouble(stringLongitude);
                    Log.e("Longitude",stringLongitude); 

                return START_STICKY;
            }
    }

我在AndroidManifest.xml文件中包含了必要的权限.必须显示当前位置.

I have included necessary permissions in AndroidManifest.xml file. Required to show current location.

<uses-permission
  android:name="android.permission.ACCESS_COARSE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
        uses-permission android:name="android.permission.INTERNET"/>

推荐答案

我通过交换两种方法的位置来解决.

I Solved by exchanging position of both methods.

如果未启用 GPS ,则第一个尝试使用 GPS 获取位置的应用,然后使用网络提供商方法.

First app trying to get location using GPS if GPS is not enabled then using network provider method.

public class GpsTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS Status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
public double latitude;
public double longitude;

// The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10
// metters

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

// Declaring a Location Manager
protected LocationManager locationManager;

public GpsTracker(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 GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
            if (location == null) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, 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();
                    }
                }
            }
            // if Network Provider Enabled get lat/long using GPS Services
            if (isNetworkEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);


                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        updateGPSCoordinates();
                    }
                }
            }


        }

    } catch (Exception e) {
        // e.printStackTrace();
        Log.e("Error : Location",
                "Impossible to connect to LocationManager", e);
    }

    return location;
}

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

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    return latitude;
}

public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

}

这篇关于无法获取GPS当前位置的纬度和经度Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆