Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

java - Retrieving XML Attributes from OSM XML File || Android

I'm trying to retrieve speed limits using OSM/Overpass. The XML below is returned in a file called "interpreter" after querying the Overpass API. I cannot retrieve the "maxspeed" or "60" tag from this xml using Android.

Could anyone offer any advice or point me in the right direction as I can't find any tutorials.

There are no errors when the file is run.

XML:

 <?xml version="1.0" encoding="UTF-8"?>
    <osm version="0.6" generator="Overpass API">

    <node id="768053039" lat="54.9526671" lon="-7.7273348"/>
  <node id="768053040" lat="54.9498094" lon="-7.7176056"/>
  <node id="768053041" lat="54.9497066" lon="-7.7173174"/>
  <node id="768053043" lat="54.9495658" lon="-7.7170937"/>
  <node id="768053044" lat="54.9495035" lon="-7.7169816"/>
  <node id="791492493" lat="54.9494183" lon="-7.7168205"/>
  <node id="795319854" lat="54.9510427" lon="-7.7218262"/>
  <node id="795320324" lat="54.9509153" lon="-7.7213706"/>
  <node id="1922546572" lat="54.9502165" lon="-7.7190169"/>
  <node id="1922546679" lat="54.9504739" lon="-7.7199078"/>
  <node id="1922546692" lat="54.9500860" lon="-7.7185174"/>
  <node id="1922602861" lat="54.9517250" lon="-7.7241644"/>
  <node id="1922622063" lat="54.9514357" lon="-7.7231690"/>
  <node id="2673934802" lat="54.9498543" lon="-7.7177617"/>
  <way id="64273241">
    <nd ref="768053039"/>
    <nd ref="1922602861"/>
    <nd ref="1922622063"/>
    <nd ref="795319854"/>
    <nd ref="795320324"/>
    <tag k="highway" v="secondary"/>
    <tag k="maxspeed" v="60"/>
    <tag k="name" v="Port Road"/>
    <tag k="oneway" v="no"/>
    <tag k="ref" v="R229"/>
  </way>
  <way id="64887990">
    <nd ref="795320324"/>
    <nd ref="1922546679"/>
    <nd ref="1922546572"/>
    <nd ref="1922546692"/>
    <nd ref="2673934802"/>
    <nd ref="768053040"/>
    <nd ref="768053041"/>
    <nd ref="768053043"/>
    <nd ref="768053044"/>
    <nd ref="791492493"/>
    <tag k="highway" v="secondary"/>
    <tag k="maxspeed" v="60"/>
    <tag k="name" v="Port Road"/>
    <tag k="oneway" v="no"/>
    <tag k="ref" v="R229"/>
  </way>

</osm>

Java:

//Location listener
class mylocationListener implements LocationListener
{

    @Override
    public void onLocationChanged(Location location) {
        if(location != null)
        {
            //Get position
            double lon = location.getLongitude();
            double lat = location.getLatitude();
            txtLong.setText("Longitude: " +lon);
            txtLat.setText("Latitude: " + lat);

            //Get speed
            double speed = location.getSpeed();
            txtSpeed.setText(speed + " km/h");

            //Send request to recieve information regarding Speed zones everytime there is a change in location
            //sendRequest(lat+"", lon+"");
            //sendGetRequest(lat+"", lon+"");
            try {
                readXML2(lat+"", lon+"");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }




public void readXML2(String lat, String lon) throws IOException, ParserConfigurationException, SAXException
{

    //InputStream input = getDataViaOverpass(query2);

    try {
        String query = "http://overpass-api.de/api/interpreter?data=way(around:40,"
                +lon+","+lat+")" + " ["highway"] ["maxspeed"]; ( ._; >; ); out;";

        String query2 = "?data=way(around:40,"
                +lon+","+lat+")" + " ["highway"] ["maxspeed"]; ( ._; >; ); out;";

        String hostname = OVERPASS_API;

        URL osm = new URL(hostname);
        HttpURLConnection connection = (HttpURLConnection) osm.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        //connection.setReadTimeout(2*60*1000);//2 minute timeout

        DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
        printout.writeBytes("data=" + URLEncoder.encode(query, "utf-8"));
        printout.flush();
        printout.close();

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document;
        document = documentBuilder.parse(connection.getInputStream());

        Element rootElement = document.getDocumentElement();

        NodeList nodeNodeList = rootElement.getElementsByTagName("way");

        for (int i = 0; i < nodeNodeList.getLength(); i++) {

            Node nNode = nodeNodeList.item(i);

            String limit = nNode.getAttributes().getNamedItem("maxspeed").getNodeValue();
            TextView curLimitText = (TextView)findViewById(R.id.curLimitTxt);
            curLimitText.setText(limit +" km/h");


            System.out.println(nNode.getAttributes().getNamedItem("maxspeed").getNodeValue());
            System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());

        }

    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...