gpt4 book ai didi

android - 当我们在室内使用安卓手机时,如何快速获取经纬度或gps数据?

转载 作者:太空狗 更新时间:2023-10-29 14:29:40 25 4
gpt4 key购买 nike

我正在尝试通过 GPS 从 Android 手机获取纬度和经度信息,当我在室外或直接在天空下时,我能够立即获取值,但当我在室内或房间内时获取这些值需要一分钟多的时间。当我在房间内使用我的应用程序时,任何人都可以帮助我快速获取这些值。

我使用以下代码获取值:

LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {

EditText myLocationText = (EditText)findViewById(R.id.editText1);
EditText myLocationText1 = (EditText)findViewById(R.id.editText2);
String latString = "";
String LongString = "";

if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latString = "" + lat;
LongString ="" + lng;



} else {
latString = "No location found";
LongString = "No location found";
}
myLocationText.setText(""+ latString);
myLocationText1.setText(""+ LongString);
}

除了使用 LocationManager 之外,还有其他方法可以获取 GPS 值吗??

最佳答案

可以得到最后已知的位置,而且速度还挺快的:

/**
* Gets the last known location.
*
* @param locationManager the location manager
* @return the last known location
*/
public static Location getLastKnownLocation(LocationManager locationManager)
{
Location bestResult = null;
float bestAccuracy = 10000;
long bestTime = 0;

List<String> matchingProviders = locationManager.getAllProviders();

for (String provider: matchingProviders) {
Log.d("LOCATION", "Provider: " + provider);
Location location = locationManager.getLastKnownLocation(provider);
Log.d("LOCATION", "Location found? "+ (location==null?"NO":"YES"));
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
Log.d("LOCATION", "Acc: "+ String.valueOf(accuracy) + " -- Time: " + String.valueOf(time));
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
Log.d("LOCATION", "BEST FOUND? "+ (bestResult==null?"NO":"YES"));

return bestResult;
}

关于android - 当我们在室内使用安卓手机时,如何快速获取经纬度或gps数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8297051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com