-->

adding Overlay to a MapView in osmdroid

2020-04-05 07:50发布

问题:

I am writing a simple android application using osmdroid that work online an offline untill now I am able to display the map (online and offline), and I want to add overlay (markers) on the map, I had search for simple example for using overlay in osmdroid and I tried some of them but they didn't work, so I want any example for adding overlay or marcker using osmdroid

回答1:

Look at the ItemizedIconOverlay class.

There's a few examples on the internet if you search, an example has been posted on Stack Overflow here: Adding Overylay to OSMDROID



回答2:

public class mapcode extends Activity {
    globalvar appState;
    int stats=0;
    private MapView mapView;
    private IMapController mapController;
    private SimpleLocationOverlay mMyLocationOverlay;
    private ScaleBarOverlay mScaleBarOverlay;  
    ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
    DefaultResourceProxyImpl resourceProxy;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.map);

        appState = ((globalvar) getApplicationContext());





        mapView = (MapView) this.findViewById(R.id.mapview);  
        mapView.setTileSource(TileSourceFactory.MAPNIK);
      //  mapView.setBuiltInZoomControls(true); //кнопка ZOOM +-
        mapView.setMultiTouchControls(true);

        mapController = this.mapView.getController();
        mapController.setZoom(2);

        this.mMyLocationOverlay = new SimpleLocationOverlay(this);                          
        this.mapView.getOverlays().add(mMyLocationOverlay);

        this.mScaleBarOverlay = new ScaleBarOverlay(this);                          
        this.mapView.getOverlays().add(mScaleBarOverlay);


        /////////////////
        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        GeoPoint  currentLocation = new GeoPoint(55.860863,37.115046); 
        GeoPoint  currentLocation2 = new GeoPoint(55.8653,37.11556); 
        OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation);
        Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);

        final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(myLocationOverlayItem);



      myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation2);
     myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);


        items.add(myLocationOverlayItem);



        currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        return true;
                    }
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        return true;
                    }
                }, resourceProxy);
        this.mapView.getOverlays().add(this.currentLocationOverlay);

mapView.invalidate(); // для того чтобы маркеры появились }