无法解析方法getElementsByTagName

时间:2015-06-09 18:44:18

标签: android google-maps

我一直试图在android中给出一个给定点之间的路径。但我得到一个错误说无法解析方法getElementsByTagName(java.lang.String)有什么我可以做的摆脱这个吗?

即时收到我的错误

nodeListStep = elementLeg.getElementsByTagName(“step”);

decodePolylines(elementStep.getElementsByTagName( “点”)项目(0).getTextContent());

我试过这个例子 http://mrbool.com/google-directions-api-tracing-routes-in-android/32001

这是我的 RotaTask.java

    public class RotaTask extends AsyncTask<Void, Integer, Boolean> {

    private static final String TOAST_MSG = "Calculating";
    private static final String TOAST_ERR_MAJ = "Impossible to trace Itinerary";
    private Context context; private GoogleMap gMap;
    private String editFrom; private String editTo;
    private final ArrayList<LatLng> lstLatLng = new ArrayList<LatLng>();

    public RotaTask(final Context context, final GoogleMap gMap, final String editFrom, final String editTo) {
        this.context = context;
        this.gMap= gMap;
        this.editFrom = editFrom;
        this.editTo = editTo;
    }

    /** * {@inheritDoc} */
    @Override protected void onPreExecute() {
        Toast.makeText(context, TOAST_MSG, Toast.LENGTH_LONG).show();
    }

    /*** * {@inheritDoc} */

    @Override protected Boolean doInBackground(Void... params) {
        try {
            final StringBuilder url = new StringBuilder("http://maps.googleapis.com/maps/api/directions/xml?sensor=false&language=pt"); url.append("&origin="); url.append(editFrom.replace(' ', '+')); url.append("&destination=");
            url.append(editTo.replace(' ', '+'));
            final InputStream stream = new URL(url.toString()).openStream();
            final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setIgnoringComments(true);
            final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            final Document document = documentBuilder.parse(stream);
            document.getDocumentElement().normalize();
            final String status = document.getElementsByTagName("status").item(0).getTextContent();

            if(!"OK".equals(status)) {
                return false;
            }

            final Element elementLeg = (Element) document.getElementsByTagName("leg").item(0);
            final NodeList nodeListStep;
            nodeListStep = elementLeg.getElementsByTagName("step");
            final int length = nodeListStep.getLength();

            for(int i=0; i<length; i++) {
                final Node nodeStep = nodeListStep.item(i);

                if(nodeStep.getNodeType() == Node.ELEMENT_NODE) {
                    final Element elementStep = (Element) nodeStep;
                    decodePolylines(elementStep.getElementByTagName("points").item(0).getTextContent());
                }
            }

            return true;
        }

        catch(final Exception e) {
            return false;
        }
    }


    private void decodePolylines(final String encodedPoints) {
        int index = 0;
        int lat = 0, lng = 0;

        while (index < encodedPoints.length()) {
            int b, shift = 0, result = 0;

            do {
                b = encodedPoints.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5;
            }

            while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat; shift = 0; result = 0;
            do {
                b = encodedPoints.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5; } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng; lstLatLng.add(new LatLng((double)lat/1E5, (double)lng/1E5));
        }
    }

    /** * {@inheritDoc} */

    @Override
    protected void onPostExecute(final Boolean result) {

        if(!result) {
        Toast.makeText(context, TOAST_ERR_MAJ, Toast.LENGTH_SHORT).show();
    }
        else {
            final PolylineOptions polylines = new PolylineOptions();
            polylines.color(Color.BLUE);

            for(final LatLng latLng : lstLatLng) {
                polylines.add(latLng);
            }

            final MarkerOptions markerA = new MarkerOptions();

            markerA.position(lstLatLng.get(0));
            markerA.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            final MarkerOptions markerB = new MarkerOptions();
            markerB.position(lstLatLng.get(lstLatLng.size()-1));
            markerB.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lstLatLng.get(0), 10)); gMap.addMarker(markerA);
            gMap.addPolyline(polylines); gMap.addMarker(markerB);
        }
    }




}

这是 acitiy_main.xml

<
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal" >

    <TextView
        android:text="From"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editFrom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:inputType="text"
        android:lines="1"
        android:maxLines="1" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="3" android:orientation="horizontal" >

<TextView
    android:text="To"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editTo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:inputType="text"
    android:lines="1"
    android:maxLines="1" />

</LinearLayout>

<Button
android:id="@+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Go" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

你这里有一个错字

它应该是,

getElementsByTagName()

而不是

getElementByTagName()

根据docs

  

Element.getElementsByTagName()方法返回实时   HTMLCollection具有给定标记名称的元素。

答案 1 :(得分:0)

我很抱歉这个死神,但是,我只是在帮助某个人时碰到了这个问题,并希望如果其他人发现这个问题,将来会有所帮助。

确保导入正确的Element类(来自org.w3c.dom)。enter image description here

这是一个如此简单的问题,它已经很久没有解决了。

相关问题