How to extract a marker info from a list of marker

2019-08-04 10:42发布

I'm using the following code to create a list of markers in a mapfragment using three arrays that sends the information of latitude, longitude and name of one list of records. My objective is to get send to a new intent the name of the record when the info window is clicked. Now sends for all markers the recordname of the last item of the array but I need send the specific info to each marker. Any idea?

            String[] arraylatitud = arrlat.toArray(new String[arrlat.size()]);
            String[] arraylongitud = arrlon.toArray(new String[arrlon.size()]);
            String[] arrayrecordname = arrrecname.toArray(new String[arrrecname.size()]);

            for(int i=0; i<arrlon.size();i++){


                     mapa.addMarker(new MarkerOptions()
                    .position(new LatLng(Float.valueOf (arraylatitud[i]),Float.valueOf (arraylongitud[i])))
                    .title("Grabación:" + arrayrecordname[i])
                    .snippet("Latitud:" + arraylatitud[i] + "Longitud:" + arraylongitud[i]));

                    filename = arrayrecordname[i];

            } 

            mapa.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    public void onInfoWindowClick(Marker marker) {


                           Intent intent = new Intent(MapArea.this, StreamingArea.class);
                           intent.putExtra("variable_selection", filename.toString());
                           startActivity(intent);


                    }
            }

1条回答
\"骚年 ilove
2楼-- · 2019-08-04 11:29

I've just made a blog post on this. You can find it at: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html

Every marker has got an Id.

Marker m = mapa.addMarker(new MarkerOptions()
                .position(new LatLng(Float.valueOf (arraylatitud[i]),Float.valueOf (arraylongitud[i])))
                .title("Grabación:" + arrayrecordname[i])
                .snippet("Latitud:" + arraylatitud[i] + "Longitud:" + arraylongitud[i]));

m.getId();

if you store that id, along with the information you need you can get the right info back

getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        public void onInfoWindowClick(Marker marker) {
            int id = marker.getId();
            Intent i = new Intent(MapArea.this.getActivity(), StreamingArea.class);
            //your code goes here
            i.putExtra(....);

            startActivity(i);

        }
    });
查看更多
登录 后发表回答