enable go to point

git-svn-id: https://osmand.googlecode.com/svn/trunk@89 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-27 15:33:52 +00:00
parent c938d9eda4
commit 5e30a13588
7 changed files with 154 additions and 29 deletions

View file

@ -3,5 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/map_context_menu" android:menuCategory="container"><item android:id="@+id/map_show_settings" android:title="@string/settings_Button"></item>
<item android:id="@+id/map_show_location" android:title="@string/show_location"></item>
<item android:title="@string/navigate_to_point" android:id="@+id/map_navigate_to_point"></item>
</group>
</menu>

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="stop_navigation">Unmark location</string>
<string name="navigate_to_point">Mark location</string>
<string name="map_view_3d_descr">Enable 3D view of the map</string>
<string name="map_view_3d">Map View 3D</string>
<string name="rotate_map_to_bearing_descr">Rotate map to bearing of your direction</string>

View file

@ -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<Amenity> toFill){
public synchronized void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> 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<Amenity> toFill, boolean fillFound){
public synchronized boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill, boolean fillFound){
if (db == null) {
return true;
}

View file

@ -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);
@ -153,6 +155,17 @@ 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
@ -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);
}

View file

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

View file

@ -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());
}

View file

@ -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() {
}
}