anmpout / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

ItemizedOverlay<OverlayItem>:onTap on called when tapping #245

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using osmdroid-android-3.0.5.jar‬ to embed OSMap in my app.
What steps will reproduce the problem?
1. Open the app.
2. Tap an item.
3. onTap(index i) not called

What is the expected output? What do you see instead?
breakpoint inside onTap nor reached.

What version of the product are you using? On what operating system?
2.2 emulator and also device - samsung galaxy

Please provide any additional information below.
same problem with osmdroid-android-3.0.4.jar

code:
MapActivity:

       MapView mapView = new MapView(this, 256);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);

        mapView.getController().setZoom(10);
        mapView.getController().setCenter(new GeoPoint(39.461078, 2.856445));

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.icon);

        MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,this);
        itemizedoverlay.setEnabled(true);

        double lat = 39.461078;
        double lng = 2.856445;

        GeoPoint homePoint = new GeoPoint(lat,lng);
        OverlayItem overlayitem = new OverlayItem("Home Sweet Home", "name", homePoint);

        mapView.getController().setZoom(18);
        mapView.getController().setCenter(homePoint);

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);

        setContentView(mapView);

MapItemizedOverlay:

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
     private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
     private Context mContext;

     public MapItemizedOverlay(Drawable defaultMarker, Context context)
     {
         //super(boundCenterBottom(defaultMarker));
         super(defaultMarker, new DefaultResourceProxyImpl(context));
         mContext = context;
     }

     public void addOverlay(OverlayItem overlay)
     {
         mOverlays.add(overlay);
         populate();
     }
     @Override
     protected OverlayItem createItem(int i)
     {
         return mOverlays.get(i);
     }
     @Override
     public int size()
     {
         return mOverlays.size();
     }

     protected boolean onTap(int index)
     {
         OverlayItem item = mOverlays.get(index);
         AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
         dialog.setTitle(item.getTitle());
         dialog.setMessage(item.getSnippet());
         dialog.show();
         return true;
    }

     //boolean onTap(GeoPoint p, MapView mapView)
     //{
    //   return false;
     //}

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

}

Original issue reported on code.google.com by erezbars...@gmail.com on 27 Jul 2011 at 9:17

GoogleCodeExporter commented 9 years ago
Doesn't work for me either.
Is there a workaround or another way on how to execute methods for each marker?

Original comment by reinhard...@gmail.com on 9 Aug 2011 at 9:26

GoogleCodeExporter commented 9 years ago
Same here. Just ported an app from Google Maps to osmdroid, and the onTap 
doesn't work. I use it a lot on driving directions, to tap on the turn markers 
to get some text info in a popup window.

In my original code, I'm using the @Override annotation in the onTap method. In 
osmdroid, I can't. That method doesn't exist.

Original comment by rui.mtd....@gmail.com on 18 Aug 2011 at 2:35

GoogleCodeExporter commented 9 years ago
doesntr work for me teither

Original comment by azq...@gmail.com on 27 Aug 2011 at 8:51

GoogleCodeExporter commented 9 years ago
Hi. I've the same issue. anybody knows how to solve it?

Original comment by zero...@gmail.com on 11 Sep 2011 at 6:23

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I have the same problem with that. I think they change one method by the other 
one (onSnapToItem by onTap).

Original comment by javi.xanxez@gmail.com on 25 Oct 2011 at 9:54

GoogleCodeExporter commented 9 years ago
Why it says:

    @Override
    public boolean onSnapToItem(int pX, int pY, Point pSnapPoint, MapView pMapView) {
        // TODO Implement this!
        return false;
    }

TODO?

Original comment by kako1...@gmail.com on 27 Oct 2011 at 9:16

GoogleCodeExporter commented 9 years ago
'up' !

I'm porting an application that uses an old snapshot of osmdroid, and it tries 
to override onTap() in an ItemizedOverlay<> class. But since r417 
(http://code.google.com/p/osmdroid/source/detail?spec=svn417&r=417 ), this 
method has been removed.

It is still documented in 4.0 docs of Android API:

http://code.google.com/android/add-ons/google-apis/reference/com/google/android/
maps/ItemizedOverlay.html

Is this a bug ? Maybe there is a known workaround on how to get the same 
behavior, but I could not find it after searching different 
forums/groups/source code.

Any help very appreciated :)

Original comment by marc.pou...@gmail.com on 20 Feb 2012 at 12:15

GoogleCodeExporter commented 9 years ago
Has anyone solved this?

Original comment by jyee.dis...@onescreen.com on 2 May 2012 at 10:44

GoogleCodeExporter commented 9 years ago
Hi,

I also got this problem some weeks ago... Finally, I solved it using 
ItemizedOverlayWithFocus class and its onItemSingleTapUp method.

Original comment by 90.se...@gmail.com on 2 May 2012 at 10:58

GoogleCodeExporter commented 9 years ago
sergi, can you post an example of using ItemizedOverlayWithFocus within a 
class? Thanks!

Original comment by siile...@gmail.com on 9 May 2012 at 3:01

GoogleCodeExporter commented 9 years ago
I encountered exactly the same issue. And this is probably the only real issue 
when porting an app from GoogleAPI to osmdroid. So solutions should be 
documented well. Here is my modest contribution. 

First of all, look at ItemizedIconOverlay, as it does most of what we want. 
After struggling a lot trying to sub-class ItemizedIconOverlay with my custom 
onSingleTapUpHelper, I finally made a class containing ItemizedIconOverlay. 
Applied to your implementation, it looks like this:

public class MyOwnItemizedOverlay {
    protected ItemizedIconOverlay<OverlayItem> mOverlay;
    protected Context mContext;
    protected Drawable mMarker;

    public MyOwnItemizedOverlay(Drawable marker, Context context) {
        mContext = context;
        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        ResourceProxy resourceProxy = (ResourceProxy) new DefaultResourceProxyImpl(mContext);
        mMarker = marker;

        mOverlay = new ItemizedIconOverlay<OverlayItem>(
                items, mMarker, 
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
            @Override public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                return onSingleTapUpHelper(index, item);
            }

            @Override public boolean onItemLongPress(final int index, final OverlayItem item) {
                return true;
            }
        }, resourceProxy);

    }

    public boolean onSingleTapUpHelper(int i, OverlayItem item) {
        //Toast.makeText(mContext, "Item " + i + " has been tapped!", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }

    public void addItem(OverlayItem item){
        mOverlay.addItem(item);
    }

    public ItemizedIconOverlay<OverlayItem> getOverlay(){
        return mOverlay;
    }
}

Using this class is nearly identical to using a sub-class of ItemizedOverlay 
(as you did in your MapActivity), except that adding the overlay to the map is 
done this way: 
    mapView.getOverlays().add(itemizedoverlay.getOverlay());

Note that you can also easily implement onItemLongPress, if desired. 

Original comment by www.trad...@free.fr on 16 May 2012 at 2:32

GoogleCodeExporter commented 9 years ago
Thanks man, that worked out just awesomely! :)

Original comment by siile...@gmail.com on 17 May 2012 at 2:12

GoogleCodeExporter commented 9 years ago
See also issue 343

Original comment by neilboyd on 17 May 2012 at 2:18

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Guys, 
Find below a very simple and "regular" implementation, as MyOwnItemizedOverlay 
is now a "true" overlay. 

class MyOwnItemizedOverlay extends ItemizedIconOverlay<OverlayItem> {
    protected Context mContext;

    public MyItemizedOverlay(final Context context, final List<OverlayItem> aList) {
         super(context, aList, new OnItemGestureListener<OverlayItem>() {
            @Override public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                    return false;
            }
            @Override public boolean onItemLongPress(final int index, final OverlayItem item) {
                    return false;
            }
          } );
      mContext = context;
    }

    @Override protected boolean onSingleTapUpHelper(final int index, final OverlayItem item, final MapView mapView) {
        //Toast.makeText(mContext, "Item " + index + " has been tapped!", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
}

And to use it in your activity:
ArrayList<OverlayItem> list = new ArrayList<OverlayItem>();
MyOwnItemizedOverlay<OverlayItem> overlay = new MyOwnItemizedOverlay(this, 
list);
mapView.getOverlays.add(overlay);

Original comment by mathieu....@gmail.com on 23 Jun 2012 at 8:30

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks for sharing matheu.  The constructor name is wrong in comment 16 above, 
it should be public MyOwnItemizedOverlay(...)

Original comment by carlos...@gmail.com on 21 Aug 2012 at 4:36

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This issue should be closed. We have a samples app that covers handling item 
clicks.

If there is any issue here it is that we aren't implementing an onTap() method 
as the Google Maps API does (how did that happen?).

Original comment by kurtzm...@gmail.com on 25 Jan 2013 at 7:59

GoogleCodeExporter commented 9 years ago
Issue 343 has been merged into this issue.

Original comment by kurtzm...@gmail.com on 25 Jan 2013 at 7:59

GoogleCodeExporter commented 9 years ago
Issue 225 has been merged into this issue.

Original comment by kurtzm...@gmail.com on 12 Apr 2013 at 3:33

GoogleCodeExporter commented 9 years ago
In r1209 we have added onTap and OnFocusChangedListenerSupport.

Original comment by kurtzm...@gmail.com on 12 Apr 2013 at 3:39

GoogleCodeExporter commented 9 years ago
Hi kurtzmarc,

If we want to catch a tap event at an overlay, what's the difference between 
new way with ItemizedOverlay's onTap and old way with ItemizedIconOverlay's 
onSingleTapUpHelper ?
Personally I extend ItemizedIconOverlay and use helper methods for both single 
tap and long press.
Now we can catch the tap event with 2 ways?

Thanks

Original comment by devemu...@gmail.com on 12 Apr 2013 at 7:30

GoogleCodeExporter commented 9 years ago
ItemizedIconOverlay is a concrete implementation of the ItemizedOverlay - it 
really belongs in the samples app because it's more of an example of how you 
can implement ItemizedOverlay. It could probably be rewritten to take advantage 
of onTap().

It's up to you which one you use. It looks like the ItemizedIconOverlay does 
not take the "hotspot" into account, so you may have better results with 
onTap().

Original comment by kurtzm...@gmail.com on 12 Apr 2013 at 11:47