jjzjj

Android MapView 无法删除标记

coder 2023-12-28 原文

我正在使用 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);
  }

 }

你能帮我解决这个问题吗?

非常感谢。

map Activity :

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);
    }

  }      

    }

最佳答案

根据 Dave 的建议,这解决了问题:

  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 无法删除标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4633841/

有关Android MapView 无法删除标记的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  8. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  9. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  10. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

随机推荐