Recently I've been trying this example code I found which helps me to apply in my android application. This is the link to that example code -> http://mrbool.com/google-directions-api-tracing-routes-in-android/32001
However, in that I'm getting an Error in the nodeListStep = elementLeg.getElementsByTagName("step"); and
decodePolylines(elementStep.getElementsByTagName("points").item(0).getTextContent()); saying it cannot resolved method. Is there anything I could do to solve this problem?
This is the 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();
}
public RotaTask() {
super();
}
/*** * {@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.getElementsByTagName("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);
}
}
}
this is my activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="10dp" >
<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>
It looks like you just need to add all the necessary imports.
Here are all of the imports I used to get it to compile successfully: