Adding ItemizedOverlay to my mapview

aTa picture aTa · May 13, 2012 · Viewed 7.1k times · Source

i am develpoing a map application with osm. i want to create some marker on my mapview. im using the folowwing code:

this.mMyLocationOverlay = new ItemizedOverlay<OverlayItem>(this, items,
                new ItemizedOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        Toast.makeText(
                                SampleWithMinimapItemizedoverlay.this,
                                "Item '" + item.mTitle + "' (index=" + index
                                        + ") got single tapped up", Toast.LENGTH_LONG).show();
                        return true; // We 'handled' this event.
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        Toast.makeText(
                                SampleWithMinimapItemizedoverlay.this,
                                "Item '" + item.mTitle + "' (index=" + index
                                        + ") got long pressed", Toast.LENGTH_LONG).show();
                        return false;
                    }
                }, mResourceProxy);

...

but in line 2 under the "new ItemizedOverlay.OnItemGestureListener" i get a red line and an error that says fix project setup. i am very confiused. why i get this error in eclipse? please help me

Answer

Ahyle Blue picture Ahyle Blue · May 14, 2012

You can use something like that. - extend ItemizedOverlay

package dispatch.driver.osmMaps;

import java.util.ArrayList;

import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.graphics.Point;

public class OsmMapsItemizedOverlay extends ItemizedIconOverlay<OverlayItem>
{
    private ArrayList<OverlayItem> mItemList = new ArrayList<OverlayItem>();

    public OsmMapsItemizedOverlay(ArrayList<OverlayItem> pList,
            ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener, ResourceProxy pResourceProxy)
    {
        super(pList, pOnItemGestureListener, pResourceProxy);
        mItemList = pList;
        // TODO Auto-generated constructor stub
    }

    public void addOverlay(OverlayItem aOverlayItem)
    {
        mItemList.add(aOverlayItem);
        populate();
    }

    public void removeOverlay(OverlayItem aOverlayItem)
    {
        mItemList.remove(aOverlayItem);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i)
    {
        return mItemList.get(i);
    }

    @Override
    public int size()
    {
        if (mItemList != null)
            return mItemList.size();
        else
            return 0;
    }

    @Override
    public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3)
    {
        // TODO Auto-generated method stub
        return false;
    }

}

And in your activity class call

private void PrepareItemizedOverlay()
    {
        /* itemized overlay */
        mItemizedOverlay = new OsmMapsItemizedOverlay(mItems,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>()
                {
                    @Override
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item)
                    {

                        Toast.makeText(OsmMapsActivity.this,
                                "Item '" + item.mTitle + "' (index=" + index + ") got single tapped up",
                                Toast.LENGTH_LONG).show();

                        return true; // We 'handled' this event.
                    }

                    @Override
                    public boolean onItemLongPress(final int index, final OverlayItem item)
                    {

                        Toast.makeText(OsmMapsActivity.this,
                                "Item '" + item.mTitle + "' (index=" + index + ") got long pressed", Toast.LENGTH_LONG)
                                .show();

                        return true;
                    }
                }, mResourceProxy);
    }

to add a new overlay you can use something like this:

{    OverlayItem overlayItem;


            overlayItem = new OverlayItem("Center", "Center", (GeoPoint) aPoint);



            mItemizedOverlay.addOverlay(overlayItem);

            mMapView.getOverlays().clear();
            mMapView.getOverlays().add(mItemizedOverlay);
            mMapView.getController().animateTo(aPoint);
}