gpt4 book ai didi

java - 希望将坐标列表导出到 gpx 文件

转载 作者:行者123 更新时间:2023-12-02 11:16:52 24 4
gpt4 key购买 nike

我有一个需要构建的 Android 程序,它将一些 GPS 坐标导出到 gpx 文件。这是我到目前为止的代码。我正在寻找从位置管理器存储 GPS 坐标然后传递到 gpx 文件的最佳方法。

主 Activity.java

public class MainActivity extends AppCompatActivity {
LocationListener locationListener;
LocationManager locationManager;

public void StartRec(View view, int requestCode, String[] permissions, int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 5000, 0, (LocationListener) this);
}

}

LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};

}

public void stopRec(View view) {
Intent intent = new Intent(this, Statistics.class);
startActivity(intent);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

最佳答案

您可以使用 Location 的列表类如下:

public static void generateGfx(File file, String name, List<Location> points) {

String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"MapSource 6.15.5\" version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\"><trk>\n";
name = "<name>" + name + "</name><trkseg>\n";

String segments = "";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
for (Location location : points) {
segments += "<trkpt lat=\"" + location.getLatitude() + "\" lon=\"" + location.getLongitude() + "\"><time>" + df.format(new Date(location.getTime())) + "</time></trkpt>\n";
}

String footer = "</trkseg></trk></gpx>";

try {
FileWriter writer = new FileWriter(file, false);
writer.append(header);
writer.append(name);
writer.append(segments);
writer.append(footer);
writer.flush();
writer.close();

} catch (IOException e) {
Log.e("generateGfx", "Error Writting Path",e);
}}

You can refer this

关于java - 希望将坐标列表导出到 gpx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50205890/

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