0

我正在尝试在到位置之间绘制一条路径,但在解析 KML 时出现错误。执行在这里停止:

InputSource inputSource = new InputSource(url.openStream());

不执行以下行,

reader.parse(inputSource);

private void drawPath(GeoPoint src, GeoPoint dest, MapView mapView) {

    String strUrl = "http://maps.google.com/maps?";
    //From
    strUrl += "saddr=" +
           (src.getLatitudeE6()/1.0E6) + 
           "," +
           (src.getLongitudeE6()/1.0E6);
    //To
    strUrl += "&daddr=" +
           (dest.getLatitudeE6()/1.0E6) + 
           "," + 
           (dest.getLongitudeE6()/1.0E6);
    //Walk attribute (for walk path)
    strUrl += "&dirflg=w";
    //File format
    strUrl += "&output=kml";

    try {
        //Parse KML
        URL url = new URL(strUrl.toString());
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        KMLHandler kmlHandler = new KMLHandler();
        reader.setContentHandler(kmlHandler);
        InputSource inputSource = new InputSource(url.openStream());
        reader.parse(inputSource);

        String path = kmlHandler.getPathCoordinates();
        //Draw path
        if(path != null) {
            RouteOverlay routeOverlay = new RouteOverlay();
            String pairs[] = path.split(" ");
            for (String pair : pairs) {
                String coordinates[] = pair.split(",");
                GeoPoint geoPoint = new GeoPoint(
                        (int) (Double.parseDouble(coordinates[1]) * 1E6),
                        (int) (Double.parseDouble(coordinates[0]) * 1E6));
                routeOverlay.addGeoPoint(geoPoint);
            }

            mapView.getOverlays().add(routeOverlay);
        }
    } catch (Exception e) {
        Log.w("RoutePath", e.toString());
    }
}
4

0 回答 0