Initial rendering
git-svn-id: https://osmand.googlecode.com/svn/trunk@449 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
parent
d8e20d3499
commit
9c99e216a7
3 changed files with 205 additions and 10 deletions
|
@ -497,6 +497,17 @@ public class ResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////// Working with map ////////////////////////////////////////////////
|
||||
public void updateRendererIfNeeded(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom){
|
||||
if(!renderer.updateMap(topLatitude, leftLongitude, bottomLatitude, rightLongitude, zoom)){
|
||||
asyncLoadingTiles.requestToLoadMap(
|
||||
new MapLoadRequest(topLatitude, leftLongitude, bottomLatitude, rightLongitude, zoom));
|
||||
}
|
||||
}
|
||||
|
||||
public RenderMapsRepositories getRenderer() {
|
||||
return renderer;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////// Closing methods ////////////////////////////////////////////////
|
||||
|
||||
|
@ -617,6 +628,8 @@ public class ResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static class TransportLoadRequest {
|
||||
public final TransportIndexRepository repository;
|
||||
public final double topLatitude;
|
||||
|
@ -637,6 +650,24 @@ public class ResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
private static class MapLoadRequest {
|
||||
public final double topLatitude;
|
||||
public final double bottomLatitude;
|
||||
public final double leftLongitude;
|
||||
public final double rightLongitude;
|
||||
public final int zoom;
|
||||
|
||||
public MapLoadRequest(double topLatitude, double leftLongitude,
|
||||
double bottomLatitude, double rightLongitude, int zoom) {
|
||||
super();
|
||||
this.bottomLatitude = bottomLatitude;
|
||||
this.leftLongitude = leftLongitude;
|
||||
this.rightLongitude = rightLongitude;
|
||||
this.topLatitude = topLatitude;
|
||||
this.zoom = zoom;
|
||||
}
|
||||
}
|
||||
|
||||
public class AsyncLoadingThread extends Thread {
|
||||
Stack<Object> requests = new Stack<Object>();
|
||||
|
||||
|
@ -651,6 +682,7 @@ public class ResourceManager {
|
|||
boolean update = false;
|
||||
boolean amenityLoaded = false;
|
||||
boolean transportLoaded = false;
|
||||
boolean mapLoaded = false;
|
||||
while(!requests.isEmpty()){
|
||||
Object req = requests.pop();
|
||||
if (req instanceof TileLoadDownloadRequest) {
|
||||
|
@ -672,9 +704,14 @@ public class ResourceManager {
|
|||
r.bottomLatitude, r.rightLongitude, r.zoom, LIMIT_TRANSPORT, null);
|
||||
transportLoaded = true;
|
||||
}
|
||||
} else if(req instanceof MapLoadRequest){
|
||||
if(!mapLoaded){
|
||||
MapLoadRequest r = (MapLoadRequest) req;
|
||||
renderer.loadMap(r.topLatitude, r.leftLongitude, r.bottomLatitude, r.rightLongitude, r.zoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(update || amenityLoaded || transportLoaded){
|
||||
if(update || amenityLoaded || transportLoaded || mapLoaded){
|
||||
// use downloader callback
|
||||
for(IMapDownloaderCallback c : downloader.getDownloaderCallbacks()){
|
||||
c.tileDownloaded(null);
|
||||
|
@ -696,6 +733,10 @@ public class ResourceManager {
|
|||
requests.push(req);
|
||||
}
|
||||
|
||||
public void requestToLoadMap(MapLoadRequest req){
|
||||
requests.push(req);
|
||||
}
|
||||
|
||||
public void requestToLoadTransport(TransportLoadRequest req){
|
||||
requests.push(req);
|
||||
}
|
||||
|
|
|
@ -1,14 +1,20 @@
|
|||
package com.osmand.render;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import com.osmand.IProgress;
|
||||
import com.osmand.LogUtil;
|
||||
import com.osmand.data.index.IndexConstants;
|
||||
import com.osmand.osm.Node;
|
||||
import com.osmand.osm.Way;
|
||||
|
||||
public class RenderMapsRepositories {
|
||||
|
||||
|
@ -19,6 +25,7 @@ public class RenderMapsRepositories {
|
|||
private int cZoom;
|
||||
private double cLeftLongitude;
|
||||
private double cRightLongitude;
|
||||
private List<Way> cWays = new LinkedList<Way>();
|
||||
|
||||
|
||||
public boolean initializeNewResource(final IProgress progress, File file) {
|
||||
|
@ -40,6 +47,10 @@ public class RenderMapsRepositories {
|
|||
return true;
|
||||
}
|
||||
|
||||
public List<Way> getCache() {
|
||||
return cWays;
|
||||
}
|
||||
|
||||
|
||||
public void clearAllResources(){
|
||||
if(db != null){
|
||||
|
@ -62,24 +73,64 @@ public class RenderMapsRepositories {
|
|||
}
|
||||
|
||||
|
||||
public void loadMap(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom){
|
||||
cTopLatitude = topLatitude + (topLatitude - bottomLatitude);
|
||||
public void loadMap(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom) {
|
||||
cBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude);
|
||||
cTopLatitude = topLatitude + (topLatitude -bottomLatitude);
|
||||
cLeftLongitude = leftLongitude - (rightLongitude - leftLongitude);
|
||||
cRightLongitude = rightLongitude + (rightLongitude - leftLongitude);
|
||||
cZoom = zoom;
|
||||
// TODO clear cache
|
||||
loadingData();
|
||||
// after prepare images
|
||||
}
|
||||
|
||||
private void loadingData(){
|
||||
|
||||
String query = "SELECT ways.id way, node.id node, node.latitude, node.longitude FROM (" +
|
||||
"SELECT DISTINCT ways.id id FROM ways JOIN " +
|
||||
"(SELECT id, latitude, longitude FROM node WHERE ?< latitude AND latitude < ? AND ? < longitude AND longitude < ?) A "+
|
||||
"ON A.id = ways.node) B "+
|
||||
"JOIN ways ON B.id=ways.id JOIN node ON ways.node = node.id";
|
||||
|
||||
log.info(String.format("BLat=%s, TLat=%s, LLong=%s, RLong=%s, zoom=%s", cBottomLatitude, cTopLatitude, cLeftLongitude, cRightLongitude, zoom));
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
Cursor result = db.rawQuery(query, new String[]{Double.toString(cBottomLatitude),Double.toString(cTopLatitude),
|
||||
Double.toString(cLeftLongitude), Double.toString(cRightLongitude)});
|
||||
|
||||
List<Way> local = new LinkedList<Way>();
|
||||
try {
|
||||
int count = 0;
|
||||
if (result.moveToFirst()) {
|
||||
long id = result.getLong(0);
|
||||
long prevId = -1;
|
||||
Way way = new Way(id);
|
||||
do {
|
||||
count++;
|
||||
if (prevId != -1) {
|
||||
id = result.getLong(0);
|
||||
if (id != prevId) {
|
||||
local.add(way);
|
||||
way = new Way(id);
|
||||
}
|
||||
}
|
||||
Node node = new Node(result.getDouble(2), result.getDouble(3), result.getLong(1));
|
||||
way.addNode(node);
|
||||
prevId = id;
|
||||
} while (result.moveToNext());
|
||||
|
||||
cWays = local;
|
||||
}
|
||||
log.info(String.format("Search has been done in %s ms. %s results were found.", System.currentTimeMillis()-now, count));
|
||||
} finally {
|
||||
result.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void addToCache(Way way) {
|
||||
cWays.add(way);
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
|
||||
cWays.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
103
OsmAnd/src/com/osmand/render/RendererLayer.java
Normal file
103
OsmAnd/src/com/osmand/render/RendererLayer.java
Normal file
|
@ -0,0 +1,103 @@
|
|||
package com.osmand.render;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.Paint.Style;
|
||||
|
||||
import com.osmand.ResourceManager;
|
||||
import com.osmand.osm.MapUtils;
|
||||
import com.osmand.osm.Node;
|
||||
import com.osmand.osm.Way;
|
||||
import com.osmand.views.OsmandMapLayer;
|
||||
import com.osmand.views.OsmandMapTileView;
|
||||
|
||||
public class RendererLayer implements OsmandMapLayer {
|
||||
|
||||
private OsmandMapTileView view;
|
||||
private final static int startZoom = 15;
|
||||
private Rect pixRect;
|
||||
private RectF tileRect;
|
||||
private ResourceManager resourceManager;
|
||||
private Paint paint;
|
||||
|
||||
@Override
|
||||
public void destroyLayer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawInScreenPixels() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initLayer(OsmandMapTileView view) {
|
||||
this.view = view;
|
||||
pixRect = new Rect();
|
||||
tileRect = new RectF();
|
||||
resourceManager = ResourceManager.getResourceManager();
|
||||
paint = new Paint();
|
||||
paint.setStyle(Style.STROKE);
|
||||
paint.setStrokeWidth(2);
|
||||
paint.setColor(Color.RED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas) {
|
||||
if (view.getZoom() >= startZoom) {
|
||||
pixRect.set(0, 0, view.getWidth(), view.getHeight());
|
||||
view.calculateTileRectangle(pixRect, view.getCenterPointX(),
|
||||
view.getCenterPointY(), view.getXTile(), view.getYTile(), tileRect);
|
||||
double topLatitude = MapUtils.getLatitudeFromTile(view.getZoom(), tileRect.top);
|
||||
double leftLongitude = MapUtils.getLongitudeFromTile(view.getZoom(), tileRect.left);
|
||||
double bottomLatitude = MapUtils.getLatitudeFromTile(view.getZoom(), tileRect.bottom);
|
||||
double rightLongitude = MapUtils.getLongitudeFromTile(view.getZoom(), tileRect.right);
|
||||
|
||||
resourceManager.updateRendererIfNeeded(topLatitude, leftLongitude, bottomLatitude, rightLongitude, view.getZoom());
|
||||
RenderMapsRepositories renderer = resourceManager.getRenderer();
|
||||
if(renderer != null) {
|
||||
for (Way way:renderer.getCache()) {
|
||||
draw(way,canvas);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void draw(Way way, Canvas canvas) {
|
||||
|
||||
Path path = null;
|
||||
for (Node node:way.getNodes()) {
|
||||
int x = view.getMapXForPoint(node.getLongitude());
|
||||
int y = view.getMapYForPoint(node.getLatitude());
|
||||
if (path == null) {
|
||||
path = new Path();
|
||||
path.moveTo(x, y);
|
||||
} else {
|
||||
path.lineTo(x, y);
|
||||
}
|
||||
}
|
||||
if (path != null) {
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongPressEvent(PointF point) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(PointF point) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue