android - Move marker in Google map when latitude longitude changes -


in application creating array list containing latitude , longitude json , adding marker in map buy suing array list when lat long changes marker moves 1 position create 2 marker instead of moving,if give map.clear(),the whole marker getting disable , enabling want remove old marker how possible without using map.clear(),please help

private void setupmapifneeded(view inflatedview) {

        if (mmap == null) {              mmap = ((mapview) inflatedview.findviewbyid(r.id.mapview)).getmap();              mmap.setmylocationenabled(true);               location mylocation = mmap.getmylocation();              if (mmap != null) {                   //mmap.clear();                  // setupmap();                  mmap.setonmylocationchangelistener(new googlemap.onmylocationchangelistener() {                       @override                      public void onmylocationchange(location arg0) {                           // todo auto-generated method stub                          final latlngbounds.builder builder = new latlngbounds.builder();                       mmap.clear();                         if (marker != null) {                              marker.remove();                          }                         (int = 0; < arraylist1.size(); i++) {                              final latlng position = new latlng(double                                      .parsedouble(arraylist1.get(i).get("latitude")),                                      double.parsedouble(arraylist1.get(i).get(                                              "longitude")));                              string ime1 = arraylist1.get(i).get("ime");                              final markeroptions options = new markeroptions()                              .position(position);                              //mmap.addmarker(options);                              //mmap.addmarker(options.icon(bitmapdescriptorfactory .fromresource(r.drawable.buspng)).title(ime1));                                marker=mmap.addmarker(new markeroptions().position(position).icon(bitmapdescriptorfactory .fromresource(r.drawable.buspng)).title(ime1));                              //options.title(ime1);                             builder.include(position);                           }                          latlng latlng = new latlng(arg0.getlatitude(), arg0                                  .getlongitude());                          mmap.setmaptype(googlemap.map_type_normal);                         mmap.setmylocationenabled(true);                           mmap.movecamera(cameraupdatefactory.newlatlng(latlng));                          // mmap.setonmapclicklistener(null);                          mmap.setonmarkerclicklistener(null);                           mmap.animatecamera(cameraupdatefactory.zoomto(9));                      }                  });               }          }      }       /*  protected void retrieveandaddcities() throws ioexception {          httpurlconnection conn = null;          final stringbuilder json = new stringbuilder();          try {               url url = new url(service_url);              conn = (httpurlconnection) url.openconnection();              inputstreamreader in = new inputstreamreader(conn.getinputstream());               int read;              char[] buff = new char[1024];              while ((read = in.read(buff)) != -1) {                  json.append(buff, 0, read);              }          } catch (ioexception e) {              log.e(log_tag, "error connecting service", e);              throw new ioexception("error connecting service", e);          } {              if (conn != null) {                  conn.disconnect();              }          }          new downloadjson().execute();       } */      private class downloadjson extends asynctask<void, void, void> {          string result="";          @override          protected void onpreexecute() {              super.onpreexecute();          }          @override          protected void doinbackground(void... params) {              try {                  arraylist1 = new arraylist<hashmap<string, string>>();                   jsonparser jparser = new jsonparser();                  string result = "";                   json = jparser.getjsonfromurl(service_url);                   try {                      //arraylist1.clear();                       jsonarray = json.getjsonarray("singleimes");                      log.d("haaaaaaaaaaaa", "" + json);                       (int = 0; < jsonarray.length(); i++) {                          log.d("h11111111111111111111111111",                                  "" + jsonarray.length());                          map = new hashmap<string, string>();                          json = jsonarray.getjsonobject(i);                           // pubname = json.getstring("pubname");                          latitude = json.getdouble("latitude");                          longitude = json.getdouble("longitude");                          ime = json.getstring("ime");                         //  map.put("pubname", json.getstring("pubname"));                          //map.put("pubid", json.getstring("pubid"));                          map.put("latitude", json.getstring("latitude"));                          log.e("checklat",""+json.getstring("latitude") );                         map.put("longitude", json.getstring("longitude"));                          log.e("checklong",""+json.getstring("longitude") );                          map.put("ime", json.getstring("ime"));                         arraylist1.add(map);                      }                   } catch (jsonexception e) {                      log.e("log_tag", "error parsing data " + e.tostring());                      result="error";                      e.printstacktrace();                  }              }catch(exception e){                  result="error";              }               return null;          }           protected void onpostexecute(void args) {               // mprogressdialog.dismiss();           }      }       @override      public void onresume() {          super.onresume();          mmapview.onresume();      }       @override      public void onpause() {          super.onpause();          mmapview.onpause();      }       @override      public void ondestroy() {          mmapview.ondestroy();      super.ondestroy();      }      @override     public void onlocationchanged(location location) {         // todo auto-generated method stub     /* toast.maketext(getactivity(), "onlocationupdated!!!", toast.length_short).show();         log.d("onlocationupdated!!!","");         new downloadjson().execute();          setupmapifneeded(rootview);*/      }       @override     public void onstatuschanged(string provider, int status, bundle extras) {         // todo auto-generated method stub      }      @override     public void onproviderenabled(string provider) {         // todo auto-generated method stub      }      @override     public void onproviderdisabled(string provider) {          // todo auto-generated method stub      }    } 

first of need identify markers somehow, every marker has have uniq id, lets it's string.

then need store markers.

private hashmap<string, marker> mmarkers = new hashmap<>(); 

then before update markers new data need make copy of previous markers collection find out 1 newly added , 1 missing:

hashmap<string, marker> oldmarkers = mmarkers; mmarkers = new hashmap<>();  (yourmarkerobject m : newmarkers) { // assume have collections of objects here   string id = m.getid(); // id   marker marker = oldmarkers.get(id);   if (marker == null) { // means it's new 1     // create marker , attach map on right position   } else { // means have marker attached, move     marker.setposition(m.getposition());     oldmarkers.remove(id); // remove 1 old markers collection   }    mmarkers.put(id, marker); }  (marker oldmarker : oldmarkers.values()) {   // detach old markers } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -