Back to project page Google-Directions-Android.
The source code is released under:
Copyright (c) 2013 Joel Dean Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software...
If you think the Android project Google-Directions-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.directions.route; /*w w w .j ava 2s . c o m*/ /** * Async Task to access the Google Direction API and return the routing data * which is then parsed and converting to a route overlay using some classes created by Hesham Saeed. * @author Joel Dean * Requires an instance of the map activity and the application's current context for the progress dialog. * */ import android.os.AsyncTask; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.PolylineOptions; import java.util.ArrayList; public class Routing extends AsyncTask<LatLng, Void, Route> { protected ArrayList<RoutingListener> _aListeners; protected TravelMode _mTravelMode; public enum TravelMode { BIKING("biking"), DRIVING("driving"), WALKING("walking"), TRANSIT("transit"); protected String _sValue; private TravelMode(String sValue) { this._sValue = sValue; } protected String getValue() { return _sValue; } } public Routing(TravelMode mTravelMode) { this._aListeners = new ArrayList<RoutingListener>(); this._mTravelMode = mTravelMode; } public void registerListener(RoutingListener mListener) { _aListeners.add(mListener); } protected void dispatchOnStart() { for (RoutingListener mListener : _aListeners) { mListener.onRoutingStart(); } } protected void dispatchOnFailure() { for (RoutingListener mListener : _aListeners) { mListener.onRoutingFailure(); } } protected void dispatchOnSuccess(PolylineOptions mOptions, Route route) { for (RoutingListener mListener : _aListeners) { mListener.onRoutingSuccess(mOptions, route); } } /** * Performs the call to the google maps API to acquire routing data and * deserializes it to a format the map can display. * * @param aPoints * @return */ @Override protected Route doInBackground(LatLng... aPoints) { for (LatLng mPoint : aPoints) { if (mPoint == null) return null; } return new GoogleParser(constructURL(aPoints)).parse(); } protected String constructURL(LatLng... points) { LatLng start = points[0]; LatLng dest = points[1]; String sJsonURL = "http://maps.googleapis.com/maps/api/directions/json?"; final StringBuffer mBuf = new StringBuffer(sJsonURL); mBuf.append("origin="); mBuf.append(start.latitude); mBuf.append(','); mBuf.append(start.longitude); mBuf.append("&destination="); mBuf.append(dest.latitude); mBuf.append(','); mBuf.append(dest.longitude); mBuf.append("&sensor=true&mode="); mBuf.append(_mTravelMode.getValue()); return mBuf.toString(); } @Override protected void onPreExecute() { dispatchOnStart(); } @Override protected void onPostExecute(Route result) { if (result == null) { dispatchOnFailure(); } else { PolylineOptions mOptions = new PolylineOptions(); for (LatLng point : result.getPoints()) { mOptions.add(point); } dispatchOnSuccess(mOptions, result); } }//end onPostExecute method }