/**
* 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;
}
}
/**
* 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这个类了