开发者

Android parse KML file for time

开发者 https://www.devze.com 2023-02-27 21:59 出处:网络
I\'ve been trying to work out how to obtain the travel time between two locations (walking, driving etc...).

I've been trying to work out how to obtain the travel time between two locations (walking, driving etc...).

As I understand it, the only way to do this accurately is by retrieving a KML file from google, then parsing it.

Research has shown t开发者_StackOverflow中文版hat it then needs to be parsed with SAX. The problem is, I can't seem to work out how to extract the correct variables (the time). Does anybody know if / how this can be done?

Many thanks for your help,

Pete.


Parsing XML (what KML basically is), using a SAX-Parser: http://www.dreamincode.net/forums/blog/324/entry-2683-parsing-xml-in-java-part-1-sax/

<kml>
  <Document>
    <Placemark>
      <name>Route</name>
      <description>Distance: 1.4&#160;mi (about 30 mins)<br/>Map data &#169;2011 Tele Atlas </description>
    </Placemark>
  </Document>
</kml>

In the example you can see, that the guessed time is stored in the "description"-Tag. It's saved in the last "Placemark"-Tag in the KML-File and it has a "<name>Route</name>"-Tag.

Getting this Tag with the SAX-Parser and extracting the time using regex should be easy done.


Here's my JSOUP implementation for getting tracks

public ArrayList<ArrayList<LatLng>> getAllTracks() {
    ArrayList<ArrayList<LatLng>> allTracks = new ArrayList<ArrayList<LatLng>>();

    try {
        StringBuilder buf = new StringBuilder();
        InputStream json = MyApplication.getInstance().getAssets().open("track.kml");
        BufferedReader in = new BufferedReader(new InputStreamReader(json));
        String str;

        while ((str = in.readLine()) != null) {
            buf.append(str);
        }

        in.close();
        String html = buf.toString();
        Document doc = Jsoup.parse(html, "", Parser.xmlParser());
        ArrayList<String> tracksString = new ArrayList<String>();
        for (Element e : doc.select("coordinates")) {
            tracksString.add(e.toString().replace("<coordinates>", "").replace("</coordinates>", ""));
        }

        for (int i = 0; i < tracksString.size(); i++) {
            ArrayList<LatLng> oneTrack = new ArrayList<LatLng>();
            ArrayList<String> oneTrackString = new ArrayList<String>(Arrays.asList(tracksString.get(i).split("\\s+")));
            for (int k = 1; k < oneTrackString.size(); k++) {
                LatLng latLng = new LatLng(Double.parseDouble(oneTrackString.get(k).split(",")[0]), Double.parseDouble(oneTrackString.get(k)
                        .split(",")[1]));
                oneTrack.add(latLng);
            }
            allTracks.add(oneTrack);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return allTracks;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消