问题
My App is using a local service, that provides changing locations. These locations are added to my ArrayList:
static List<Location> weg = new ArrayList<Location>();
The WegAnsehenActivity
now shows the path connecting the points.
My goal: I want to change the icon on the path for the last current point
completion: I want to change the green arrow on the last position
My problem: how can I achieve this ?
import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
.
.
.
public class WegAnsehenActivity extends Activity {
MapView mapView;
IMapController mapController;
MyLocationNewOverlay o;
Paint paint;
PathOverlay overlay;
//filled by the local service, method onLocationChanged
static List<Location> weg = new ArrayList<Location>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
mapController = mapView.getController();
//false keeps the mapView from loading online tiles using network connection!!!
mapView.setUseDataConnection(false);//true);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(7);
paint.setColor(Color.BLUE);
overlay = new PathOverlay(Color.BLUE, this);
overlay.setPaint(paint);
o = new MyLocationNewOverlay(getApplicationContext(), mapView);
}
@Override
protected void onResume() {
super.onResume();
mapController.setZoom(16);
if (weg.size() >= 1) {
for (int i = 0; i < weg.size(); i++) {
if (i==weg.size()-1) {
mapController.setCenter(new GeoPoint(weg.get(i).getLatitude(), weg.get(i).getLongitude()));
}
GeoPoint point = new GeoPoint(weg.get(i));
overlay.addPoint(point);
}
mapView.getOverlayManager().add(overlay);
o.enableMyLocation(); //shows a green arrow I want to replace
mapView.getOverlays().add(o);
mapView.postInvalidate();
}
}
}
Please help!
Regards Wicki
回答1:
What you can do is sub-class MyLocationNewOverlay, and set your own bitmaps (mPersonBitmap and mDirectionArrowBitmap) in your constructor.
Or you can try to use this constructor:
public MyLocationNewOverlay(IMyLocationProvider myLocationProvider,
MapView mapView, ResourceProxy resourceProxy)
and define your own "ResourceProxy".
回答2:
Thanks for the advices.
Now I have found a solution.
I have commented out the instruction "o.enableMyLocation();", that is responsible for standard icon.
It follows the replacement for the last 4 instructions:
mapView.getOverlayManager().add(overlay);
// o.enableMyLocation(); //shows the green-arrow-marker (my own position)
mapView.getOverlays().add(o);
setMarker(); //set an own marker
mapView.postInvalidate();
.
.
void setMarker() {
Drawable marker = getResources().getDrawable(R.drawable.icon);
ItemizedIconOverlay<OverlayItem> overlay = new ItemizedIconOverlay<OverlayItem>(
new ArrayList<OverlayItem>(), marker, null,
new DefaultResourceProxyImpl(this));
// gc: last GeoPoint
OverlayItem item = new OverlayItem(null, null, gc);
overlay.addItem(item);
mapView.getOverlays().add(overlay);
}
Regards Wicki
来源:https://stackoverflow.com/questions/32669305/osmdroid-set-a-custom-icon-in-a-mapview