从OSM XML文件中检索XML属性|| Android的

时间:2015-03-09 19:35:56

标签: java android xml openstreetmap overpass-api

我尝试使用OSM / Overpass检索速度限制。 下面的XML在名为" interpreter"的文件中返回。查询Overpass API后。 我找不到" maxspeed"或" 60"使用Android从此xml标记。

任何人都可以提供任何建议或指出我正确的方向,因为我找不到任何教程。

运行文件时没有错误。

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>

爪哇:

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


}

0 个答案:

没有答案