如何从类确认对话框后,将数据传递给活动? (上MapView类图) [英] How to pass data from class to activity after confirming dialog? (drawing on mapview)
本文介绍了如何从类确认对话框后,将数据传递给活动? (上MapView类图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个扩展的覆盖类:
I have an extended overlay class:
短code:
public class MapOverlay extends Overlay {
private Context context;
private ProgressDialog dDialog;
Drawable drawable;
GeoPoint MainPoint;
MapView mapView;
public MapOverlay(Context context, MapView mapView)
{
this.context = context;
this.mapView = mapView;
}
@Override
public boolean onTap(GeoPoint p, MapView mapView)
{
this.MainPoint = p;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setPoint();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog.show();
return true;
}
public void setPoint()
{
OverlayItem overlayitem = new OverlayItem(MainPoint, "Hi!", "You touched this location!");
}
我要动用我的MapView,这是本次活动的触摸点:
I want to draw the touched point on my mapview, which is in this activity:
public class MyMapLocationActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
drawable = this.getResources().getDrawable(R.drawable.androidmarker);
MapOverlay myOverlay = new MapOverlay(this, mapView);
mapView.getOverlays().add(myOverlay);
mapView.postInvalidate();
}
我要标记的触摸点,确认从MapOverlay类对话框后。我觉得我失去了传递的东西 - 什么更多的应该怎么办
I want to mark the touched point, after confirming the dialog box from MapOverlay class. I think I'm missing to pass something - what more should I do?
推荐答案
您需要有延伸ItemizedOverlay,这反过来又可以持有OverlayItem,这需要一个GeoPoint对象在其构造函数的类。类似
You need to have a class which extends ItemizedOverlay, which in turn can hold an OverlayItem, which takes a GeoPoint in its constructor. Something like
public class MapDemo extends MapActivity implements OnTouchListener {
private Drawable mDrawable;
private ItemizedMapOverlay mItemizedOverlay;
private OverlayItem mOverlayitem;
private GeoPoint mClickedPoint = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GeoPoint mapCentrePoint = new GeoPoint(51500000, 0);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setOnTouchListener(this);
MapController mapCtrlr = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapCtrlr.setZoom(8);
mapCtrlr.setCenter(mapCentrePoint);
List<Overlay> mapOverlays = mapView.getOverlays();
mDrawable = this.getResources().getDrawable(R.drawable.icon);
mItemizedOverlay = new ItemizedMapOverlay(mDrawable, this);
mapOverlays.add(mItemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {return false;}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
final View fv = v;
AlertDialog.Builder dialog = new AlertDialog.Builder(v
.getRootView().getContext());
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
setPoint(fv);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.show();
// Can't show point till +ve button selected, so store it
mClickedPoint = ((MapView) v).getProjection()
.fromPixels((int)e.getX(), (int)e.getY());
}
return true;
}
void setPoint(View v) {
if (mClickedPoint != null) {
mOverlayitem = new OverlayItem(mClickedPoint, "test", "test2");
mOverlayitem.setMarker(mDrawable);
mItemizedOverlay.clear(); // clear last marker
mItemizedOverlay.addOverlay(mOverlayitem);
v.postInvalidate();
}
}
}
和
public class ItemizedMapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public ItemizedMapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public ItemizedMapOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {return mOverlays.get(i);}
@Override
public void draw(android.graphics.Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
@Override
public int size() { return mOverlays.size();}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
}
}
应该是足够接近你去适应。
should be close enough for you to adapt.
这篇关于如何从类确认对话框后,将数据传递给活动? (上MapView类图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文