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);
}
}
}
I'm using locationManager and ItemizedOverlay to draw My Location marker, the problem is when onLocationChanged is triggered I'm getting new marker drawed and not the last one moved to the new location, here is my onLocationChanged code :
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);
}
}
Can you please help me solve this issue ?
Thanks a lot.
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
}
and myOverlay Class :
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据戴夫的建议,这解决了问题:
谢谢..
according to Dave's suggestion this fixed the problem :
thanks..
getOverlays()
返回 MapView 中的 Overlay 对象列表。每次更新位置时,您的代码都会向此列表添加另一个覆盖层。您需要做的是删除以前的覆盖,或者每次都完全清空列表。因此,在添加新叠加层之前,请在您的方法中调用
getOverlays().clear()
。编辑,或者这可能会为您指明正确的方向?我不知道你的 myOverlay 类到底是如何工作的,所以希望你能填补空白!
getOverlays()
returns the List of Overlay objects in your MapView. Each time the location is updated, your code is adding another overlay to this list. You need to do is remove the previous overlay, or empty the list entirely each time.So, in your method call
getOverlays().clear()
before adding your new overlay.Edit, alternatively maybe this will point you in the right direction? I don't know exactly how your myOverlay class works, so hopefully you can fill in the blanks!
使用 ItemizedOverlay,并按照 Dave 的建议,跟踪要移动的 OverlayItem,然后删除跟踪的 OverlayItem 并添加新的 OverlayItem,或者获取跟踪的 OverlayItem,获取其标记或 MapPoint,并将其经纬度坐标重置为新的位置坐标。您可能还需要调用mapView.invalidate()来强制重绘。
Use an ItemizedOverlay, and as Dave suggested, keep track of the one you want moved and then either delete the tracked OverlayItem and add a new one or take the tracked one, get its marker or MapPoint and reset its lat/lon coordinates to the new location coordinates. You may also have to call mapView.invalidate() to force a redraw.