From 5e30a1358878ebb1510ce6e1e6f2317d0302f302 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 27 May 2010 15:33:52 +0000 Subject: [PATCH] enable go to point git-svn-id: https://osmand.googlecode.com/svn/trunk@89 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- OsmAnd/res/menu/map_menu.xml | 1 + OsmAnd/res/values/strings.xml | 2 + .../com/osmand/AmenityIndexRepository.java | 6 +- .../com/osmand/activities/MapActivity.java | 31 ++++- .../com/osmand/views/OsmandMapTileView.java | 14 +++ .../com/osmand/views/PointLocationLayer.java | 22 +--- .../osmand/views/PointNavigationLayer.java | 107 ++++++++++++++++++ 7 files changed, 154 insertions(+), 29 deletions(-) create mode 100644 OsmAnd/src/com/osmand/views/PointNavigationLayer.java diff --git a/OsmAnd/res/menu/map_menu.xml b/OsmAnd/res/menu/map_menu.xml index 09fbfecbaa..670992345e 100644 --- a/OsmAnd/res/menu/map_menu.xml +++ b/OsmAnd/res/menu/map_menu.xml @@ -3,5 +3,6 @@ xmlns:android="http://schemas.android.com/apk/res/android"> + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 887247f0e4..f3e1528740 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,7 @@ + Unmark location + Mark location Enable 3D view of the map Map View 3D Rotate map to bearing of your direction diff --git a/OsmAnd/src/com/osmand/AmenityIndexRepository.java b/OsmAnd/src/com/osmand/AmenityIndexRepository.java index 2142891064..feaebb0467 100644 --- a/OsmAnd/src/com/osmand/AmenityIndexRepository.java +++ b/OsmAnd/src/com/osmand/AmenityIndexRepository.java @@ -68,7 +68,7 @@ public class AmenityIndexRepository { } - public void clearCache(){ + public synchronized void clearCache(){ cachedAmenities.clear(); cTopLatitude = 0; cBottomLatitude = 0; @@ -76,7 +76,7 @@ public class AmenityIndexRepository { cLeftLongitude = 0; } - public void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List toFill){ + public synchronized void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List toFill){ cachedAmenities.clear(); cTopLatitude = topLatitude + (topLatitude -bottomLatitude); cBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude); @@ -86,7 +86,7 @@ public class AmenityIndexRepository { checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill); } - public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List toFill, boolean fillFound){ + public synchronized boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List toFill, boolean fillFound){ if (db == null) { return true; } diff --git a/OsmAnd/src/com/osmand/activities/MapActivity.java b/OsmAnd/src/com/osmand/activities/MapActivity.java index dc428b12b4..e4f0c44645 100644 --- a/OsmAnd/src/com/osmand/activities/MapActivity.java +++ b/OsmAnd/src/com/osmand/activities/MapActivity.java @@ -31,6 +31,7 @@ import com.osmand.osm.LatLon; import com.osmand.views.OsmandMapTileView; import com.osmand.views.POIMapLayer; import com.osmand.views.PointLocationLayer; +import com.osmand.views.PointNavigationLayer; public class MapActivity extends Activity implements LocationListener, IMapLocationListener { @@ -39,14 +40,13 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat private OsmandMapTileView mapView; private boolean linkLocationWithMap = true; - private ImageButton backToLocation; - private ImageButton backToMenu; private PointLocationLayer locationLayer; - + private PointNavigationLayer navigationLayer; private POIMapLayer poiMapLayer; + private WakeLock wakeLock; @@ -65,6 +65,8 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat mapView.setMapLocationListener(this); poiMapLayer = new POIMapLayer(); mapView.addLayer(poiMapLayer); + navigationLayer = new PointNavigationLayer(); + mapView.addLayer(navigationLayer); locationLayer = new PointLocationLayer(); mapView.addLayer(locationLayer); @@ -152,8 +154,19 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat } } } - + public void navigateToPoint(LatLon point){ + navigationLayer.setPointToNavigate(point); + } + + public Location getLastKnownLocation(){ + return locationLayer.getLastKnownLocation(); + } + + public LatLon getPointToNavigate(){ + return navigationLayer.getPointToNavigate(); + } + @Override public void onLocationChanged(Location location) { @@ -247,7 +260,7 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat inflater.inflate(R.menu.map_menu, menu); return true; } - + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.map_show_location) { @@ -260,6 +273,14 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat final Intent settings = new Intent(MapActivity.this, SettingsActivity.class); startActivity(settings); return true; + } else if (item.getItemId() == R.id.map_navigate_to_point) { + if(navigationLayer.getPointToNavigate() != null){ + item.setTitle(R.string.navigate_to_point); + navigateToPoint(null); + } else { + item.setTitle(R.string.stop_navigation); + navigateToPoint(new LatLon(mapView.getLatitude(), mapView.getLongitude())); + } } return super.onOptionsItemSelected(item); } diff --git a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java index 61d9bfd8c6..f5d64dcb67 100644 --- a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java +++ b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java @@ -379,6 +379,20 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall } + public boolean isPointOnTheMap(double latitude, double longitude){ + int cx = getWidth()/2; + int cy = getHeight()/2; + int dx = MapUtils.getPixelShiftX(zoom, longitude, this.longitude, getTileSize()); + int dy = MapUtils.getPixelShiftY(zoom, latitude, this.latitude , getTileSize()); + float rad = (float) Math.toRadians(rotate); + int newX = (int) (dx * FloatMath.cos(rad) - dy * FloatMath.sin(rad) + cx); + int newY = (int) (dx * FloatMath.sin(rad) + dy * FloatMath.cos(rad) + cy); + if(newX >= 0 && newX <= getWidth() && newY >=0 && newY <= getHeight()){ + return true; + } + return false; + } + @Override public void dragTo(float fromX, float fromY, float toX, float toY){ float dx = (fromX - toX) ; diff --git a/OsmAnd/src/com/osmand/views/PointLocationLayer.java b/OsmAnd/src/com/osmand/views/PointLocationLayer.java index e46c6d9438..c81a85875e 100644 --- a/OsmAnd/src/com/osmand/views/PointLocationLayer.java +++ b/OsmAnd/src/com/osmand/views/PointLocationLayer.java @@ -7,7 +7,6 @@ import android.graphics.Paint; import android.graphics.Path; import android.graphics.Paint.Style; import android.location.Location; -import android.util.FloatMath; import android.view.MotionEvent; import com.osmand.osm.MapUtils; @@ -103,26 +102,7 @@ public class PointLocationLayer implements OsmandMapLayer { if(l == null || view == null){ return false; } - int cx = view.getWidth()/2; - int cy = view.getHeight()/2; - int dx = MapUtils.getPixelShiftX(view.getZoom(), - l.getLongitude(), view.getLongitude(), view.getTileSize()); - int dy = MapUtils.getPixelShiftY(view.getZoom(), - l.getLatitude(), view.getLatitude() , view.getTileSize()); - float rad = (float) Math.toRadians(view.getRotate()); - int newX = (int) (dx * FloatMath.cos(rad) - dy * FloatMath.sin(rad) + cx); - int newY = (int) (dx * FloatMath.sin(rad) + dy * FloatMath.cos(rad) + cy); - int radius = MapUtils.getLengthXFromMeters(view.getZoom(), view.getLatitude(), view.getLongitude(), - l.getAccuracy(), view.getTileSize(), view.getWidth()); - if(newX >= 0 && newX <= view.getWidth() && newY >=0 && newY <= view.getHeight()){ - return true; - } else { - // check radius (simplified version - if (newX + radius >= 0 && newX - radius <= view.getWidth() && newY + radius >= 0 && newY - radius <= view.getHeight()) { - return true; - } - } - return false; + return view.isPointOnTheMap(l.getLatitude(), l.getLongitude()); } diff --git a/OsmAnd/src/com/osmand/views/PointNavigationLayer.java b/OsmAnd/src/com/osmand/views/PointNavigationLayer.java new file mode 100644 index 0000000000..d5a3888102 --- /dev/null +++ b/OsmAnd/src/com/osmand/views/PointNavigationLayer.java @@ -0,0 +1,107 @@ +package com.osmand.views; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Paint.Style; +import android.location.Location; +import android.view.MotionEvent; + +import com.osmand.osm.LatLon; +import com.osmand.osm.MapUtils; + +public class PointNavigationLayer implements OsmandMapLayer { + protected final static int RADIUS = 10; + protected final static int DIST_TO_SHOW = 120; + + private Paint point; + + protected LatLon pointToNavigate = null; + private OsmandMapTileView view; + private Path pathForDirection; + private float[] calculations = new float[2]; + + + private void initUI() { + point = new Paint(); + point.setColor(Color.rgb(250, 80, 80)); + point.setAlpha(230); + point.setAntiAlias(true); + point.setStyle(Style.FILL); + + pathForDirection = new Path(); + } + + public void initLayer(OsmandMapTileView view) { + this.view = view; + initUI(); + } + + + @Override + public boolean onTouchEvent(MotionEvent event) { + return false; + } + + + @Override + public void onDraw(Canvas canvas) { + if(pointToNavigate == null){ + return; + } + if (isLocationVisible()) { + int locationX = MapUtils.getPixelShiftX(view.getZoom(), pointToNavigate.getLongitude(), view.getLongitude(), + view.getTileSize()) + view.getWidth() / 2; + int locationY = MapUtils.getPixelShiftY(view.getZoom(), + pointToNavigate.getLatitude(), view.getLatitude(), view.getTileSize()) + view.getHeight() / 2; + + canvas.drawCircle(locationX, locationY, RADIUS, point); + } else { + Location.distanceBetween(view.getLatitude(), view.getLongitude(), pointToNavigate.getLatitude(), + pointToNavigate.getLongitude(), calculations); + float bearing = calculations[1]; + pathForDirection.reset(); + pathForDirection.moveTo(0, 0); + pathForDirection.lineTo(0.5f, 1f); + pathForDirection.lineTo(-0.5f, 1f); + pathForDirection.lineTo(0, 0); + float radiusBearing = DIST_TO_SHOW ; + Matrix m = new Matrix(); + m.reset(); + m.postScale(RADIUS * 2, RADIUS * 2); + m.postTranslate(0, -radiusBearing); + m.postTranslate(view.getWidth() / 2, view.getHeight() / 2); + m.postRotate(bearing, view.getWidth() / 2, view.getHeight() / 2); + pathForDirection.transform(m); + canvas.drawPath(pathForDirection, point); + } + } + + public boolean isLocationVisible(){ + if(pointToNavigate == null || view == null){ + return false; + } + return view.isPointOnTheMap(pointToNavigate.getLatitude(), pointToNavigate.getLongitude()); + } + + + public LatLon getPointToNavigate() { + return pointToNavigate; + } + + public void setPointToNavigate(LatLon pointToNavigate) { + this.pointToNavigate = pointToNavigate; + view.prepareImage(); + } + + @Override + public void destroyLayer() { + + } + + + + +}