得到纬度&第一次运行应用程序时经度为零,但在10分钟后,它将获得正确的值。如何解决这个问题?

时间:2015-05-12 07:03:20

标签: android gps

我第一次来:

latitude:0.00
longitude:0.00
address: not found

10分钟后,我得到了:

latitude:13.00666178
longitude:80.25727619
address:some address

3 个答案:

答案 0 :(得分:0)

private LocationManager mLocationManager;
mLocationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    if (mLocationManager != null) {
        Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            currentLat = location.getLatitude();
            currentLong = location.getLongitude();

        }
}

添加适当的权限

答案 1 :(得分:0)

LocationManager locationManager;
Location location;

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
         location = googleMap.getMyLocation();
         if (location != null) {
            LatLng latLang = new LatLng(location.getLatitude(),location.getLongitude());
            cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLang, 17);
            googleMap.animateCamera(cameraUpdate);
        }
}

答案 2 :(得分:0)

这是我的代码。工作完全正常,立即给出结果

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;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // 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 (!isGPSEnabled && !isNetworkEnabled) {


                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider

                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }

                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                }
                // 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();
        }

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

添加权限

<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"/>