gpt4 book ai didi

java - 每次调用函数时如何获取当前 gps 位置?

转载 作者:行者123 更新时间:2023-11-29 23:25:39 25 4
gpt4 key购买 nike

我想做的是每次调用函数时获取位置经纬度。据我所知,最好的方法是将位置更新保留几秒钟以获取正确的修复,然后将其禁用,但我无法在我的应用程序中使用它。

到目前为止,我一直设法在每次调用 displayData 函数时获取手机的最后已知位置,但我无法克服尝试更改为时出现的所有错误请求位置更新。我在这里所做的就是调用 displayData 函数,当有来自蓝牙设备的传入数据时,以获取位置并将数据 + 位置写入文件。

谁能帮助我,因为所有指南都展示了如何在位置更新时触发某些东西,但我不想那样做。 我只是想要定期获得正确的位置...

private void displayData(final byte[] byteArray) {
try {

mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.

if (byteArray != null) {
String data = new String(byteArray);
tv.setText(n/2 + " measurements since startup...");
n += 1;

if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(lng);
}

try
{
FileWriter fw = new FileWriter(textfile,true); //the true will append the new data
if (writeDate()) {
fw.write("\n");
fw.write(stringDate);
fw.write(data); //appends the string to the file
}
else {
fw.write(data); //appends the string to the file
fw.write(" - ");
fw.write(latitude);
fw.write(",");
fw.write(longitude);
}
fw.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}


// find the amount we need to scroll. This works by
// asking the TextView's internal layout for the position
// of the final line and then subtracting the TextView's height
final int scrollAmount = tv.getLayout().getLineTop(
tv.getLineCount())
- tv.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if (scrollAmount > 0)
tv.scrollTo(0, scrollAmount);
else
tv.scrollTo(0, 0);
}
}
});

} catch (SecurityException e) {
// lets the user know there is a problem with the gps
}
}

最佳答案

这是我对您的问题的理解:

  • 您希望按需获取 GPS 位置,但是:
  • 您不希望 GPS 一直运行,并且:
  • 您接受 GPS 必须在短时间内运行

尽量不要考虑“返回手机当前位置的函数”,因为这意味着它是一个简单的同步操作,可以在不阻塞的情况下提供答案。我们不能在这里这样做。

相反,我建议您将其更多地视为 FSM ,因为在调用 displayData() 和开始获取实时 GPS 定位之间需要任意时间(可能是几秒,也可能更长)。换句话说,displayData() 不会直接产生位置;它将启动一系列事件,最终导致您获得位置。

您必须 promise 使用 requestLocationUpdates()(或类似方法):

private void displayData(final byte[] byteArray) {
//This call turns the GPS on, and returns immediately:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

//This function gets called some time after displayData() returns (possibly
//*way* after). It executes on the UI thread.
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this); //Shut down the GPS

//(Execute the remainder of your onSuccess() logic here.)

//Now our state machine is complete, and everything is cleaned up.
//We are ready for the next call to displayData().
}

public void onStatusChanged(String provider, int status, Bundle extras) {}

public void onProviderEnabled(String provider) {}

public void onProviderDisabled(String provider) {}
);
}

这样,

  • 我们不阻塞 UI 线程(displayData() 立即返回)
  • 状态机最终会收敛到一个答案(前提是 GPS 可以正常工作)
  • 一旦我们获得所需信息,GPS 就会关闭

您可能需要考虑对该方案进行一些改进:

  • 如果在上一个请求已解决之前调用 displayData(),则避免重复调用 requestLocationUpdates() 的方法
  • 处理 onSuccess() 方法中引用的 UI 元素不再可用的情况 b/c Activity 在此期间有 onDestroy()'d GPS 请求“正在进行中”
  • 如果需要清理等,取消正在进行的请求。

关于java - 每次调用函数时如何获取当前 gps 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614643/

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