/**
* This is class for touch event ,when user touched the screen for
* enough time, user can tag the touched place as user's favorite
* place, or fix current position and set as destination.
* @author xinyan
*@date 2011-10-10
*/
class Touchy extends Overlay {
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
Log.v(TAG, "Touchy is touched…");
if (MotionEvent.ACTION_DOWN == e.getAction()) {
start = e.getEventTime();
x = (int) e.getX();
y = (int) e.getY();
touchPoint = mMapView.getProjection().fromPixels(x, y);
Log.v(TAG, "Touchy is touched.. and we get touch point.");
}
if (MotionEvent.ACTION_UP == e.getAction()) {
stop = e.getEventTime();
}
if (stop – start > 1500) {
OverlayItem overlayItem = new OverlayItem(touchPoint,
"Pined position", "A new position");
CustomPinpoint custom = new CustomPinpoint(marker,
StandardActivity.this);
custom.insertPinpoint(overlayItem);
mMapView.getOverlays().add(custom);
new AlertDialog.Builder(StandardActivity.this)
.setIcon(null)
.setTitle(R.string.whatYouWant)
.setSingleChoiceItems(
R.array.select_dialog_whatYouWant, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
if (0 == whichButton) {
// user clicked fix my current
// position choice
} else if (1 == whichButton) {
// user clicked set as destination
// choice
}
}
})
.setPositiveButton(R.string.positive,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
if (0 == whichButton) {
// user clicked fix my current
// position choice
} else if (1 == whichButton) {
// user clicked set as destination
// choice
} else if (3 == whichButton) {
// user clicked put into favorite
// choice
}
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
}).create().show();
} // end of if (stop – start > 1500)
else {
// I think this overrided method shouldn't capture all
// of the touch events, otherwise we can't control the map.
return false;
}
return true;
}
}
www.aiwalls.com
/**
* This is a custom class of pin point overlay,
* it means this overlay appears a marker when you click a place on
* the mapview. The StandardActivtiy will use it.
* @author xinyan
*@date 2011-10-9
*
*/
public class CustomPinpoint extends ItemizedOverlay<OverlayItem> {
private final ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;
public CustomPinpoint(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
}
public CustomPinpoint(Drawable marker, Context context) {
this(marker);
c = context;
}
@Override
protected OverlayItem createItem(int i) {
return pinpoints.get(i);
}
@Override
public int size() {
return pinpoints.size();
}
public void insertPinpoint(OverlayItem item) {
pinpoints.add(item);
this.populate();
}
}
說明下這裡所說的標記地點是當你長按地圖某個地方的時候,就生成一個新的氣泡標記它。
關鍵的地方就是Touchy這個Overlay子類,它實現瞭Touch事件的響應方法。我們隻要在onCreate方法裡面把Touchy的對象,也就是實現瞭Touch事件的響應方法的Overlay這一層加進MapView裡的Overlays裡面去,就可以實現事件的響應瞭、、當事件響應後我們就可以標記氣泡,然後取得地址、想幹嘛、幹嘛
貌似還有更簡單的方法、、那就是CustomPinpoint覆蓋ItemizedOverlay裡面的public boolean onTap(GeoPoint p, MapView mapView) 、、、沒試過、、如果是這樣就可以不用Touchy這個類瞭
作者:顏