2. Outline
• Environment
• Open Street Maps
• Our aplication
• Code
• Running the aplication
• What’s next?
• Video demostration
3. Environment
Eclipse IDE for Java Developers
Android SDK Tools
Apache Tomcat
PostgresSQL OpenSource Database
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. Our Aplication
Our aplication allows users getting the average position of a set
of points
Current point Average point
6. General view of the code
Android Aplication Dynamic Web Database
Main_Activity DynWeb
OnlocationChanged Servlet Location Connect
postgres
(Hub) database
OnCreate (ConnDB)
DisplayLabel
OnDraw
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. Code Set request and response
Dynamic Web. Servlet
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
String point = request.getParameter("point");
if("location".equalsIgnoreCase(point)) {
Location location = new Location();
Location.getLocation(request, response);
}
}
...
Connects server and database
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" );
}
...
8. Code
Telling the database
how to store the records
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);
...
Sending a SQL query to get back the average of all latitude
and longitude values that are already store in the database
9. Code Displaying map
Main_Activity. OnCreate
public 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. Code
Main_Activity. OnLocationChanged
... Connect to the server
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(); Sending latitude and
DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream()); longitude from current
dop.writeBytes("point=" + URLEncoder.encode("location","utf-8"));
dop.writeBytes("&lon=" + URLEncoder.encode(Double.toString(Lon),"utf-8")); positionto the server
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]); Reading latitude and longitude
AVGLon = Double.parseDouble(mystrings[2]);
noavgyet = false;
of the average point of all
recorded points and display
displayLabel(locPassage);
... it in the label
11. Code Setting the OSM display
Main_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 point
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);
...
12. Running the aplication
Simulate GPS with Dalvik Debug Monitor Server
KML file with 5 points in Vaxholm
-- Table: allpositions
Database
-- DROP TABLE allpositions;
CREATE TABLE allpositions
Allpositions table (
With 2 columns: reportlat double precision,
reportlon double precision
reportlat )
reportlon WITH (
OIDS=FALSE
);
ALTER TABLE allpositions OWNER TO postgres;
14. Use of the app
Imagine 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
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