Google地图标记错误

时间:2012-03-12 06:50:55

标签: android google-maps

我正在尝试将标记放在地图上并且我已经完成了但问题是它显示了所有标记。以前&最近也。我不想显示以前的标记,而只是想在最近点击的地方显示标记。不应该看到所有其他标记

我已经在下面的教程

的帮助下编写了代码

http://mobiforge.com/developing/story/using-google-maps-android

我的代码是

public class SelectLocation extends MapActivity {
MapView mapView; 
MapController mc;
GeoPoint p;
String coordinates[];


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

    private GeoPoint p; 
    public MapOverlay(GeoPoint p){
        this.p = p;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

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

    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());

            Toast.makeText(getBaseContext(), 
                    p.getLatitudeE6() / 1E6 + "," + 
                            p.getLongitudeE6() /1E6 , 
                            Toast.LENGTH_SHORT).show();
            mapView.getOverlays().add(new MapOverlay(p));
            Intent i=new Intent(SelectLocation.this,SetLocat.class);
            startActivity(i);

        }                            
        return false;
    }        

}
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectlocation);

    mapView = (MapView) findViewById(R.id.mapView);

    mapView.setBuiltInZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {"1.352566007", "103.78921587"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

    mc.animateTo(p);
    mc.setZoom(17);
    MapOverlay mapOverlay = new MapOverlay(p);


    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);        




}
/*protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}*/
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

1 个答案:

答案 0 :(得分:0)

在此代码之前

mapView.getOverlays().add(new MapOverlay(p)); 

试,

mapView.getOverlays().clear();
mapView.invalidate();
mapView.getOverlays().add(new MapOverlay(p));