Average- An android project

841 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
841
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • createStatement()           Creates a Statement object for sending SQL statements to the database
  • Average- An android project

    1. 1. AverageAn android aplication Manuela Alvarez Ipsit Dash
    2. 2. Outline• Environment• Open Street Maps• Our aplication• Code• Running the aplication• What’s next?• Video demostration
    3. 3. Environment Eclipse IDE for Java Developers Android SDK Tools Apache Tomcat PostgresSQL OpenSource Database
    4. 4. Open Street Maps• Lot of possibilities• Multiple Web tools: ITO OSM tools, OSM inspector, OpenStreetBugs, Mapnik, Cloudmade, OpenCycleMap, OpenBusMap• Android: Mapzen POI Collector (Uses Cloudmade), Osmand (OSM Automated Navigation Directions)
    5. 5. Our AplicationOur aplication allows users getting the average position of a setof points Current point Average point
    6. 6. General view of the codeAndroid Aplication Dynamic Web Database Main_Activity DynWebOnlocationChanged Servlet Location Connect postgres (Hub) databaseOnCreate (ConnDB)DisplayLabelOnDraw Layout OnlocationChanged: Starts when change in location Activity_main.xml OnCreate: Sets the display of OSM and Label DisplayLabel: Says what to show in the label OnDraw: Draws the points Servlet: Sets request and response between device and database Location: Starts when location is changed Looks what the new position is Sends position to the database where it is run SQL query Emulator Get reponse from database ConnDB: Connects server to database
    7. 7. Code Set request and responseDynamic Web. Servlet...protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {// TODO Auto-generated method stubString point = request.getParameter("point"); if("location".equalsIgnoreCase(point)) { Location location = new Location(); Location.getLocation(request, response); }}... Connects server and databaseDynamic Web. Conection to database...private Connection connection = null;public Connection getConn() {Class.forName("org.postgresql.Driver").newInstance();String url = "jdbc:postgresql://localhost:5432/postgres" ;connection = DriverManager.getConnection(url, "postgres" , "postgres" );}...
    8. 8. Code Telling the database how to store the recordsDynamic Web. Location...ConnDB conndb = new ConnDB();String updateSQL = "insert into allPositions(reportlat, reportlon) select " + lat + ", " + lon;connect = conndb.getConn();Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);stmt.executeUpdate(updateSQL);Statement stmt2 = connect.createStatement();stmt2.executeQuery("select avg(reportlat) avgreportlat,avg(reportlon) avgreportlon from allPositions");ResultSet rs = stmt2.getResultSet();rs.next();double avglon = rs.getDouble(1);double avglat = rs.getDouble(2);DataOutputStream dos = new DataOutputStream(response.getOutputStream());dos.writeUTF("Succeed:" + avglon + ":" + avglat);... Sending a SQL query to get back the average of all latitude and longitude values that are already store in the database
    9. 9. Code Displaying mapMain_Activity. OnCreatepublic void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mapView = (MapView) this.findViewById(R.id.mapview); mapView.setTileSource(TileSourceFactory.MAPNIK); mapView.setBuiltInZoomControls(true); mapView.setMultiTouchControls(true); mapController = mapView.getController(); GPS mapController.setZoom(11); point = new GeoPoint(59351756, 18057822); mapController.setCenter(point); mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5*1000, 10,locListener); mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); Setting zoom Label = (TextView)findViewById(R.id.Label); } Label to show average point coordinates
    10. 10. CodeMain_Activity. OnLocationChanged... Connect to the serverurl = new URL(strUrl);HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();urlConn.setDoInput(true);urlConn.setDoOutput(true);urlConn.setRequestMethod("POST");urlConn.setUseCaches(false);urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");urlConn.setRequestProperty("Charest", "utf-8");urlConn.connect(); Sending latitude andDataOutputStream dop = new DataOutputStream(urlConn.getOutputStream()); longitude from currentdop.writeBytes("point=" + URLEncoder.encode("location","utf-8"));dop.writeBytes("&lon=" + URLEncoder.encode(Double.toString(Lon),"utf-8")); positionto the serverdop.writeBytes("&lat=" + URLEncoder.encode(Double.toString(Lat),"utf-8"));dop.flush();dop.close();DataInputStream dis = new DataInputStream(urlConn.getInputStream());String locPassage = dis.readUTF();String[] mystrings = locPassage.split(":");AVGLat = Double.parseDouble(mystrings[1]); Reading latitude and longitudeAVGLon = Double.parseDouble(mystrings[2]);noavgyet = false; of the average point of all recorded points and displaydisplayLabel(locPassage);... it in the label
    11. 11. Code Setting the OSM displayMain_Activity. Activiy_main.xml... <org.osmdroid.views.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> Setting the label <TextView android:id="@+id/Label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="5dip" android:text="Welcome!" />... Drawing the average pointMain_Activity .OnDraw...if (!noavgyet) { GeoPoint pointavg = new GeoPoint((int)(AVGLat*1e6), (int)(AVGLon*1e6)); Paint paint2 = new Paint(); paint2.setColor(Color.GREEN); Point screenPoint2 = new Point(); mapView.getProjection().toPixels(pointavg, screenPoint2); Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.dot2); canvas.drawBitmap(bmp2, screenPoint2.x, screenPoint2.y, paint2);...
    12. 12. Running the aplicationSimulate GPS with Dalvik Debug Monitor ServerKML file with 5 points in Vaxholm -- Table: allpositionsDatabase -- DROP TABLE allpositions; CREATE TABLE allpositionsAllpositions table (With 2 columns: reportlat double precision, reportlon double precisionreportlat )reportlon WITH ( OIDS=FALSE ); ALTER TABLE allpositions OWNER TO postgres;
    13. 13. Running the aplication Current point Average point
    14. 14. Use of the appImagine you want to build a factory in a city with a certain number of housesAssume that we know where future workers liveCollect the coordinates from their houses positionsGet average position coordinatesThis minimizes the total travelling distance from workers houses to the factory
    15. 15. What’s next• Add a reset button to clear records from the database• Add a button for getting GPS coordinates when pushed• Sharing location between users• Drawing all recorded points in the display
    16. 16. Thank you! Video demostration

    ×