Skip to content

Commit

Permalink
Merge pull request #3871 from salachi/TIMOB-11845-MapView
Browse files Browse the repository at this point in the history
TIMOB-11845-Capture longpress using dispatchTouchEvent in native MapView
  • Loading branch information
pingwang2011 committed Feb 21, 2013
2 parents a9ecc4c + 877288e commit a850c18
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ class LocalMapView extends MapView
private int lastLongitudeSpan;
private boolean requestViewOnScreen = false;
private View view;

//Long click related variables
private static final int MIN_MILLISECONDS_FOR_LONG_CLICK = 800;
private static final float X_TOLERANCE=10;//x pixels that your finger can be off but still constitute a long press
private static final float Y_TOLERANCE=10;//y pixels that your finger can be off but still constitute a long press

private long longClickStartTime = 0;
private float longClickXCoordinate;
private float longClickYCoordinate;

public LocalMapView(Context context, String apiKey)
{
super(context, apiKey);
Expand All @@ -118,13 +126,70 @@ public void setScrollable(boolean enable)
{
scrollEnabled = enable;
}

@Override
public boolean onTouchEvent(android.view.MotionEvent ev)
{
int actionType = ev.getAction();

// Handle LongPress
if (proxy.hierarchyHasListener(TiC.EVENT_LONGPRESS)) {
if (actionType == MotionEvent.ACTION_DOWN) {
// Save the values to use in ACTION_MOVE and ACTION_UP
longClickStartTime = ev.getEventTime();
longClickXCoordinate = ev.getX();
longClickYCoordinate = ev.getY();
} else if (actionType == MotionEvent.ACTION_MOVE) {
if (ev.getPointerCount() > 1) {
// Multitouch
longClickStartTime = 0;
} else {
float xmove = ev.getX();
float ymove = ev.getY();
// See if the movement is within the tolerance boundary
float xlow = longClickXCoordinate - X_TOLERANCE;
float xhigh = longClickXCoordinate + X_TOLERANCE;
float ylow = longClickYCoordinate - Y_TOLERANCE;
float yhigh = longClickYCoordinate + Y_TOLERANCE;
if ((xmove < xlow || xmove > xhigh) || (ymove < ylow || ymove > yhigh)) {
// Out of range
longClickStartTime = 0;
}
}

} else if (actionType == MotionEvent.ACTION_UP) {
// determine if this was a long click:
long eventTime = ev.getEventTime();
long downTime = ev.getDownTime();// This should match longClickStartTime unless we reset it earlier
if (longClickStartTime == downTime) {
// See if it is within the threshhold
if ((eventTime - longClickStartTime) > MIN_MILLISECONDS_FOR_LONG_CLICK) {
// Is it within the boundary
float xup = ev.getX();
float yup = ev.getY();
float xlow = longClickXCoordinate - X_TOLERANCE;
float xhigh = longClickXCoordinate + X_TOLERANCE;
float ylow = longClickYCoordinate - Y_TOLERANCE;
float yhigh = longClickYCoordinate + Y_TOLERANCE;
if ((xup > xlow && xup < xhigh) && (yup > ylow && yup < yhigh)) {
// Treat it as a long press
proxy.fireEvent(TiC.EVENT_LONGPRESS, dictFromEvent(ev));
}
}
}
}
}

return super.onTouchEvent(ev);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
if (!scrollEnabled && ev.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}


return super.dispatchTouchEvent(ev);
}
Expand Down Expand Up @@ -1083,6 +1148,14 @@ private int scaleToGoogle(double value)
{
return (int)(value * 1000000);
}

@Override
protected boolean allowRegisterForTouch()
{
// Skip TiUIView registration.
// Handled inside the LocalMapView as it is not working in the TiUIView
return false;
}
}


7 changes: 5 additions & 2 deletions apidoc/Titanium/Map/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ description: |
</mobileweb>
extends: Titanium.UI.View
excludes: {
events: [ 'singletap', 'doubletap', 'dblclick', 'longclick', 'longpress', 'pinch',
events: [ 'singletap', 'doubletap', 'dblclick', 'longclick', 'pinch',
'swipe', 'touchstart', 'touchend', 'touchcancel', 'touchmove', 'twofingertap' ]
}
since: "0.8"
Expand Down Expand Up @@ -261,7 +261,10 @@ events:
- name: loading
summary: Fired when the map begins loading.
platforms: [iphone, ipad, mobileweb]


- name: longpress
since: { android: 3.1.0 }

- name: pinchangedragstate
summary: Fired when the user interacts with a draggable annotation.
properties:
Expand Down

0 comments on commit a850c18

Please sign in to comment.