Как нарисовать маршрут, анализируя XML для Google Map на Android - PullRequest
1 голос
/ 10 июля 2011

Как я могу разобрать файл XML в URL, который содержит широту и долготу, по которым я нарисую маршрут, используя эти координаты

Вот XML-файл

<tgt_api>
  <tgt_trip>
    <id>1</id>
   <tgt_tourist>
    <id>1</id>
    <firstname>tourist1</firstname>
    <lastname>touristlname</lastname>
    <passport_id>123fjkl34552</passport_id>
    <country>United States</country>
    <telephone>08912345678</telephone>
  </tgt_tourist>
  <tgt_taxi>
   <license_id/>
   <firstname/>
   <lastname/>
   <mobile/>
   <license_plate/>
   </tgt_taxi>
  <tgt_destination>
   <place_name>Ladkrabang</place_name>
   <destination_point>
      <lat>13.72331</lat>
      <lng>100.78453999999999</lng>
   </destination_point>
  <routes>
   <point counter="1">
     <lat>13.692941</lat>
     <lng>100.750723</lng>
   </point>
   <point counter="2">
     <lat>13.71347</lat>
     <lng>100.75589000000002</lng>
   </point>
   <point counter="3">
     <lat>13.71329</lat>
     <lng>100.75553000000002</lng>
   </point>
   <point counter="4">
     <lat>13.70851</lat>
     <lng>100.77463999999998</lng>
   </point>
   <point counter="5">
     <lat>13.7218</lat>
     <lng>100.77636000000007</lng>
   </point>
   <point counter="6">
     <lat>13.72206</lat>
     <lng>100.78467</lng>
   </point>
   <point counter="7">
     <lat>13.72284</lat>
     <lng>100.78467</lng>
   </point>
  <point counter="8">
     <lat>13.72331</lat>
     <lng>100.78453999999999</lng>
  </point>
 </routes>
</tgt_destination>


при нажатии на кнопку перейдет к этому методу

    private void parseXML() {
        contents="http://10.0.2.2/AirportWebApplication/tgt_api.php?trip_id=1";
        try {
            /* Create a URL we want to load some xml-data from. */
            URL url = new URL(contents);
            InputStream in = url.openStream();
            InputSource is = new InputSource(in);

            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader */
            handler = new XMLHandler();
            xr.setContentHandler(handler);

            /* Parse the xml-data from our URL. */
            xr.parse(is);
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            maplist = handler.getParsedData();


        } catch (MalformedURLException 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();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

 private void routeMap(ArrayList<MapPoint> points){
        for (int i = 0; i<points.size()-1;i++){
            TGT3.this.drawPath(points.get(i), points.get(i+1),Color.GREEN);
        }
    }

    private void drawPath(MapPoint mapPoint, MapPoint mapPoint2,int color) {
        // TODO Auto-generated method stub
        GeoPoint gp1 = new GeoPoint((int)(mapPoint.getCoor()[0]),(int)(mapPoint.getCoor()[1]));
        GeoPoint gp2 = new GeoPoint((int)(mapPoint2.getCoor()[0]),(int)(mapPoint2.getCoor()[1]));
        mapView.getOverlays().add(new RouteOverlay(gp1,gp2,color)); 

    }

и класс XMLHandler

package com.TouristNavigation;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class XMLHandler extends DefaultHandler {
    private MapPoints MapList = new MapPoints();

    private String latitude, longitude;

    private boolean inRouteTag;
    private boolean inPointTag;
    private boolean inLatTag;
    private boolean inLongTag;

    private int index = 0;

    public ArrayList<MapPoint> getParsedData() {
        // return this.myParsedExampleDataSet;
        return MapList.getAllPoint();
    }

    @Override
    public void startDocument() throws SAXException {
        // this.myParsedExampleDataSet = new ParsedExampleDataSet();
        MapList = new MapPoints();
    }

    @Override
    public void endDocument() throws SAXException {
        // Nothing to do
    }

    /**
     * Gets be called on opening tags like: <tag> Can provide attribute(s), when
     * xml was like: <tag attribute="attributeValue">
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
    {

        if (localName.equals("routes")) {
            inRouteTag = true;
        }
        if (localName.equals("point")) {
            inPointTag = true;
        }
        if (localName.equals("lat")) {
            inLatTag = true;
        }
        if (localName.equals("lng")) {
            inLongTag = true;
        }
    }

    /**
     * Gets be called on closing tags like: </tag>
     */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {

        if (localName.equals("lat")) {
            inLatTag = false;
        }
        if (localName.equals("lng")) {
            inLongTag = false;
        }

        if (localName.equals("point")) {
            //MapPoint i = new MapPoint(Double.parseDouble(latitude),Double.parseDouble(longitude));
            MapPoint i = new MapPoint(Double.parseDouble(latitude),Double.parseDouble(longitude));
            Log.e(latitude+" "+longitude, getClass().getSimpleName());
            MapList.addPoint(i);
            inPointTag = false;
            clearInfo();
        }
    }

    private void clearInfo(){
        latitude = "";
        longitude = "";

    }

    /**
     * Gets be called on the following structure: <tag>characters</tag>
     */
    @Override
    public void characters(char ch[], int start, int length) {
        if(inLatTag){
            latitude = new String(ch, start, length);
            }
        if (inLongTag) {
            longitude = new String(ch, start, length);

        }

    }

}

но после того, как я его протестировал, он получил ошибку в классе XMLhandler ОШИБКА: NUMBERFORMATEXCEPTION что я сделал не так ?, есть какие-то предложения?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...