删除mapquest android中的标记(清除地图)

时间:2013-07-18 07:06:52

标签: android mapquest

我在我的应用程序中使用mapquest android,我可以显示标记,但我希望每次只能显示两个标记,因此如果我已经显示了两个标记,则删除现有标记。 有谁知道我该怎么做? 这是我的代码

MainActivity.java

public class MainActivity extends NavigationIntentDemo {

    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
    ArrayList<GeoPoint> routeData = new ArrayList<GeoPoint>();

    JSONParser parser = new JSONParser();

    /**
     * Initialize the map.
     */
    @Override
    protected void init() {
        setupMapView(new GeoPoint(40.25f, 116.5f), 12);

        TouchOverlay overlay = new TouchOverlay();
        map.getOverlays().add(overlay);
    }


    /**
     * Construct a simple line overlay to display on the map and add a OverlayTapListener
     * to respond to tap events.
     * 
     */
        private void showLineOverlayWithPoints(ArrayList<GeoPoint> routeData) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.RED);
            paint.setAlpha(100);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth(10);

            LineOverlay line = new LineOverlay(paint);
            line.setData(routeData);
            line.setShowPoints(true, null);
            line.setKey("Line #2");

            line.setTouchEventListener(new LineOverlay.OverlayTouchEventListener() {            
                @Override
                public void onTouch(MotionEvent evt, MapView mapView) {
                    Toast.makeText(getApplicationContext(), "Line Touch!", Toast.LENGTH_SHORT).show();              
                }
            });
            this.map.getOverlays().add(line);
            this.map.invalidate();
        }   


    /**
     * Crude way to launch navigation to various locations based on clicking on the map.
     *
     */
    private class TouchOverlay extends Overlay { 

        @Override
        public boolean onTouchEvent(MotionEvent event, final MapView mapView) {

            Drawable icon = getResources().getDrawable(R.drawable.location_marker);
            DefaultItemizedOverlay poiOverlay = new DefaultItemizedOverlay(icon);

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                GeoPoint p = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY());
                Toast.makeText(getApplicationContext(), p.getLatitude() + "," + p.getLongitude(), Toast.LENGTH_LONG).show();


                if(points.size()<2) {
                points.add(p);



                // set GeoPoints and title/snippet to be used in the annotation view 
                poiOverlay.addItem(new OverlayItem(p, "blabla", "blabla"));

                if (points.size() == 2) {

                    routeData = parser.getRoute(    points.get(0).getLongitude(), 
                                                    points.get(0).getLatitude(), 
                                                    points.get(1).getLongitude(),
                                                    points.get(1).getLatitude(),
                                                    "SPS");

                    if (routeData != null)
                        showLineOverlayWithPoints(routeData);
                    else 
                        Log.e("ERROR","Route data error");

                } else exit(0);

               /* poiOverlay.setOnFocusChangeListener(new ItemizedOverlay.OnFocusChangeListener() {
                    @Override
                    public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
                        // when focused item changes, recenter map and show info
                        map.getController().animateTo(newFocus.getPoint());
                        Toast.makeText(map.getContext().getApplicationContext(), newFocus.getTitle() + ": " + 
                                newFocus.getSnippet(), Toast.LENGTH_SHORT).show();      
                    }           
                });*/

                map.getOverlays().add(poiOverlay);
                return true;

                }
                else {

                    points.clear();
                }   
            }

            return false;
        }

        private void exit(int i) {
            // TODO Auto-generated method stub

        }
    }
}

NavigationIntentDemo

public class NavigationIntentDemo extends MapActivity {

    protected MapView map; 
    protected MyLocationOverlay myLocationOverlay;
    protected Button followMeButton;

    /** 
     * Called when the activity is first created. 
     * 
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        followMeButton=(Button)findViewById(R.id.followMeButton);
        followMeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myLocationOverlay.setFollowing(true);
            }
        });

        setupMapView(new GeoPoint(40.25f, 116.5f), 12);
        setupMyLocation();

       init();
    }

    /**
     * Initialize the view.
     */
    protected void init() {
        this.setupMapView(new GeoPoint(40.25f, 116.5f), 12);
    }


    protected void setupMyLocation() {
        this.myLocationOverlay = new MyLocationOverlay(this, map);

        myLocationOverlay.enableMyLocation();
        myLocationOverlay.runOnFirstFix(new Runnable() {
            @Override
            public void run() {
                GeoPoint currentLocation = myLocationOverlay.getMyLocation(); 
                map.getController().animateTo(currentLocation);
                map.getController().setZoom(14);
                map.getOverlays().add(myLocationOverlay);
                myLocationOverlay.setFollowing(true);
            }
        });
    }

    /**
     * This will set up a basic MapQuest map with zoom controls
     */
    protected void setupMapView(GeoPoint pt, int zoom) {
        this.map = (MapView) findViewById(R.id.map);

        // set the zoom level
        map.getController().setZoom(zoom);

        // set the center point
        map.getController().setCenter(pt);

        // enable the zoom controls
        map.setBuiltInZoomControls(true);

    }

    /**
     * Get the id of the layout file.
     * @return
     */
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    /**
     * Utility method for getting the text of an EditText, if no text was entered the hint is returned
     * @param editText
     * @return
     */
    public String getText(EditText editText){
        String s = editText.getText().toString();
        if("".equals(s)) s=editText.getHint().toString();
        return s;
    }

    /**
     * Hides the softkeyboard
     * @param v
     */
    public void hideSoftKeyboard(View v){
        //hides soft keyboard
        final InputMethodManager imm = (InputMethodManager)getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }


}

谢谢

1 个答案:

答案 0 :(得分:0)

您可以先清除地图,然后再使用放置新标记

map.getController()。clear()

它将清除地图上的所有内容。

如果只想删除标记,则在地图上添加标记时必须创建标记对象

Marker some_Marker = map.getController()。addMarker(new MarkerOptions()。icon(marker_icon).position(coordinates);

并删除标记

map.getgetController()。removeMarker(some_Marker)