Merge pull request #376 from maciekpapiez/master
Implemented a simple distance calculator plugin
This commit is contained in:
commit
63f5ca92b3
10 changed files with 198 additions and 0 deletions
BIN
OsmAnd/res/drawable-hdpi/map_pin_destination.png
Normal file
BIN
OsmAnd/res/drawable-hdpi/map_pin_destination.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
BIN
OsmAnd/res/drawable-hdpi/map_pin_origin.png
Normal file
BIN
OsmAnd/res/drawable-hdpi/map_pin_origin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
BIN
OsmAnd/res/drawable-large/map_pin_destination.png
Normal file
BIN
OsmAnd/res/drawable-large/map_pin_destination.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
BIN
OsmAnd/res/drawable-large/map_pin_origin.png
Normal file
BIN
OsmAnd/res/drawable-large/map_pin_origin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
BIN
OsmAnd/res/drawable-mdpi/map_pin_destination.png
Normal file
BIN
OsmAnd/res/drawable-mdpi/map_pin_destination.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
OsmAnd/res/drawable-mdpi/map_pin_origin.png
Normal file
BIN
OsmAnd/res/drawable-mdpi/map_pin_origin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
|
@ -1198,4 +1198,6 @@
|
|||
<string name="edit_filter_create_message">Filter {0} has been created</string>
|
||||
<string name="default_buttons_selectall">Select All</string>
|
||||
<string name="email">email</string>
|
||||
<string name="osmand_distance_plugin_description">Measure a distance between two points. Specify the origin / the destination / clear the measurement by a long press.</string>
|
||||
<string name="osmand_distance_plugin_name">Distance Calculator</string>
|
||||
</resources>
|
||||
|
|
|
@ -11,6 +11,7 @@ import net.osmand.plus.activities.MapActivity;
|
|||
import net.osmand.plus.activities.SettingsActivity;
|
||||
import net.osmand.plus.background.OsmandBackgroundServicePlugin;
|
||||
import net.osmand.plus.development.OsmandDevelopmentPlugin;
|
||||
import net.osmand.plus.distancecalculator.DistanceCalculatorPlugin;
|
||||
import net.osmand.plus.extrasettings.OsmandExtraSettings;
|
||||
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
|
||||
import net.osmand.plus.osmedit.OsmEditingPlugin;
|
||||
|
@ -69,6 +70,7 @@ public abstract class OsmandPlugin {
|
|||
installPlugin(OSMODROID_PLUGIN_COMPONENT, OsMoDroidPlugin.ID, app, new OsMoDroidPlugin(app));
|
||||
installedPlugins.add(new OsmEditingPlugin(app));
|
||||
installedPlugins.add(new OsmandDevelopmentPlugin(app));
|
||||
installedPlugins.add(new DistanceCalculatorPlugin(app));
|
||||
|
||||
Set<String> enabledPlugins = settings.getEnabledPlugins();
|
||||
for (OsmandPlugin plugin : installedPlugins) {
|
||||
|
|
|
@ -0,0 +1,143 @@
|
|||
package net.osmand.plus.distancecalculator;
|
||||
|
||||
import net.osmand.osm.LatLon;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.views.OsmandMapLayer;
|
||||
import net.osmand.plus.views.OsmandMapTileView;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.RectF;
|
||||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* @author Maciej Papiez
|
||||
*
|
||||
*/
|
||||
public class DistanceCalculatorLayer extends OsmandMapLayer {
|
||||
|
||||
private final MapActivity map;
|
||||
private OsmandMapTileView view;
|
||||
private OsmandSettings settings;
|
||||
|
||||
private LatLon originPoint = null;
|
||||
private LatLon destinationPoint = null;
|
||||
private Bitmap originIcon;
|
||||
private Bitmap destinationIcon;
|
||||
private Paint bitmapPaint;
|
||||
|
||||
|
||||
public DistanceCalculatorLayer(MapActivity map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initLayer(OsmandMapTileView view) {
|
||||
this.view = view;
|
||||
this.settings = ((OsmandApplication) map.getApplication()).getSettings();
|
||||
|
||||
originIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_origin);
|
||||
destinationIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_destination);
|
||||
|
||||
bitmapPaint = new Paint();
|
||||
bitmapPaint.setDither(true);
|
||||
bitmapPaint.setAntiAlias(true);
|
||||
bitmapPaint.setFilterBitmap(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas, RectF latlonRect, RectF tilesRect,
|
||||
DrawSettings settings) {
|
||||
drawIcon(canvas, originPoint, originIcon);
|
||||
drawIcon(canvas, destinationPoint, destinationIcon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a given map pin icon (source or destination point)
|
||||
* @param canvas to draw on
|
||||
* @param point the icon should be drawn at
|
||||
* @param icon to be drawn
|
||||
*/
|
||||
public void drawIcon(Canvas canvas, LatLon point, Bitmap icon) {
|
||||
if (point == null)
|
||||
return;
|
||||
|
||||
double latitude = point.getLatitude();
|
||||
double longitude = point.getLongitude();
|
||||
if (isLocationVisible(latitude, longitude)) {
|
||||
int marginY = icon.getHeight();
|
||||
int locationX = view.getMapXForPoint(longitude);
|
||||
int locationY = view.getMapYForPoint(latitude);
|
||||
canvas.rotate(-view.getRotate(), locationX, locationY);
|
||||
canvas.drawBitmap(icon, locationX, locationY - marginY, bitmapPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyLayer() {}
|
||||
|
||||
@Override
|
||||
public boolean drawInScreenPixels() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongPressEvent(PointF point) {
|
||||
if(originPoint == null) {
|
||||
originPoint = view.getLatLonFromScreenPoint(point.x, point.y);
|
||||
} else if(destinationPoint == null) {
|
||||
destinationPoint = view.getLatLonFromScreenPoint(point.x, point.y);
|
||||
|
||||
double startLatitude = originPoint.getLatitude();
|
||||
double startLongitude = originPoint.getLongitude();
|
||||
double endLatitude = destinationPoint.getLatitude();
|
||||
double endLongitude = destinationPoint.getLongitude();
|
||||
float[] results = new float[3];
|
||||
|
||||
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
|
||||
|
||||
(Toast.makeText(view.getContext(), "The distance = " + formatDistance(results[0]), Toast.LENGTH_SHORT)).show();
|
||||
|
||||
Log.i("SOMETAG", "Distance calculated: " + results[0]);
|
||||
} else {
|
||||
originPoint = null;
|
||||
destinationPoint = null;
|
||||
}
|
||||
|
||||
view.refreshMap();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a distance into String representation with correct unit
|
||||
* @param distance to be formatted (in meters)
|
||||
* @return the formatted String with the distance and unit (m or km)
|
||||
*/
|
||||
private String formatDistance(float distance) {
|
||||
if(distance < 5000)
|
||||
return String.format("%.2f m", distance);
|
||||
else
|
||||
return String.format("%.2f km", distance / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param latitude
|
||||
* @param longitude
|
||||
* @return true if the distance measurement point is located on a visible part of map
|
||||
*/
|
||||
private boolean isLocationVisible(double latitude, double longitude) {
|
||||
if(originPoint == null || view == null){
|
||||
return false;
|
||||
}
|
||||
return view.isPointOnTheRotatedMap(latitude, longitude);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package net.osmand.plus.distancecalculator;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandPlugin;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
|
||||
/**
|
||||
* @author Maciej Papiez
|
||||
*
|
||||
*/
|
||||
public class DistanceCalculatorPlugin extends OsmandPlugin {
|
||||
private static final String ID = "osmand.distance";
|
||||
private OsmandApplication app;
|
||||
private DistanceCalculatorLayer distanceCalculatorLayer;
|
||||
|
||||
public DistanceCalculatorPlugin(OsmandApplication app) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return app.getString(R.string.osmand_distance_plugin_description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return app.getString(R.string.osmand_distance_plugin_name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(OsmandApplication app) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerLayers(MapActivity activity) {
|
||||
// remove old if existing
|
||||
if(distanceCalculatorLayer != null) {
|
||||
activity.getMapView().removeLayer(distanceCalculatorLayer);
|
||||
}
|
||||
distanceCalculatorLayer = new DistanceCalculatorLayer(activity);
|
||||
activity.getMapView().addLayer(distanceCalculatorLayer, 8.5f);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue