Retrieving XML Attributes from OSM XML File || Android

别说谁变了你拦得住时间么 提交于 2019-12-25 03:44:06

问题


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();
    }


}

来源:https://stackoverflow.com/questions/28950359/retrieving-xml-attributes-from-osm-xml-file-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!