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

时间:2018-05-07 01:02:10

标签: java android

我有一个我需要构建的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);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用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