Getting Directions From Mapquest.com |
|||
Directions.java: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; import java.io.*; import javax.microedition.io.*; public class Directions extends MIDlet implements CommandListener { private Command exitCommand, goCommand, backCommand; private Display display; private Form locationsScreen; private TextField addrFromField, zipFromField, addrToField, zipToField; private Form directionsScreen; public Directions() { // Get the Display object for the MIDlet display = Display.getDisplay(this); // Create the Exit, Go, and Back commands exitCommand = new Command("Exit", Command.EXIT, 2); goCommand = new Command("Go", Command.OK, 2); backCommand = new Command("Back", Command.BACK, 2); // Create the locations screen form locationsScreen = new Form("Enter Locations"); addrFromField = new TextField("From Address", "6663 Owens Drive", 30, TextField.ANY); locationsScreen.append(addrFromField); zipFromField = new TextField("From Zip Code", "94588", 5, TextField.NUMERIC); locationsScreen.append(zipFromField); addrToField = new TextField("To Address", "536 Mission Street", 30, TextField.ANY); locationsScreen.append(addrToField); zipToField = new TextField("To Zip Code", "94105", 5, TextField.NUMERIC); locationsScreen.append(zipToField); // Set the Exit and Go commands for the locations screen locationsScreen.addCommand(exitCommand); locationsScreen.addCommand(goCommand); locationsScreen.setCommandListener(this); // Create the directions screen form directionsScreen = new Form("Directions"); // Set the Back command for the directions screen directionsScreen.addCommand(backCommand); directionsScreen.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { // Set the current display to the locations screen display.setCurrent(locationsScreen); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if (c == goCommand) { // Get the directions getDirections(addrFromField.getString().toLowerCase(), zipFromField.getString().toLowerCase(), addrToField.getString().toLowerCase(), zipToField.getString().toLowerCase()); } else if (c == backCommand) { // Set the current display back to the locations screen display.setCurrent(locationsScreen); // Clear the directions for (int i = directionsScreen.size() - 1; i >= 0; i--) directionsScreen.delete(i); } } private String replaceSpaces(String s) { StringBuffer str = new StringBuffer(s); // Read each character and replace if it's a space // Original code from book //for (int i = 0; i < str.length(); i++) { // if (str.charAt(i) == ' ') { // str.deleteCharAt(i); // str.insert(i, "%20"); // } // Read each character and replace if it's a space // This block of code I included after mapquest made some changes. for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { str.deleteCharAt(i); str.insert(i, "+"); } } return str.toString(); } private void getDirections(String addrFrom, String zipFrom, String addrTo, String zipTo) { StreamConnection conn = null; InputStream in = null; StringBuffer data = new StringBuffer(); // Replace any spaces in the addresses addrFrom = replaceSpaces(addrFrom); addrTo = replaceSpaces(addrTo); // Build the URL for the directions page (Original) // The following line is the original line of the code in the book //String url = "http://www.mapquest.com/cgi-bin/mqtrip?link=btwn/twn-ddir_options_jumppage&" + // "print.x=1&" + "ADDR_0=" + addrFrom + "&ZIP_0=" + zipFrom + // "&ADDR_1=" + addrTo + "&ZIP_1=" + zipTo + "&CC_0=US&CC_1=US"; // Build the URL for the directions page (Adapted after mapquest changed) // The following line is the new one I included. String url = "http://www.mapquest.com/directions/main.adp?go=1&do=nw&" + "ct=NA&1ah=&1a=" + addrFrom + "&1p=&1c=&1s=&1z=" + zipFrom + "&1y=US&" + "2ah=&2a=" + addrTo + "&2p=&2c=&2s=&2z=" + zipTo + "&2y=US&lr=2&x=70&y=15"; try { // Open the HTTP connection conn = (StreamConnection)Connector.open(url); // Obtain an input stream for the connection in = conn.openInputStream(); // Read a line at a time from the input stream int ch, step = 1; boolean isStep = false; while ((ch = in.read()) != -1) { if (ch != '\n') { // Read the line a character at a time data.append((char)ch); } else { // If this line is a step, add it to the directions text box if (isStep) { directionsScreen.append(new StringItem("", "* " + data.toString())); isStep = false; } else // See if this line precedes a step in the directions // The following line is the original line of the code in the book //if (data.toString().compareTo("<b>" + step + ":</b>") == 0) { // This following line does not work due to spaces in the beginning (I believe) //if (data.toString().compareTo("<td class=formType align=right><b>" + // step + ":</b> </td>") == 0) { // The following line is the new one I included. It works but it // includes html commands in the result. Need some poolishment. if (data.toString().indexOf("<td class=formType align=right><b>" + step + ":</b> </td>") >= 0) { System.out.println("indexOf=true - step occurrence found"); isStep = true; step++; } // Clear the string for the next line data = new StringBuffer(); } } } catch (IOException e) { System.err.println("The connection could not be established."); } // Display the directions screen if everything is OK, otherwise display an alert if (directionsScreen.size() > 0) display.setCurrent(directionsScreen); else display.setCurrent(new Alert("Directions", "The locations are invalid. Please try again.", null, AlertType.ERROR)); } }
|