Hi, my project is FRIENDS AND FAMILY LOCATOR in which i neeed to find latitude and longitude of a place where i m, have developed a code if i m providing the co-ordinates manually then it is showing me the exact location but otherwise if m not giving ny constant values and wanting to change the co-ordinates at regular intervals ... co-ordinates r the same and showing map of some different location
Plzz help me out... m stuck at this particular point!!!Code:public class MidletGPS extends MIDlet implements CommandListener,LocationListener { Form mainForm = new Form("Info GPS"); StringItem infoGPS = new StringItem("GPS status:", "unknown"); StringItem infoCoordinates = new StringItem("Coordinates:", "unknown"); Command cmdExit = new Command("EXIT", Command.EXIT, 1); Command cmdStart = new Command("GPS data", Command.SCREEN, 1); Command cmdDisplay = new Command("Display map", Command.SCREEN, 2); LocationProvider lp; Location loc; QualifiedCoordinates qc; double latitude = 0; //default values double longitude = 0; //default values public MidletGPS() { mainForm.append(infoGPS); mainForm.append(infoCoordinates); mainForm.addCommand(cmdExit); mainForm.addCommand(cmdStart); mainForm.addCommand(cmdDisplay); } private void getGPSData() { Display.getDisplay(this).setCurrent( mainForm); try { Criteria c=new Criteria(); c.setHorizontalAccuracy(1000); c.setVerticalAccuracy(1000); c.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW); lp=LocationProvider.getInstance(c); loc=lp.getLocation(60); qc=loc.getQualifiedCoordinates(); mainForm.append("Alt: "+qc.getAltitude()); mainForm.append("Lat: "+qc.getLatitude()); mainForm.append("Long: "+qc.getLongitude()); Alert a = new Alert("DONE"); mainForm.append("DONE"); } catch(Exception e) { mainForm.append("Exception: "+e); } } public void startApp() { Display.getDisplay(this).setCurrent(mainForm); mainForm.setCommandListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c == cmdExit) { //provider.setLocationListener(null, -1, -1, -1); destroyApp(true); notifyDestroyed(); } else if (c == cmdStart) { getGPSData(); } else if (c == cmdDisplay) { GoogleMaps gm = new GoogleMaps(this, Double.toString(latitude), Double.toString(longitude)); Display.getDisplay(this).setCurrent(gm); } } public void locationUpdated(LocationProvider arg0, Location arg1) { if (arg1 != null && arg1.isValid()) { //get the coordinates Coordinates coordinates = arg1.getQualifiedCoordinates(); if (coordinates != null) { //get the latitude and longitude of the coordinates. latitude = coordinates.getLatitude(); longitude = coordinates.getLongitude(); infoCoordinates.setText("\n" + "latitude: " + latitude + "\n" + "longitude: " + longitude); } else { //no valid coordinates infoGPS.setText("GPS inactive"); infoCoordinates.setText("unknown"); } } } public void providerStateChanged(LocationProvider arg0, int arg1) { if (arg1 == LocationProvider.OUT_OF_SERVICE || arg1 == LocationProvider.TEMPORARILY_UNAVAILABLE) { infoGPS.setText("GPS inactive"); } } /*public void setCurrentForm(Displayable nextForm) { Display.getDisplay(this).setCurrent(nextForm); }*/ public class GoogleMaps extends Canvas implements CommandListener { Command cmdBack = new Command("Back", Command.EXIT, 1); Command cmdRefresh = new Command("Refresh", Command.SCREEN, 1); //reference to the parent MIDlet MidletGPS midGPS; int zoom = 2; double latitude = 0; double longitude = 0 ; double altitude = 0; HttpConnection connection = null; public GoogleMaps(MidletGPS mGPS, String Lat, String Longit) { //only for testing //latitude = "18.961968"; //longitude = "72.835493"; //latitude = Lat; //longitude = Longit; latitude = (qc.getLatitude()); longitude = (qc.getLongitude()); altitude = (qc.getAltitude()); midGPS = mGPS; this.addCommand(cmdBack); this.addCommand(cmdRefresh); this.setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == cmdBack) { Display.getDisplay(midGPS).setCurrent(midGPS.mainForm); } if (c == cmdRefresh) { this.repaint(); } } public HttpConnection conGoogleMap() { //get the width and the height of the screen final int width = getWidth(); final int height = getHeight(); new Thread() { public void run() { //the query string for the Google service String url = "http://maps.google.com/maps/api/staticmap?center="; url += latitude + "," + longitude; url += "&zoom=" + String.valueOf(zoom); url += "&size=" + width + "x" + height + "&sensor=true"; try { connection = (HttpConnection) Connector.open(url); connection.setRequestMethod(HttpConnection.GET); } catch (Exception ex) { ex.printStackTrace(); } /*finally { try { if (connection != null) { connection.close(); } } catch (Exception ex) { ex.printStackTrace(); } }*/ } }.start(); return connection; } public Image getGoogleMap() { InputStream inputStream = null; HttpConnection connection2 = null; try { connection2 = conGoogleMap(); inputStream = connection2.openInputStream(); Image map = Image.createImage(inputStream); //another solution for creating the Image //ByteArrayOutputStream byteArray = new ByteArrayOutputStream();// //get the image byte by byte //int c; //while ((c = inputStream.read()) != -1) //{ //byteArray.write(c);// //} //byte[] buffer = byteArray.toByteArray(); //byteArray.close();// //create an Image object //logo = Image.createImage(buffer, 0, buffer.length); return map; } catch (Exception ex) { ex.printStackTrace(); } /*finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception ex) { ex.printStackTrace(); } }*/ return null; } //override paint handler protected void paint(Graphics g) { //get the image Image map = getGoogleMap(); if(map!=null) { //draw the image on the canvas g.drawImage(map, 0, 0, Graphics.LEFT | Graphics.TOP); } else { g.setColor(255,0,0); g.drawString("No map available", 20, 20, 0); } } protected void keyPressed(int keyCode) { if (((char) keyCode) == '1') { zoom--; } if (((char) keyCode) == '3') { zoom++; } //if you want to move the map in all directions double offset = 0.02; if (getGameAction(keyCode) == 5) { double lon = (longitude); lon += offset / zoom; longitude = lon; } if (getGameAction(keyCode) == 2) { double lon = (longitude); lon -= offset / zoom; longitude = (lon); } if (getGameAction(keyCode) == 4) { double lat = (latitude); lat += offset / zoom; latitude = (lat); } if (getGameAction(keyCode) == 6) { double lat = (latitude); lat -= offset / zoom; latitude = (lat); } //call the paint event this.repaint(); } } }
plzz do help me asap!!!
Thanks
Vinita!!!

Reply With Quote

