Android MapView无法删除标记
作者:互联网
我正在使用locationManager和ItemizedOverlay绘制“我的位置”标记,问题是当触发onLocationChanged时,我正在绘制新标记,而不是最后一个移到新位置,这是我的onLocationChanged代码:
public void onLocationChanged(Location location) {
myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
if(location!=null){
MyMap.invalidate();
GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
MyController.animateTo(MyPos);
object1.addPoint(MyPos,"Ma position","Ma position");
MyMap.getOverlays().add(object1);
}
}
您能帮我解决这个问题吗?
非常感谢.
MapActivity:
public class Main extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
MapView MyMap;
MapController MyController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyMap=(MapView)findViewById(R.id.MyGMap);
MyMap.setBuiltInZoomControls(true);
try {
getEventsFromAnXML(this);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int j=0;
myOverlay object = new myOverlay(getResources().getDrawable(R.drawable.marker),MyMap);
while( j<agencies.size()){
GeoPoint point = new GeoPoint(microdegrees(agencies.get(j).getLatitude()),microdegrees(agencies.get(j).getLongitude()));
String Tit;
String Des;
Tit=agencies.get(j).getTspTitle();
Des=agencies.get(j).getTspTitle() + "\nAgence: " + agencies.get(j).getTitle() +
"\nTél: " + agencies.get(j).getPhone();
object.addPoint(point,Tit,Des);
j=j+1;
}
MyMap.getOverlays().add(object);
MyController=MyMap.getController();
MyController.setZoom(12);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
private int microdegrees(double value){
return (int)(value*1000000);
}
boolean na=false;
List<Agency> agencies = new ArrayList<Agency>();
Agency agency=new Agency();
int i=0;
String TempTspTitle;
String TempTspPhone;
Boolean TempTspEnabled;
private String getEventsFromAnXML(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.hotels);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append("--- Start XML ---");
}
else if(eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equals("DataBase")){
String name=xpp.getAttributeValue(null, "name");
agency.setTspTitle(name);
TempTspTitle=name;
na=true;
}
if (xpp.getName().equals("DataBaseEnabled")){
xpp.next();
agency.setTspEnabled(Boolean.parseBoolean(xpp.getText()));
TempTspEnabled=Boolean.parseBoolean(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Title")){
xpp.next();
agency.setTitle(xpp.getText());
na=false;
xpp.nextTag();
}
if (xpp.getName().equals("Address")){
xpp.next();
agency.setAddress(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Phone") && na==true){
xpp.next();
agency.setTspPhone(xpp.getText());
TempTspPhone=xpp.getText();
xpp.nextTag();
}else{
if (xpp.getName().equals("Phone") && na==false){
xpp.next();
agency.setPhone(xpp.getText());
xpp.nextTag();
}
}
if (xpp.getName().equals("Fax")){
xpp.next();
agency.setFax(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("e-Mail")){
xpp.next();
agency.setMail(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Latitude")){
xpp.next();
agency.setLatitude(Double.parseDouble(xpp.getText()));
xpp.nextTag();
}
if (xpp.getName().equals("Longitude")){
xpp.next();
agency.setLongitude(Double.parseDouble(xpp.getText()));
}
}
else if(eventType == XmlPullParser.END_TAG)
{
if (xpp.getName().equals("DataBase") ){
agency = new Agency();
}else if (xpp.getName().equals("Agency")){
agencies.add(i,agency);
i=i+1;
agency = new Agency();
agency.setTspTitle(TempTspTitle);
agency.setTspPhone(TempTspPhone);
agency.setTspEnabled(TempTspEnabled);
}
}
eventType = xpp.next();
}
stringBuffer.append("\n" + "Size: " + agencies.size());
return stringBuffer.toString();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location location) {
myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
if(location!=null){
MyMap.getOverlays().remove(object1);
MyMap.invalidate();
GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
MyController.animateTo(MyPos);
object1.addPoint(MyPos,"Ma position","Ma position");
MyMap.getOverlays().add(object1);
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
和myOverlay类:
public class myOverlay extends ItemizedOverlay<OverlayItem>{
List<OverlayItem> points= new ArrayList<OverlayItem>();
private Drawable marker=null;
private Context c;
public myOverlay(Drawable marker, MapView mapView) {
super(marker);
this.marker=marker;
c=mapView.getContext();
}
@Override
protected OverlayItem createItem(int i) {
return (points.get(i));
}
public void draw(Canvas canvas,MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size() {
return points.size();
}
public void addPoint(GeoPoint point, String Titre, String Desc){
this.points.add(new OverlayItem(point,Titre,Desc));
populate();
}
public void removePoint(){
this.points.clear();
populate();
}
@Override
protected boolean onTap(int i){
Toast.makeText(c, points.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
return (true);
}
}
}
解决方法:
根据戴夫的建议,这解决了这个问题:
public void onLocationChanged(Location location) {
if(mMyOverlay == null) {
mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
MyMap.getOverlays().add(mMyOverlay);
}else{
MyMap.getOverlays().remove(mMyOverlay);
MyMap.invalidate();
mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
MyMap.getOverlays().add(mMyOverlay);
}
if(location!=null){
MyMap.invalidate();
GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
MyController.animateTo(MyPos);
mMyOverlay.addPoint(MyPos,"Ma position","Ma position");
}
谢谢..
标签:android-mapview,marker,android 来源: https://codeday.me/bug/20191023/1914259.html