Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android上通过解析xml到GoogleMap绘制路线_Android_Xml_Google Maps - Fatal编程技术网

如何在android上通过解析xml到GoogleMap绘制路线

如何在android上通过解析xml到GoogleMap绘制路线,android,xml,google-maps,Android,Xml,Google Maps,如何解析URL中包含纬度和经度的XML文件,我将使用此坐标绘制路线 这是xml文件 <tgt_api> <tgt_trip> <id>1</id> <tgt_tourist> <id>1</id> <firstname>tourist1</firstname> <lastname>touristlname</lastname>

如何解析URL中包含纬度和经度的XML文件,我将使用此坐标绘制路线

这是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>

1.
1.
旅游者1
旅游者姓名
123fjkl34552
美国
08912345678
拉卡班
13.72331
100.78453999999999
13.692941
100.750723
13.71347
100.75589000000002
13.71329
100.75553000000002
13.70851
100.77463999999998
13.7218
100.77636000000007
13.72206
100.78467
13.72284
100.78467
13.72331
100.78453999999999

单击按钮时,将转到此方法

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

    }
private void parseXML(){
内容=”http://10.0.2.2/AirportWebApplication/tgt_api.php?trip_id=1";
试一试{
/*创建要从中加载一些xml数据的URL*/
URL=新的URL(内容);
InputStream in=url.openStream();
InputSource is=新的InputSource(in);
/*从SAXPArserFactory获取SAXParser*/
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
/*获取我们创建的SAXParser的XMLReader*/
XMLReader xr=sp.getXMLReader();
/*创建新的ContentHandler并将其应用于XML读取器*/
handler=新的XMLHandler();
xr.setContentHandler(handler);
/*从我们的URL解析xml数据*/
xr.parse(is);
/*解析已完成*/
/*我们的ExampleHandler现在向我们提供解析的数据*/
maplist=handler.getParsedData();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
专用无效路由映射(阵列列表点){

对于(int i=0;i)抛出异常的确切位置是(行号)?它说“at.com.TouristNavigation.XMLHandler.endElement(XMLHandler.java line 79)”,您能在编辑器中找到该行并识别它吗?实际上无法从上面的列表中计算行数。在这一部分>>映射点i=new映射点(Double.parseDouble(lation),Double.parseDouble(longitude));是否设置了断点并查看了latitude和longitude的内容。错误基本上是说其中一个字符串无法解析为Double
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);

        }

    }

}