Удаление маркеров в mapquest android (очистка карты)
Я использую 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 ответ
Вы можете очистить карту, прежде чем ставить новые маркеры, используя
map.getController(). ясно ()
это очистит все на карте.
если вы хотите удалить маркер только тогда, вы должны создать маркерный объект, когда вы добавляете маркер на карту
Marker some_Marker = map.getController(). AddMarker (new MarkerOptions (). Icon (marker_icon).position (координаты);
И удалить маркер
map.getgetController (). removeMarker (some_Marker)