16
Average An android aplication Manuela Alvarez Ipsit Dash

Average- An android project

Embed Size (px)

Citation preview

Page 1: Average- An android project

AverageAn android aplication

Manuela AlvarezIpsit Dash

Page 2: Average- An android project

Outline

• Environment

• Open Street Maps

• Our aplication

• Code

• Running the aplication

• What’s next?

• Video demostration

Page 3: Average- An android project

EnvironmentEclipse IDE for Java DevelopersAndroid SDK ToolsApache TomcatPostgresSQL OpenSource Database

Page 4: Average- An android project

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)

Page 5: Average- An android project

Our Aplication

Our aplication allows users getting the average position of a set of points

Current point Average point

Page 6: Average- An android project

General view of the code

OnCreateDisplayLabelOnDraw

Dynamic WebDynWeb

Servlet (Hub)

Connect database (ConnDB)

postgres

Android AplicationMain_Activity

Database

OnlocationChanged

LayoutActivity_main.xml

Emulator

Location

OnlocationChanged: Starts when change in locationOnCreate: Sets the display of OSM and LabelDisplayLabel: Says what to show in the labelOnDraw: Draws the pointsServlet: Sets request and response between device and databaseLocation: Starts when location is changed Looks what the new position is Sends position to the database where it is run SQL query Get reponse from databaseConnDB: Connects server to database

Page 7: Average- An android project

CodeDynamic Web. Servlet...protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubString point = request.getParameter("point");

if("location".equalsIgnoreCase(point)) {Location location = new Location();Location.getLocation(request, response);

}}...

Dynamic 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" );}...

Set request and response

Connects server and database

Page 8: Average- An android project

Code

Dynamic 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);

...

Telling the database how to store the records

Sending a SQL query to get back the average of all latitude and longitude values that are already store in the database

Page 9: Average- An android project

Code

Main_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(); 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); Label = (TextView)findViewById(R.id.Label); }

Displaying map

GPS

Setting zoom

Label to show average point coordinates

Page 10: Average- An android project

Code Main_Activity. OnLocationChanged ...

url = 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(); DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream()); dop.writeBytes("point=" + URLEncoder.encode("location","utf-8")); dop.writeBytes("&lon=" + URLEncoder.encode(Double.toString(Lon),"utf-8")); dop.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]); AVGLon = Double.parseDouble(mystrings[2]); noavgyet = false;

displayLabel(locPassage); ...

Connect to the server

Sending latitude and longitude from currentpositionto the server

Reading latitude and longitudeof the average point of all recorded points and display it in the label

Page 11: Average- An android project

CodeMain_Activity. Activiy_main.xml...

<org.osmdroid.views.MapViewxmlns: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" />

<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!" />...

Main_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);

...

Setting the OSM display

Setting the label

Drawing the average point

Page 12: Average- An android project

Running the aplication

Simulate GPS with Dalvik Debug Monitor ServerKML file with 5 points in Vaxholm

Database -- Table: allpositions

-- DROP TABLE allpositions;

CREATE TABLE allpositions( reportlat double precision, reportlon double precision)WITH ( OIDS=FALSE);

ALTER TABLE allpositions OWNER TO postgres;

Allpositions table With 2 columns:reportlatreportlon

Page 13: Average- An android project

Running the aplicationCurrent point

Average point

Page 14: Average- An android project

Use of the appImagine you want to build a factory in a city with a certain number of houses

Assume that we know where future workers live

Collect the coordinates from their houses positions

Get average position coordinates

This minimizes the total travelling distance from workers houses to the factory

Page 15: Average- An android project

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

Page 16: Average- An android project

Thank you!

Video demostration