I did this a few years ago, I don't know if Osmdroid has changed in the mean time. I was overriding overlay
and its draw
method, inserting my bitmap.
As I said, this is old code, check with the current API before you use it.
public class MyLocationOverlay extends Overlay implements MyLocationListener {
private static final String TAG = MyLocationOverlay.class.getSimpleName();
private BoundedMapView mapView;
private Point point = new Point();
private Bitmap myLocationMarker;
private Bitmap haloMarker;
private Location myLocation;
private Bitmap bm;
private MyLocationManager locationManager;
public MyLocationOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy,
Context context, MapView mapView, MyLocationManager locationManager,
BitmapUtils bitmapUtils) {
super(pResourceProxy);
this.mapView = (BoundedMapView) mapView;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
myLocationMarker = bitmapUtils.rotateIcon(BitmapFactory.decodeResource(
context.getResources(), R.drawable.trackingdot, options));
haloMarker = BitmapFactory.decodeResource(context.getResources(),
R.drawable.trackingdothalo);
myLocation = locationManager.getMyLocation();
this.locationManager = locationManager;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
/*
* draw my location
*/
if (myLocation != null) {
try {
point = mapView.getProjection().toPixels(
new GeoPoint(myLocation.getLatitude(), myLocation.getLongitude()), point);
canvas.drawBitmap(myLocationMarker, point.x - (myLocationMarker.getWidth() / 2),
point.y - (myLocationMarker.getWidth() / 2), null);
int size = (int) mapView.getProjection().metersToEquatorPixels(
myLocation.getAccuracy());
if (bm != null) {
bm.recycle();
}
if (size > 40) {
bm = Bitmap.createScaledBitmap(haloMarker, size, size, false);
canvas.drawBitmap(bm, point.x - (bm.getWidth() / 2), point.y
- (bm.getHeight() / 2), null);
}
} catch (OutOfMemoryError e) {
Log.d(TAG, e.getMessage() == null ? "OutOfMemoryError" : e.getMessage());
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage() == null ? "IllegalArgumentException" : e.getMessage());
}
}
}
@Override
public void onLocationChanged(Location location) {
myLocation = location;
mapView.postInvalidate();
}
@Override
public void onProviderDisabled(String provider) {
if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)) {
myLocation = null;
}
}
@Override
public void onProviderEnabled(String provider) {
if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)) {
myLocation = locationManager.getMyLocation();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)
&& status == LocationProvider.AVAILABLE) {
myLocation = locationManager.getMyLocation();
}
}
@Override
public void onExitEnterPark(InParkStatus inParkStatus) {
}
}
Step 1: install osmonuspack ( https://github.com/MKergall/osmbonuspack/wiki/Tutorial_0 )
Step 2: download osmand and get icons from osmand;
Step 3: Change DirectedLocationOverlay icon:
myLocationOverlay = new DirectedLocationOverlay(this);
first choice, Single bitmap;
// 1-Single Bitmap Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.direction_arrow, null); Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
or second option, Blue Arrow;
// 2-Single LayerDrawable - Blue Arrow LayerDrawable ld = (LayerDrawable)getResources().getDrawable(R.drawable.map_navigation_default); final int width = ld.getIntrinsicWidth(); final int height = ld.getIntrinsicHeight(); final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); ld.setBounds(0, 0, width, height); ld.draw(new Canvas(bitmap));
or third option, similar to the icon above;
//3- Double layerdrawable Drawable bottom = ContextCompat.getDrawable(this, R.drawable.map_location_default_view_angle); Drawable top = ContextCompat.getDrawable(this, R.drawable.map_location_default); final LayerDrawable ld = new LayerDrawable(new Drawable[]{bottom, top}); final int width = ld.getIntrinsicWidth(); final int height = ld.getIntrinsicHeight(); final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); ld.setBounds(0, 0, width, height); ld.draw(new Canvas(bitmap));
change icon;
myLocationOverlay.setDirectionArrow(bitmap); map.getOverlays().add(myLocationOverlay);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.