В вашей деятельности
public final static String MODE_DRIVING = "driving";
public final static String MODE_TRANSIT = "transit";
public final static String MODE_CYCLING = "cycling";
public final static String MODE_WALKING = "walking";
private ArrayList<SearchItem> directionSteps;
private ArrayList <LatLng> directionPoints;
...
switch(checkedId) {
case R.id.btnDriving:
sMode = GMapV3Direction.MODE_DRIVING;
break;
case R.id.btnTransit:
sMode = GMapV3Direction.MODE_TRANSIT;
break;
case R.id.btnCycling:
sMode = GMapV3Direction.MODE_CYCLING;
break;
case R.id.btnWalking:
sMode = GMapV3Direction.MODE_WALKING;
break;
}
findDirectionsAddress(startAddress, destAddress, sMode );
...
@SuppressWarnings("unchecked")
public void findDirectionsAddress(String fromPosition, String toPosition, String mode)
{
Map<String, String> map = new HashMap<String, String>();
map.put(GetDirectionsAsyncTask3.USER_CURRENT_ADDRESS, String.valueOf(fromPosition));
map.put(GetDirectionsAsyncTask3.DESTINATION_ADDRESS, String.valueOf(toPosition));
map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);
map.put(GetDirectionsAsyncTask.DIRECTIONS_LANGUAGE, (String) getResources().getText(R.string.language));
GetDirectionsAsyncTask3 asyncTask = new GetDirectionsAsyncTask3(this);
asyncTask.execute(map);
GetDirectionsAsyncTask4 asyncTask2 = new GetDirectionsAsyncTask4(this);
asyncTask2.execute(map);
}
// Handle the turn by turn text directions
public void handleTurnByTurnResult(ArrayList<SearchItem> directionSteps)
{
this.directionSteps = directionSteps;
Log.d("Test", "Steps:" + directionSteps.size());
adapter = new PickListAdapter(this, this.directionSteps);
setListAdapter(adapter);
}
// Handle the lat/lons of each step
public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints)
{
this.directionPoints = directionPoints;
Configuration config = getResources().getConfiguration();
int width = config.screenWidthDp;
int height = config.screenHeightDp;
if (calcPolyline != null) {
calcPolyline.remove();
}
LatLngBounds.Builder bounds = null;
bounds = new LatLngBounds.Builder();
boolean foundOne = false;
bounds.include(currentLatLng);
if ((theTrip.getStartAddress().getLatitude() != 0.0) && (theTrip.getStartAddress().getLongitude() !=0.0)) {
bounds.include(new LatLng(theTrip.getStartAddress().getLatitude(), theTrip.getStartAddress().getLongitude()));
}
if ((theTrip.getDestinationAddress().getLatitude() != 0.0) && (theTrip.getDestinationAddress().getLongitude() !=0.0)) {
bounds.include(new LatLng(theTrip.getDestinationAddress().getLatitude(), theTrip.getDestinationAddress().getLongitude()));
}
PolylineOptions rectLine = new PolylineOptions().width(15).color(Color.BLUE);
for(int i = 0 ; i < directionPoints.size() ; i++)
{
rectLine.add((LatLng) directionPoints.get(i));
bounds.include((LatLng) directionPoints.get(i));
foundOne = true;
}
for (SegmentItem item : theTrip.getPathSegments()) {
pathDynamic = new PolylineOptions().width(20).color(Color.RED);
pathDynamic.addAll(decode(item.getEncodedPath()));
for (int i = 0; i < pathDynamic.getPoints().size(); i++) {
LatLng point = pathDynamic.getPoints().get(i);
bounds.include(point);
}
}
if (mMap != null) {
calcPolyline = mMap.addPolyline(rectLine);
if (foundOne == true) {
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), width, height, 40));
}
}
}
GMapV3Direction.java
package org.myapp;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.venturacounty.hsamileage.model.SearchItem;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
public class GMapV3Direction {
public final static String MODE_DRIVING = "driving";
public final static String MODE_TRANSIT = "transit";
public final static String MODE_CYCLING = "cycling";
public final static String MODE_WALKING = "walking";
public GMapV3Direction() { }
public Document getDocument(String start, String dest, String mode, String language) {
try {
start = URLEncoder.encode(start, "utf-8");
dest = URLEncoder.encode(dest, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
long milliseconds = System.currentTimeMillis();
long seconds = milliseconds/1000;
String url = "https://maps.googleapis.com/maps/api/directions/xml?departure_time="
+ seconds
+ "&origin=" + start
+ "&destination=" + dest
+ "&language=" + language
+ "&sensor=false&mode=" + mode;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getDurationText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DurationText", node2.getTextContent());
return node2.getTextContent();
}
public int getDurationValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("duration");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DurationValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getDistanceText (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "text"));
Log.i("DistanceText", node2.getTextContent());
return node2.getTextContent();
}
public int getDistanceValue (Document doc) {
NodeList nl1 = doc.getElementsByTagName("distance");
Node node1 = nl1.item(0);
NodeList nl2 = node1.getChildNodes();
Node node2 = nl2.item(getNodeIndex(nl2, "value"));
Log.i("DistanceValue", node2.getTextContent());
return Integer.parseInt(node2.getTextContent());
}
public String getStartAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("start_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getEndAddress (Document doc) {
NodeList nl1 = doc.getElementsByTagName("end_address");
Node node1 = nl1.item(0);
Log.i("StartAddress", node1.getTextContent());
return node1.getTextContent();
}
public String getCopyRights (Document doc) {
NodeList nl1 = doc.getElementsByTagName("copyrights");
Node node1 = nl1.item(0);
Log.i("CopyRights", node1.getTextContent());
return node1.getTextContent();
}
public ArrayList<SearchItem> getTurnByTurn (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<SearchItem> listDirections = new ArrayList<SearchItem>();
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node distanceNode = nl2.item(getNodeIndex(nl2, "distance"));
nl3 = distanceNode.getChildNodes();
Node textNode = nl3.item(getNodeIndex(nl3, "text"));
String distance = textNode.getTextContent();
Node durationNode = nl2.item(getNodeIndex(nl2, "duration"));
nl3 = durationNode.getChildNodes();
textNode = nl3.item(getNodeIndex(nl3, "text"));
String duration = textNode.getTextContent();
Node instructionsNode = nl2.item(getNodeIndex(nl2, "html_instructions"));
String instructions = instructionsNode.getTextContent();
String details = distance + " -- " + duration;
listDirections.add(new SearchItem(instructions, details, "", false));
}
}
return listDirections;
}
public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
LatLng item = (LatLng) arr.get(j);
listGeopoints.add(new LatLng(item.latitude, item.longitude));
}
locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
}
}
return listGeopoints;
}
private int getNodeIndex(NodeList nl, String nodename) {
for(int i = 0 ; i < nl.getLength() ; i++) {
if(nl.item(i).getNodeName().equals(nodename))
return i;
}
return -1;
}
private ArrayList<LatLng> decodePoly(String encoded) {
ArrayList<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.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 = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
poly.add(position);
}
return poly;
}
}
GetDirectionAsyncTask.java
package org.myapp;
import java.util.ArrayList;
import java.util.Map;
import org.myapp.model.SearchItem;
import org.w3c.dom.Document;
import com.google.android.gms.maps.model.LatLng;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Toast;
public class GetDirectionsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<SearchItem>>
{
public static final String USER_CURRENT_LAT = "user_current_lat";
public static final String USER_CURRENT_LONG = "user_current_long";
public static final String DESTINATION_LAT = "destination_lat";
public static final String DESTINATION_LONG = "destination_long";
public static final String DIRECTIONS_MODE = "directions_mode";
public static final String DIRECTIONS_LANGUAGE = "directions_language";
private PickerActivity activity;
private Exception exception;
private ProgressDialog progressDialog;
public GetDirectionsAsyncTask(PickerActivity pickerActivity)
{
super();
this.activity = pickerActivity;
}
public void onPreExecute()
{
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle((String) this.activity.getResources().getText(R.string.directions));
progressDialog.setMessage((String) this.activity.getResources().getText(R.string.calculating_directions));
progressDialog.show();
}
public void onPostExecute(ArrayList<SearchItem> result)
{
progressDialog.dismiss();
if (exception == null)
{
// activity.handleGetDirectionsResult(result);
activity.handleTurnByTurnResult(result);
}
else
{
processException();
}
}
@Override
protected ArrayList<SearchItem> doInBackground(Map<String, String>... params)
{
Map<String, String> paramMap = params[0];
try
{
LatLng fromPosition = new LatLng(Double.valueOf(paramMap.get(USER_CURRENT_LAT)) , Double.valueOf(paramMap.get(USER_CURRENT_LONG)));
LatLng toPosition = new LatLng(Double.valueOf(paramMap.get(DESTINATION_LAT)) , Double.valueOf(paramMap.get(DESTINATION_LONG)));
String sMode = paramMap.get(DIRECTIONS_MODE);
String sLanguage = paramMap.get(DIRECTIONS_LANGUAGE);
GMapV2Direction md = new GMapV2Direction();
Document doc = md.getDocument(fromPosition, toPosition, sMode, sLanguage);
// ArrayList directionPoints = md.getDirection(doc);
ArrayList<SearchItem> directionSteps = md.getTurnByTurn(doc);
return directionSteps;
}
catch (Exception e)
{
exception = e;
return null;
}
}
private void processException()
{
Toast.makeText(activity, activity.getString(R.string.error_when_retrieving_data), Toast.LENGTH_SHORT).show();
}
}
GetDirectionsAsyncTask3.java
package org.myapp;
import java.util.ArrayList;
import java.util.Map;
import org.myapp.model.SearchItem;
import org.w3c.dom.Document;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class GetDirectionsAsyncTask3 extends AsyncTask<Map<String, String>, Object, ArrayList<SearchItem>>
{
public static final String USER_CURRENT_ADDRESS = "user_current_address";
public static final String DESTINATION_ADDRESS = "destination_address";
public static final String DIRECTIONS_MODE = "directions_mode";
public static final String DIRECTIONS_LANGUAGE = "directions_language";
private PickerActivity activity;
private Exception exception;
public GetDirectionsAsyncTask3(PickerActivity pickerActivity)
{
super();
this.activity = pickerActivity;
}
public void onPreExecute()
{
}
public void onPostExecute(ArrayList<SearchItem> result)
{
if (exception == null)
{
// Task4 handles direction points
// activity.handleGetDirectionsResult(result);
// Task3 handles direction text steps
activity.handleTurnByTurnResult(result);
}
else
{
processException();
}
}
@Override
protected ArrayList<SearchItem> doInBackground(Map<String, String>... params)
{
Map<String, String> paramMap = params[0];
try
{
String fromPosition = paramMap.get(USER_CURRENT_ADDRESS);
String toPosition = paramMap.get(DESTINATION_ADDRESS);
String mode = paramMap.get(DIRECTIONS_MODE);
String sLanguage = paramMap.get(DIRECTIONS_LANGUAGE);
GMapV3Direction md = new GMapV3Direction();
Document doc = md.getDocument(fromPosition, toPosition, mode, sLanguage);
// Task4 handles direction points
// ArrayList directionPoints = md.getDirection(doc);
// Task3 handles direction steps
ArrayList<SearchItem> directionSteps = md.getTurnByTurn(doc);
return directionSteps;
}
catch (Exception e)
{
exception = e;
return null;
}
}
private void processException()
{
Toast.makeText(activity, activity.getString(R.string.error_when_retrieving_data), Toast.LENGTH_SHORT).show();
}
}
GetDirectionsAsyncTask4.java
package org.myapp;
import java.util.ArrayList;
import java.util.Map;
import org.w3c.dom.Document;
import com.google.android.gms.maps.model.LatLng;
import android.os.AsyncTask;
import android.widget.Toast;
public class GetDirectionsAsyncTask4 extends AsyncTask<Map<String, String>, Object, ArrayList<LatLng>>
{
public static final String USER_CURRENT_ADDRESS = "user_current_address";
public static final String DESTINATION_ADDRESS = "destination_address";
public static final String DIRECTIONS_MODE = "directions_mode";
public static final String DIRECTIONS_LANGUAGE = "directions_language";
private PickerActivity activity;
private Exception exception;
public GetDirectionsAsyncTask4(PickerActivity pickerActivity)
{
super();
this.activity = pickerActivity;
}
public void onPreExecute()
{
}
public void onPostExecute(ArrayList<LatLng> result)
{
if (exception == null)
{
// Task4 handles the directions points
activity.handleGetDirectionsResult(result);
// Task3 handles the directions text results
// activity.handleTurnByTurnResult(result);
}
else
{
processException();
}
}
@Override
protected ArrayList<LatLng> doInBackground(Map<String, String>... params)
{
Map<String, String> paramMap = params[0];
try
{
String fromPosition = paramMap.get(USER_CURRENT_ADDRESS);
String toPosition = paramMap.get(DESTINATION_ADDRESS);
String mode = paramMap.get(DIRECTIONS_MODE);
String sLanguage = paramMap.get(DIRECTIONS_LANGUAGE);
GMapV3Direction md = new GMapV3Direction();
Document doc = md.getDocument(fromPosition, toPosition, mode, sLanguage);
// Task4 handles the directions points
ArrayList<LatLng> directionPoints = md.getDirection(doc);
// Task3 handles the directions text results
// ArrayList directionSteps = md.getTurnByTurn(doc);
return directionPoints;
}
catch (Exception e)
{
exception = e;
return null;
}
}
private void processException()
{
Toast.makeText(activity, activity.getString(R.string.error_when_retrieving_data), Toast.LENGTH_SHORT).show();
}
}