user1351659
user1351659

Reputation: 54

Google maps with overlays

I have an app that uses google map. I also have overlays on the map.

    public class OverLayClass extends ItemizedOverlay<OverlayItem>  
{
    private Drawable marker = null;
    private OverlayItem inDrag = null;
    private ImageView dragImage =null;
    private int xDragImageOffset, yDragImageOffset, xDragTouchOffset, yDragTouchOffset;

    public OverLayClass(Drawable marker) 
    {
        super(boundCenterBottom(marker));
        this.marker = marker;
        overlayItems = new ArrayList<OverlayItem>();

        // image to show when you drag pointer on screen to other location
        dragImage = (ImageView)findViewById(R.id.drag_image);

//          if(dragImage != null && dragImage.getDrawable() != null)
        {
            xDragImageOffset =     getResources().getDrawable(R.drawable.map_icon_corn).getIntrinsicWidth() / 2;
            yDragImageOffset =     getResources().getDrawable(R.drawable.map_icon_corn).getIntrinsicHeight();
        }
        populate();
    }

I have implemented all overlays using this class. I am also showing the user's current location using the same overlay class.

The onTouch for this overlay class is:

public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) 
    {
        //System.out.println(" --- inside onTouchEvent --- "+dontAddtap);
          final int x = (int)motionEvent.getX();
          final int y = (int)motionEvent.getY();
          boolean result = false;

        // TODO Auto-generated method stub
//               if(dontAddtap) 
             {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN && !isActionDown)
                {
                    //System.out.println(" ------ on key down ------ geo point "+pt1);
                    if(!isTap) 
                    {
                         for(OverlayItem item : overlayItems) 
                         {
                             Point p = new Point();

                              mapView.getProjection().toPixels(item.getPoint(), p);
                              item.setMarker(marker);
                              boolean isHit = hitTest(item, marker, x - p.x, y - p.y);
                              //System.out.println(" --- isHit in MotionEvent.ACTION_DOWN --- "+isHit);
                              if(isHit) 
                              {
                                  isItemHit = true;
                                  //System.out.println(" --- item hit ---- ");

                                    result = true;
                                    inDrag = item;

                                    overlayItems.remove(inDrag);
                                    gotGeoPoint = item.getPoint();
                                    //System.out.println( " -------- imporntent--item--otuched point -------- "+item.getPoint());
                                    populate();

                                    xDragTouchOffset = 0;
                                    yDragTouchOffset = 0;

                                    setDragImagePosition(p.x, p.y);
                                    dragImage.setVisibility(View.VISIBLE);
                                   //   Point p11=new Point(p.x, p.y);
                                    xDragTouchOffset = x - p.x;
                                    yDragTouchOffset = y - p.y;

                                    OverlayItemArryList.remove(item);
                                   // 
                                   mapController_.stopAnimation(true);
                                   break;
                              }
                        }                       
                    }
                }
            }

         if(dontAddtap && isItemHit) 
         {
            if(motionEvent.getAction() == MotionEvent.ACTION_MOVE && inDrag != null)
            {
                //System.out.println(" --- onTouchEvent ACTION_MOVE --- ");
                setDragImagePosition(x, y);

                result = true;
                isTap = isActionDown = false;
            }            
         }

         if(dontAddtap || isItemHit) 
         {
            if(motionEvent.getAction() == MotionEvent.ACTION_UP && inDrag != null)
            {
                //System.out.println(" --- onTouchEvent ACTION_UP --- ");

                if(isItemHit)
                {
                   dragImage.setVisibility(View.GONE);

                   GeoPoint pt = mapView.getProjection().fromPixels(x-xDragTouchOffset,y-yDragTouchOffset);

                    toDrop = new OverlayItem(pt, inDrag.getTitle(), inDrag.getSnippet());

                    overlayItems.add(toDrop);

                    //System.out.println("------------- on drag drop point -------------- "+toDrop.getPoint());

                    addGeo = toDrop.getPoint(); 

                    //addReferenceToOverLayList.add(toDrop);
                    ////////System.out.println(" addReferenceToOverLayList.add "+toDrop.getPoint());
                    Iterator<GeoPoint> st = geoArrayist.iterator();
                    // update array list with new values when any of the coordinate is moved
                    while(st.hasNext()) 
                    {
                        int k = 0;
                        GeoPoint check = st.next();
                        if(check.equals(gotGeoPoint)) 
                        {
                            //////System.out.println("yes we got geo points");
                            k = geoArrayist.indexOf(gotGeoPoint);
                            //////System.out.println(" - item index got from the array list - "+k);
                            geoArrayist.set(k, addGeo);
                            //////System.out.println(" --- updated array --- "+geoArrayist);
                        }                       
                    }
                    dontAddtap = result = true;
                    isItemHit = isActionDown = false;
                    populate();
                }
            }               
         }
        return(result || super.onTouchEvent(motionEvent, mapView));
    }   

And I am able to drag the markers anywhere on the map without any problem. However, this has also caused my current location pointer to move (if the user tries to move it). I just want to use the same overlay class but I want to fix the pointer to my current location and it should not be movable. Is there any way to find which overlay item is being touched? Or any other workaround would be of great help. Thanks.

Upvotes: 0

Views: 545

Answers (2)

binW
binW

Reputation: 13712

Simplest method would be to subclass your class OverlayClass and disable movement in it.

UPDATE:

Subclass OverlayClass and override its onTouchEvent() function. In this function get the coordinates of the event and do a hit test with all overlay items in this overlay. If no item is hit, then pass this event to parent class using super.onTouchEvent(), otherwise dont do anything and just return true. Now use your original class for all overlays except for location overlay and use this subclass for location overlya.

BTW you can also use built in MyLocationOverlay class to show your(user's) location on the map without any additional hassle. For this you do the same i.e use your OverlayClass for everything except for location. For location use MyLocationOverlay built in class.

Upvotes: 1

Andy
Andy

Reputation: 123

I think you have to put logic in onTouch event to handle your scenario look into this might be helpfulTutorial 1 Tutorial 2

Upvotes: 0

Related Questions