gpt4 book ai didi

android - 确认对话框后如何将数据从类传递到 Activity ? (在 map View 上绘制)

转载 作者:行者123 更新时间:2023-11-30 04:00:17 24 4
gpt4 key购买 nike

我有一个扩展的覆盖类:

短代码:

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!");


}

我想在我的 map View 上绘制触摸点,它在这个 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 类中确认对话框后,我想标记触摸点。我想我错过了一些东西 - 我还应该做什么?

最佳答案

您需要有一个扩展 ItemizedOverlay 的类,它又可以包含一个 OverlayItem,它在其构造函数中采用 GeoPoint。有点像

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();
}

}

应该足够接近以适应。

关于android - 确认对话框后如何将数据从类传递到 Activity ? (在 map View 上绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12601175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com