0

Adding markers in Google map using the array list values but it fetches only the Last value from array list please help.Here is my loop given below.

                for(int k=0;k<jsonarray2.length();k++)
                    {
                        jsonobject2 =jsonarray2.getJSONObject(k);
                        HashMap<String, String> map1 = new HashMap<String, String>();
                        map1.put("Lat",jsonobject2.getString("Lat"));

                        Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject2.getString("Lat"));
                        map1.put("Long",jsonobject2.getString("Long"));
                        map1.put("StopName", jsonobject2.getString("StopName"));
                        Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject2.getString("Long"));
                        //      map1.put("LiveLongitude",jsonobject1.getString("LiveLongitude"));

                        //       Log.d("Hbbbbbbbbbbbbbbb", "" + jsonobject1.getString("LiveLongitude"));


                        arraylist12.add(map1);

                        Log.e("arraylist12", ""+arraylist12);

                         for (int m = 0; m < arraylist12.size(); m++) { 

                            final LatLng position = new LatLng(Double .parseDouble(arraylist12.get(m).get("Lat")),Double.parseDouble(arraylist12.get(m).get("Long"))); 
                            Log.e("position", ""+position);                             
                            String stopname = arraylist12.get(m).get("StopName");
                            if(mark!=null){
                                mark.remove();
                            }
                            final MarkerOptions options = new MarkerOptions().position(position); 

                            //mMap.addMarker(options); 
                            mark=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.bustour)).title(stopname));

}

Muhammad Babar
  • 8,084
  • 5
  • 39
  • 56
ganesan g
  • 87
  • 9
  • @RanjitPati yes because am adding marker when i click a marker which is stable in google map ,So if i click a marker in google map it has to show the set of related markers from array list,but after that if i click any other stable markers the markers which added before should be removed and the new set of related markers to be added.Please help – ganesan g Oct 08 '15 at 06:19
  • you are explicitly removing the marker by `if(mark!=null){ mark.remove(); }` – Muhammad Babar Oct 08 '15 at 06:27
  • @MuhammadBabar How can I resolve it please help me ? – ganesan g Oct 08 '15 at 06:29
  • remove that part of code. – Muhammad Babar Oct 08 '15 at 06:31
  • @MuhammadBabar If i remove it how can I remove the markers and add the new one ??? – ganesan g Oct 08 '15 at 06:32
  • please explain what are you trying to achieve? – Muhammad Babar Oct 08 '15 at 06:34
  • @MuhammadBabar In my application am using google map when the application launches the set of markers will be displayed ,please Assume the set of markers as A ,when i click any one of the marker in "A" another set of markers will be generated and added Assume it as "B",So after that if I again click any markers in"A" the previous "B marker" should be removed and the new set of "B" Marker should be added.So only i use the mark.remove but it showing only the last value from "B" – ganesan g Oct 08 '15 at 06:40
  • @MuhammadBabar Can you explain me in detail please ?I have added the code in On click of "A"only – ganesan g Oct 08 '15 at 06:46
  • Kindly move this `if(mark!=null){ mark.remove(); }` before the loop. – Muhammad Babar Oct 08 '15 at 06:50
  • Check code here http://paste.ofcode.org/LMmXACfXdMPKfbtiLASMUA – Muhammad Babar Oct 08 '15 at 07:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91707/discussion-between-ganesan-g-and-muhammad-babar). – ganesan g Oct 08 '15 at 07:49

1 Answers1

1

I think this much code is fine to add multiple markers through loop..

for (int m = 0; m < arraylist12.size(); m++) { 
    final LatLng position = new LatLng(Double .parseDouble(arraylist12.get(m).get("Lat")),Double.parseDouble(arraylist12.get(m).get("Long"))); 

    String stopname = arraylist12.get(m).get("StopName");
    mark=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.bustour)).title(stopname));
}

If you will use if(mark!=null){mark.remove();} , then it will remove the last marker before adding the new one..so at last you can only see the last marker..

EDIT

You you want to remove the set of markers before adding a new set, then just clear the map before adding new marker set.

something like:: mMap.clear(); then add the new set..

Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • without adding the line how can I remove the previous set of markers before adding the new one? – ganesan g Oct 08 '15 at 07:01
  • I think you want to show all markers in one map.Right ..then why you want to remove the last one. – Ranjit Oct 08 '15 at 07:03
  • @ganesang you need to add previous set of marker is a Hashmap and then you could iterate to remove all of them! – Muhammad Babar Oct 08 '15 at 07:04
  • @MuhammadBabar I added it in Hashmap only kindly check the above code I updated – ganesan g Oct 08 '15 at 07:10
  • @ganesang store variable `mark` to the hashmap when you are adding them on map for the very first time next time on click on A you need to remove all the mark that are already stored in hashmap at once and then try adding the new markers! – Muhammad Babar Oct 08 '15 at 07:16
  • @MuhammadBabar can you please edit the above code ?? – ganesan g Oct 08 '15 at 07:17
  • @RanjitPati if I clear the map the map will be reloaded but I need to only remove the markers and add the new sets – ganesan g Oct 08 '15 at 07:36
  • then remove each marker one by one just before the loop..make sure the marker list is not null before removing anything from it..http://stackoverflow.com/questions/17197325/how-to-clear-all-the-markers-in-v2-google-map – Ranjit Oct 08 '15 at 07:40