, I went through HELL and HIGH WATER.. writing this (and I swear CN1 has
parsing, unless ive done something drastically wrong - hopefully our new google lib will help lol???)..
This took 9 hours of PAIN.. I post this hear because this sort of example code is SERIOUSLY LACKING, not only in CN1 but even J2SE.. Enjoy.
private void GETCOORDS(){//final Dialog progress) {
_("GETCOORDS");
try {
ConnectionRequest req = new ConnectionRequest()
{
protected void readResponse(InputStream input) throws IOException
{
JSONParser p = new JSONParser();
Hashtable h = p.parse(new InputStreamReader(input));
// "status" : "REQUEST_DENIED"
String response = (String)h.get("status");
_("//////////////STATUS FROM API: "+response);
if(response.equals("REQUEST_DENIED"))
{
_("Request Denied "+(String)h.get("error_message"));//obtain a key!
return;
}
//PARSE the json of directions data from google, warning this is hideous CN1 has very bad parsing
//
RouteHolder routeHolder = new RouteHolder();
//routeholder
// + LEGS
// + STEPS
final Vector vec = (Vector) h.get("routes");
_("///////////////////////vec size:"+vec.size());
Leg mylegs[] ;//= new Leg[vec.size()];
for (int i = 0; i < vec.size(); i++) {
Hashtable entry = (Hashtable)vec.elementAt(i);
String copyrights = (String) entry.get("copyrights");
_("copyrights:"+copyrights);
routeHolder.copyrights=copyrights;
String summary = (String) entry.get("summary");
_("summary:"+summary);
routeHolder.summary=summary;
//////////////////////////////////////////////////////////
//GET THE LEGS OF THE JOURNEY/////////////////////////////
/////////////////////////////////////////////////////////
Vector legs = (Vector) entry.get("legs");
mylegs = new Leg[ legs.size() ];
//_("legs:"+legs);
for (int j=0; j<legs.size(); j++)
{
Hashtable distance = (Hashtable) legs.get(j);
mylegs[j]=new Leg();
//each step has a number of things it can hold..
_("________________________________________________________");
//DISTANCE////////////////
_("DISTANCE:");
Hashtable distanceHT = (Hashtable) distance.get("distance");
String textDistance= (String)distanceHT.get("text");
_("text:"+textDistance);
Double valueDistance= (Double)distanceHT.get("value");
_("value:"+valueDistance);
Distance dist = new Distance(textDistance,valueDistance);
mylegs[j].distance = dist;
//////////////////////////
//DURATION////////////////
_("DURATION");
Hashtable durationHT = (Hashtable) distance.get("duration");
String textDuration= (String)durationHT.get("text");
_("text:"+textDuration);
Double valueDuration= (Double)durationHT.get("value");
_("value:"+valueDuration);
Duration dura = new Duration(textDuration,valueDuration);
mylegs[j].duration = dura;
///////////////////////////
//END LOCATION/////////////
_("#### end_location:");
Hashtable end_location = (Hashtable) distance.get("end_location");
Double lat = (Double) end_location.get("lat");
_("########lat :"+lat);
Double lng = (Double) end_location.get("lng");
_("########lng:"+lng);
Coord endLocationCoord = new Coord(lng,lat);
mylegs[j].endLocationCoord=endLocationCoord;
///////////////////////////
///START LOCATION////////////////////
_("#### start_location:");
Hashtable start_location = (Hashtable) distance.get("start_location");
lat = (Double) start_location.get("lat");
_("########lat :"+lat);
lng = (Double) start_location.get("lng");
_("########lng:"+lng);
Coord startLocationCoord = new Coord(lng,lat);
mylegs[j].startLocationCoord=startLocationCoord;
/////////////////////////////////////
///END ADDRESS////////////////////////////
String end_address = (String) distance.get("end_address");
_("END ADDRESS:"+end_address);
mylegs[j].end_address= end_address;
//////////////////////////////////////////
///START ADDRESS///////////////////////////
String start_address = (String) distance.get("start_address");
_("START ADDRESS:"+start_address);
mylegs[j].start_address= start_address;
//////////////////////////////////////////
//VIA WAYPOINTS////////////////////////////
_("#### via_waypoint:");
Vector via_waypoint = (Vector) distance.get("via_waypoint");
/// implement this when needed!
/////////////////////////////////////
Vector steps = (Vector) distance.get("steps");
_("STEPS:"+steps.size());
Step mysteps[] = new Step[steps.size()];
///////////////////////////////////////////////////////////////
////////////////////GET EACH STEP SEPERATELY///////////////////
///////////////////////////////////////////////////////////////
for (int m=0; m<steps.size(); m++)
{
mysteps[m]=new Step();
Hashtable stepsHT = (Hashtable) steps.get(m);
// _("steps:"+stepsHT.size());
//_("_________________________________________________________");
//_("#### Distance:");
Hashtable distanceHT2 = (Hashtable) stepsHT.get("distance");
String text = (String) distanceHT2.get("text");
//_("########text :"+text);
Double value = (Double) distanceHT2.get("value");
//_("########value:"+value);
dist = new Distance(text,value);
mysteps[m].distance = dist;
//_("#### Duration:");
Hashtable durationHT2 = (Hashtable) stepsHT.get("duration");
text = (String) durationHT2.get("text");
//_("########text :"+text);
Double valueDbl = (Double) durationHT2.get("value");
//_("########value:"+valueDbl);
dura = new Duration(text,valueDbl);
mysteps[m].duration = dura;
//_("#### end_location:");
end_location = (Hashtable) stepsHT.get("end_location");
lat = (Double) end_location.get("lat");
//_("########lat :"+lat);
lng = (Double) end_location.get("lng");
//_("########lng:"+lng);
endLocationCoord = new Coord(lng,lat);
mysteps[m].endLocationCoord=endLocationCoord;
String html_instructions = (String) stepsHT.get("html_instructions");
//_("####html_instructions:"+html_instructions);
//_("####start_location:");
start_location = (Hashtable) stepsHT.get("start_location");
lat = (Double) start_location.get("lat");
//_("########lat :"+text);
lng = (Double) start_location.get("lng");
//_("########lng:"+value);
startLocationCoord = new Coord(lng,lat);
mysteps[m].startLocationCoord=endLocationCoord;
String travel_mode = (String) stepsHT.get("travel_mode");
//_("####travel_mode:"+travel_mode);
mysteps[m].travel_mode=travel_mode;
html_instructions = (String) stepsHT.get("html_instructions");
//_("####html_instructions:"+html_instructions);
mysteps[m].html_instructions=html_instructions;
//_("####Polyline:");
Hashtable polyline = (Hashtable) stepsHT.get("polyline");
text = (String) polyline.get("points");
//_("########points :"+text);
mysteps[m].points=text;
String maneuver = (String) stepsHT.get("maneuver");
//_("####maneuver:"+maneuver);
mysteps[m].maneuver=maneuver;
}
mylegs[j].steps= mysteps;
///////////////////////////////////////////////////////////////////
}
routeHolder.mylegs=mylegs;
_("____________________________");
_("##### PARSING COMPLETE #####");
_("____________________________");
_("testing data");
_("routeHolder copyright:"+routeHolder.copyrights);
_("routeHolder #legs:"+routeHolder.mylegs.length);
for (int x=0; x<routeHolder.mylegs.length; x++)
{
Leg leg = routeHolder.mylegs[x];
_(" "+x+"// Leg contains "+leg.steps.length+" steps");// ---> start_address:"+leg.start_address);
_(" Leg is "+leg.distance.text+" which will take "+leg.duration.text);
_(" Start @ "+leg.start_address);
_(" End @ "+leg.end_address);
if(segmentsToPlotSTART==null)
{
segmentsToPlotSTART= new Coord[leg.steps.length];
}
for (int y=0; y<leg.steps.length; y++)
{
Step step = leg.steps[y];
_(" "+y+"// START Step distance is "+step.distance.text+" "+step.html_instructions);
segmentsToPlotSTART[y] = step.startLocationCoord;
}
if(segmentsToPlotEND==null)
{
segmentsToPlotEND= new Coord[leg.steps.length];
}
for (int y=0; y<leg.steps.length; y++)
{
Step step = leg.steps[y];
_(" "+y+"// END Step distance is "+step.distance.text+" "+step.html_instructions);
segmentsToPlotEND[y] = step.endLocationCoord;
}
}
}
}
};
String origin = ""+lastLocation.getLatitude()+","+lastLocation.getLongitude();
String dest = closestStation.latitude+","+closestStation.longitude;
req.setPost(false);
//req.addArgument("location", "" + loc.getLatitude() + "," + loc.getLongtitude());
req.addArgument("origin", origin);
req.addArgument("destination", dest);
req.addArgument("sensor", "false");
// req.addArgument("client", "radiantsilverlabs");
_("origin:"+origin);
_("destination:"+dest);
//and replace it here.
String key = Prefs.GOOGLE_MAPS_KEY;//AddYourOwnKeyHere";
// req.addArgument("key", key);
NetworkManager.getInstance().addToQueueAndWait(req);
catch (Exception ex) {
ex.printStackTrace();
}
}