Java 如何在android中获得准确的当前位置?

Java 如何在android中获得准确的当前位置?,java,android,gps,latitude-longitude,Java,Android,Gps,Latitude Longitude,我尝试了getLastKnownLocation(),但我发现它有时无法提供准确的位置,而且它获取了错误的位置,否则就很好了 有人能帮我吗? 我是android studio的初学者 下面是我的GPS跟踪器代码 if (isGPSEnabled) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,

我尝试了
getLastKnownLocation()
,但我发现它有时无法提供准确的位置,而且它获取了错误的位置,否则就很好了

有人能帮我吗? 我是android studio的初学者

下面是我的GPS跟踪器代码

if (isGPSEnabled)
{
    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);
        updateGPSCoordinates();
    }
}
} else 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);
    updateGPSCoordinates();
}
}

// ...

@Override
public void onLocationChanged(Location location) 
{ 
this.location = location;
updateGPSCoordinates();
}

关于Android开发者有一个非常有用的页面,我希望这会对你有所帮助。出于我的目的,我不得不稍微调整一下,但主要思想保持不变:

您可能认为最新的位置修复是最新的 精确的但是,由于定位精度不同,因此 最近的修复并不总是最好的。你应该包括逻辑 基于多个标准选择位置修复。标准也 根据应用程序和字段的用例而有所不同 测试

private static final long TWO_MINUTES=TimeUnit.MINUTES.toNanos(2);
/**确定一个位置读取是否优于当前位置修复
*@param location要计算的新位置
*@param currentBestLocation当前位置修复程序,您要将新位置修复程序与之进行比较
*/
受保护的布尔值isBetterLocation(位置位置,位置currentBestLocation){
如果(currentBestLocation==null){
//新位置总比没有位置好
返回true;
}
//检查新的位置修复程序是较新的还是较旧的
long-timeDelta=location.getelapsedrealtimenos()-currentBestLocation.getelapsedrealtimenos();
布尔值isSignificantlyNewer=时间增量>两分钟;
布尔值Issignificantlolder=时间增量<-2_分钟;
布尔值isNewer=timeDelta>0;
//如果距离当前位置已超过两分钟,请使用新位置
//因为用户可能已经移动了
如果(非常重要){
返回true;
//如果新位置比原来的位置早两分钟以上,情况肯定会更糟
}else if(Issignificantlolder){
返回false;
}
//检查新的位置修复是否更精确
int accuracyDelta=(int)(location.getAccuracy()-currentBestLocation.getAccuracy());
布尔值IslesAccurate=accuracyDelta>0;
布尔值IsmorePrecision=精度偏差<0;
布尔值不太精确=精度偏差>200;
//检查新旧位置是否来自同一提供商
布尔值isFromSameProvider=isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
//结合及时性和准确性确定定位质量
如果(更准确){
返回true;
}else if(isNewer&!islesAccurate){
返回true;
}else if(较新且不太准确且来自SameProvider){
返回true;
}
返回false;
}
/**检查两个提供程序是否相同*/
专用布尔值isSameProvider(字符串提供程序1、字符串提供程序2){
if(provider1==null){
返回provider2==null;
}
返回provider1.equals(provider2);
}

您所说的“错误位置”是什么意思?GPS不是100%准确,并且有一些误差。GPS是室内还是室外也很重要。这几周我做了很多测试,我注意到精度很大程度上取决于手机的品牌。有些人比其他人更擅长制造GPS芯片。。。另外,这里提到的棒棒糖操作系统也存在一些问题:错误的位置意味着有时它会显示以前获取的位置,而不是更新的位置。你有没有办法得到更新的位置(不是最新的位置)。@TDG错误的位置意味着它显示了不同的州地址,我正在使用一些应用程序,它们至少对城市是准确的。
private static final long TWO_MINUTES = TimeUnit.MINUTES.toNanos(2);

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getElapsedRealtimeNanos() - currentBestLocation.getElapsedRealtimeNanos();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
}