我正在使用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无法删除标记全部内容,希望文章能够帮你解决Android MapView无法删除标记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)