1

我有一个扩展的覆盖类:

短代码:

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


}

我想在此活动中的地图视图上绘制触摸点:

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 类的对话框后,我想标记触摸点。我想我错过了传递一些东西 - 我还应该做什么?

4

1 回答 1

1

您需要有一个扩展 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();
    }

}

应该足够接近您以适应。

于 2012-09-26T16:18:38.333 回答