diff --git a/src/org/oscim/stuff/RegionLookup.java b/src/org/oscim/stuff/RegionLookup.java new file mode 100644 index 00000000..cccff940 --- /dev/null +++ b/src/org/oscim/stuff/RegionLookup.java @@ -0,0 +1,143 @@ +/* + * Copyright 2010, 2011, 2012 mapsforge.org + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package org.oscim.stuff; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.oscim.core.MapPosition; +import org.oscim.view.MapView; + +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Message; +import android.util.Log; + +public class RegionLookup { + /* package */final static String TAG = RegionLookup.class.getName(); + + private Connection connection = null; + private PreparedStatement prepQuery = null; + + private MapView mMapView; + + private final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + Log.d(TAG, "message: " + msg.what); + // switch (msg.what) { + // handle update + // ..... + // } + } + }; + + private static final String QUERY = "" + + "SELECT * from __get_regions_around(?,?)"; + + public RegionLookup(MapView mapView) { + mMapView = mapView; + } + + boolean connect() { + Connection conn = null; + String dburl = "jdbc:postgresql://city.informatik.uni-bremen.de:5432/gis"; + + Properties dbOpts = new Properties(); + dbOpts.setProperty("user", "osm"); + dbOpts.setProperty("password", "osm"); + dbOpts.setProperty("socketTimeout", "50"); + dbOpts.setProperty("tcpKeepAlive", "true"); + + try { + DriverManager.setLoginTimeout(20); + Log.d(TAG, "Creating JDBC connection..."); + + Class.forName("org.postgresql.Driver"); + conn = DriverManager.getConnection(dburl, dbOpts); + connection = conn; + prepQuery = conn.prepareStatement(QUERY); + } catch (Exception e) { + Log.d(TAG, "Aborted due to error:" + e); + return false; + } + return true; + } + + boolean updatePosition() { + if (connection == null) { + if (!connect()) + return false; + } + + ResultSet r; + MapPosition pos = mMapView.getMapPosition().getMapPosition(); + + try { + prepQuery.setDouble(1, pos.lat); + prepQuery.setDouble(2, pos.lon); + + Log.d(TAG, "" + prepQuery.toString()); + prepQuery.execute(); + r = prepQuery.getResultSet(); + } catch (SQLException e) { + e.printStackTrace(); + connection = null; + return false; + } + String level, name, boundary; + double minx, miny, maxx, maxy; + try { + while (r != null && r.next()) { + level = r.getString(1); + boundary = r.getString(2); + name = r.getString(3); + minx = r.getDouble(4); + miny = r.getDouble(5); + maxx = r.getDouble(6); + maxy = r.getDouble(7); + + Log.d(TAG, "got:" + level + " b:" + boundary + " n:" + name + " " + minx + + " " + miny + " " + maxx + " " + maxy); + + } + } catch (SQLException e) { + e.printStackTrace(); + connection = null; + return false; + } + return true; + } + + public void updateRegion() { + new AsyncTask() { + @Override + protected Long doInBackground(Object... params) { + RegionLookup.this.updatePosition(); + return null; + } + + // @Override + // protected void onPostExecute(Long result) { + // Log.d(TAG, "got sth " + result); + // } + }.execute(null, null, null); + } + +} diff --git a/src/org/oscim/view/MapView.java b/src/org/oscim/view/MapView.java index b92419b1..0a64712b 100644 --- a/src/org/oscim/view/MapView.java +++ b/src/org/oscim/view/MapView.java @@ -30,6 +30,7 @@ import org.oscim.database.MapDatabaseFactory; import org.oscim.database.MapDatabases; import org.oscim.database.MapInfo; import org.oscim.database.OpenResult; +import org.oscim.stuff.RegionLookup; import org.oscim.theme.ExternalRenderTheme; import org.oscim.theme.InternalRenderTheme; import org.oscim.theme.RenderTheme; @@ -84,6 +85,7 @@ public class MapView extends GLSurfaceView { private int mNumMapWorkers = 4; private DebugSettings debugSettings; private String mRenderTheme; + private Map mMapOptions; /** * @param context @@ -145,6 +147,7 @@ public class MapView extends GLSurfaceView { mJobQueue = new JobQueue(); mMapRenderer = MapRendererFactory.createMapRenderer(this, mapGeneratorType); + mMapWorkers = new MapWorker[mNumMapWorkers]; for (int i = 0; i < mNumMapWorkers; i++) { @@ -162,7 +165,7 @@ public class MapView extends GLSurfaceView { if (i == 0) mMapDatabase = mapDatabase; - mMapWorkers[i] = new MapWorker(i, this, mapGenerator, mMapRenderer); + mMapWorkers[i] = new MapWorker(i, mJobQueue, mapGenerator, mMapRenderer); mMapWorkers[i].start(); } @@ -186,21 +189,12 @@ public class MapView extends GLSurfaceView { if (!debugFrameTime) setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); + + mRegionLookup = new RegionLookup(this); + } - /** - * @return the debug settings which are used in this MapView. - */ - public DebugSettings getDebugSettings() { - return debugSettings; - } - - /** - * @return the job queue which is used in this MapView. - */ - public JobQueue getJobQueue() { - return mJobQueue; - } + RegionLookup mRegionLookup; /** * @return the map database which is used for reading map files. @@ -216,13 +210,6 @@ public class MapView extends GLSurfaceView { return mMapViewPosition; } - /** - * @return the currently used projection of the map. Do not keep this object for a longer time. - */ - public Projection getProjection() { - return mProjection; - } - @Override public boolean onTouchEvent(MotionEvent motionEvent) { if (this.isClickable()) @@ -247,7 +234,12 @@ public class MapView extends GLSurfaceView { mMapRenderer.updateMap(true); } - private Map mMapOptions; + /** + * @return the debug settings which are used in this MapView. + */ + public DebugSettings getDebugSettings() { + return debugSettings; + } public Map getMapOptions() { return mMapOptions; diff --git a/src/org/oscim/view/TouchHandler.java b/src/org/oscim/view/TouchHandler.java index 63a989c0..48ca0dbe 100644 --- a/src/org/oscim/view/TouchHandler.java +++ b/src/org/oscim/view/TouchHandler.java @@ -334,22 +334,24 @@ public class TouchHandler { public void onLongPress(MotionEvent e) { Log.d("mapsforge", "long press"); + mMapView.mRegionLookup.updateRegion(); + // mMapView.zoom((byte) -1); - mPrevScale = 0; + // mPrevScale = 0; - mTimer = new CountDownTimer((int) mScaleDuration, 30) { - @Override - public void onTick(long tick) { - scale2(tick); - } - - @Override - public void onFinish() { - scale2(0); - - } - }.start(); + // mTimer = new CountDownTimer((int) mScaleDuration, 30) { + // @Override + // public void onTick(long tick) { + // scale2(tick); + // } + // + // @Override + // public void onFinish() { + // scale2(0); + // + // } + // }.start(); } diff --git a/src/org/oscim/view/mapgenerator/MapWorker.java b/src/org/oscim/view/mapgenerator/MapWorker.java index a50f7add..a9467548 100644 --- a/src/org/oscim/view/mapgenerator/MapWorker.java +++ b/src/org/oscim/view/mapgenerator/MapWorker.java @@ -15,8 +15,6 @@ package org.oscim.view.mapgenerator; import org.oscim.view.IMapRenderer; -import org.oscim.view.MapView; -import org.oscim.view.utils.PausableThread; /** * A MapWorker uses a {@link IMapGenerator} to generate map tiles. It runs in a separate thread to avoid blocking the UI @@ -33,17 +31,15 @@ public class MapWorker extends PausableThread { /** * @param id * thread id - * @param mapView - * the MapView for which this MapWorker generates map tiles. * @param mapGenerator * ... * @param mapRenderer * ... */ - public MapWorker(int id, MapView mapView, IMapGenerator mapGenerator, + public MapWorker(int id, JobQueue jobQueue, IMapGenerator mapGenerator, IMapRenderer mapRenderer) { super(); - mJobQueue = mapView.getJobQueue(); + mJobQueue = jobQueue; mMapGenerator = mapGenerator; mMapRenderer = mapRenderer; diff --git a/src/org/oscim/view/utils/PausableThread.java b/src/org/oscim/view/mapgenerator/PausableThread.java similarity index 98% rename from src/org/oscim/view/utils/PausableThread.java rename to src/org/oscim/view/mapgenerator/PausableThread.java index 593fa5e8..8d048728 100644 --- a/src/org/oscim/view/utils/PausableThread.java +++ b/src/org/oscim/view/mapgenerator/PausableThread.java @@ -12,7 +12,7 @@ * You should have received a copy of the GNU Lesser General Public License along with * this program. If not, see . */ -package org.oscim.view.utils; +package org.oscim.view.mapgenerator; /** * An abstract base class for threads which support pausing and resuming.