简体   繁体   English

在某些Android手机中无法准确定位

[英]Not gettting accurate location in some android phones

I have used below code. 我用下面的代码。 It work fine but in some devices like Redme,Asus not getting accurate location.It shown me too far from my current location aprox 20 km away.Not getting where is problem somebody have idea please share there ideas.Thanks in advance. 它可以正常工作,但是在Redme,Asus之类的设备上无法获得准确的位置。它表明我离当前位置约20公里,离当前位置太远。如果没有到哪里出了问题,请有人与我分享想法。谢谢。

public class Currentlocation 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
public 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;

SetOnLocationFoundListner OLF;

public interface SetOnLocationFoundListner {
    public void onLocationFound(Location location, boolean getLocRegularly, GoogleMap gmap);
}

public Currentlocation(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 (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();
                        }
                    }
                }
            }
        }

    } 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(Currentlocation.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/wifi enabled
 *
 * @return boolean
 */
public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

/**
 * Function to show settings alert dialog On pressing Settings button will
 * lauch Settings Options
 */

}

You can not expect the phone neither to always have a valid "last known location" nor to define its location in microseconds. 您不能期望电话既不会始终具有有效的“最后知道的位置”,也不能以毫秒为单位定义其位置。

You first call requestLocationUpdates() and instantly after that getLastKnownLocation() . 您首先调用requestLocationUpdates()然后立即调用getLastKnownLocation() There's no time for the phone to determine/update the "last known location". 手机没有时间确定/更新“最近的位置”。 Besides now you always call requestLocationUpdates() again and again when you call getLocation() . 除了现在,您总是在调用getLocation()时一次又一次调用requestLocationUpdates() getLocation() Just call it once when you want to start receiving location updates. 当您要开始接收位置更新时,只需调用一次即可。

Just subscribe to receive location updates once and always get the latest location in the callback that happens with a small delay. 只需订阅一次即可接收位置更新,并始终在回调中获得延迟很小的最新位置。 You already have the empty onLocationChanged() method. 您已经有空的onLocationChanged()方法。 That is the callback method. 那就是回调方法。 Just make that to handle the location updates. 只需使其处理位置更新即可。

You can of course call removeUpdates() or removeLocationUpdates() once you have received the fresh and up-to-date location in the onLocationChanged() callback if you don't want to receive any further updates. 当然,如果您不想接收任何进一步的更新,则可以在onLocationChanged()回调中收到最新的位置之后,当然可以调用removeUpdates()removeLocationUpdates()

Some useful Stackoverflow discussions: 一些有用的Stackoverflow讨论:

How to get location that isn't out of date? 如何获得不过期的位置?

Android: getLastKnownLocation out-of-date - how to force location refresh? Android:getLastKnownLocation已过期-如何强制刷新位置?

Android - Reliably getting the current location Android-可靠地获取当前位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM