(Android谷歌地图)当用户使用mapoverlay点击新位置时删除旧标记并放置新标记

时间:2011-11-01 07:58:37

标签: android google-maps

在我的应用中,我正在显示世界地图 当用户点击特定位置时,我会在其上附加标记。

但是,当用户点击其他位置时,我希望删除之前的标记并将其附加到新位置。

我已经检查了几个帖子,但无法提出任何解决方案。

以下是代码:

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {

        return false;

    }   
@Override
    public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = TpActivity.this.getResources().getDrawable(R.drawable.marker);
  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,TpActivity.this);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
 mapView.invalidate();

latitude=p.getLatitudeE6() / 1E6;
longitude=p.getLongitudeE6() /1E6 ;
        Toast.makeText(TpActivity.this, 
            p.getLatitudeE6() / 1E6 + "," + 
            p.getLongitudeE6() /1E6 , 
            Toast.LENGTH_SHORT).show();

        new AlertDialog.Builder(TpActivity.this)
        .setTitle("Change city")
        .setMessage("go to the new location?")
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        })
        .setPositiveButton("YES", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
             new newasync().execute();
            }
        }).show();
return true;
}

1 个答案:

答案 0 :(得分:1)

我使用了overlay类的draw函数而不是itemizedoverlay 这是代码:

Geopoint k;

在这里创建代码:

 mapview = (MapView) findViewById(R.id.mapView);
        mapview.setBuiltInZoomControls(true);
        List<Overlay> mapOverlays = mapview.getOverlays();
        MapOverlay mapOverlay = new MapOverlay();

       mapOverlays.add(mapOverlay);

重叠类:

 class MapOverlay extends com.google.android.maps.Overlay
{      


    @Override
    public boolean onTap(GeoPoint p, MapView mapView) {
        // TODO Auto-generated method stub
         Drawable drawable = TpActivity.this.getResources().getDrawable(R.drawable.marker);
        /* List<Overlay> mapOverlays = mapView.getOverlays();

            HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,TpActivity.this);


        OverlayItem overlayitem = new OverlayItem(p, "", "");

        itemizedoverlay.addOverlay(overlayitem);

        mapOverlays.add(itemizedoverlay);*/
         k = p;
         mc= mapView.getController();
        mc.animateTo(p);
        // Point pt=mapView.getProjection().toPixels(p, null);
         mapView.invalidate();
        latitude=p.getLatitudeE6() / 1E6;
        longitude=p.getLongitudeE6() /1E6 ;
                Toast.makeText(TpActivity.this, 
                    p.getLatitudeE6() / 1E6 + "," + 
                    p.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();

                new AlertDialog.Builder(TpActivity.this)
                .setTitle("Change city")
                .setMessage("go to the new location?")
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                     new newasync().execute();
                    }
                }).show();
        return true;
    }




    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   
         if(k!=null)
         {
        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(k, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.marker);            
        canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-34, null); 
         }
        return true;


    }