如何在Android中使用getLastKnownLocation方法获取当前纬度和纵向?

TuG*_*llo 4 java gps android location latitude-longitude

我试图获得current手机的位置,为此我使用GPSTracker教程,问题始终是使用方法getLastKnownLocation并返回旧位置.

                if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, 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 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();
                        }
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

你可以getLastKnownLocation在使用NetworkGPS提供者时看到教程使用,但我真的需要当前的位置.

我不知道怎么做到的!

Roa*_*ock 6

将融合位置提供程序API与GoogleApiClient结合使用.你可以在这里找到api .它们旨在为位置提供高精度.使用GoogleApiClient使用它.

new GoogleApiClient.Builder(context)
         .addApi(LocationServices.API)
         .addConnectionCallbacks(this)
         .addOnConnectionFailedListener(this)
         .build()
Run Code Online (Sandbox Code Playgroud)

还有其他使用位置提供程序的方法.您不应将自己局限于使用GPS和移动网络获得的位置.而是迭代所有位置提供商,并根据您的准确性要求选择那些给您高精度的.以下代码执行此操作:

    Location bestResult = null;
    float bestAccuracy = Float.MAX_VALUE;
    long bestAge = Long.MIN_VALUE;
    List<String> matchingProviders = mLocationManager.getAllProviders();

    for (String provider : matchingProviders) {

        Location location = mLocationManager.getLastKnownLocation(provider);

        if (location != null) {

            float accuracy = location.getAccuracy();
            long time = location.getTime();

            if (accuracy < bestAccuracy) {

                bestResult = location;
                bestAccuracy = accuracy;
                bestAge = time;

            }
        }
    }

    // Return best reading or null
    if (bestAccuracy > minAccuracy
            || (System.currentTimeMillis() - bestAge) > maxAge) {
        return null;
    } else {
        return bestResult;
    }
Run Code Online (Sandbox Code Playgroud)

您还可以在检查位置对象的准确性后直接使用它.您应该利用其他应用访问的位置,而不仅仅是依靠GPS /移动网络获得的位置.

如果要持续监听位置更新,请实施Locationlistener回调以持续获取位置更新.