Pregunta

I am using Google Maps Extension Library. I have this:

int nsize = visibleMarkers.size();
                for (int i = 0; i < nsize; i++) {
                    String title = visibleMarkers.valueAt(i).getTitle();
                    String desc = visibleMarkers.valueAt(i).getDesc();
                    Float latitude = visibleMarkers.valueAt(i).getLat();
                    Float longitude = visibleMarkers.valueAt(i).getLon();

                    m = map.addMarker(new MarkerOptions()
                            .position(new LatLng(latitude, longitude))
                            .title(title)
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.snotel_marker)));

                }

and the map gets populated fine with all the markers.

I am trying to add data to a toast to see the description and title from the marker window on click:

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                            @Override
                            public void onInfoWindowClick(Marker marker) {
                                Toast.makeText(MainActivity.this,
                                        "Title: " + visibleMarkers.valueAt(i).getTitle(),
                                        Toast.LENGTH_SHORT).show();
                            }
                        });

When i add this setOnInfoWindow Listener, i variable needs to be final. I want to get the title of the marker from my visibleMarkers SparseArray, but I just cannot figure out how to get the data from the marker I cam clicking on. I know the desc has info in it, since using a .snippet(desc) shows the info on marker click.

What am I missing here?

EDIT:::

I changed my onPostExecute adding Marker m and my data to another array:

int nsize = visibleMarkers.size();
                for (int i = 0; i < nsize; i++) {
                    MapMarkers marks = new MapMarkers();
                    String title = visibleMarkers.valueAt(i).getTitle();
                    String desc = visibleMarkers.valueAt(i).getDesc();
                    Float latitude = visibleMarkers.valueAt(i).getLat();
                    Float longitude = visibleMarkers.valueAt(i).getLon();

                    m = map.addMarker(new MarkerOptions()
                            .position(new LatLng(latitude, longitude))
                            .title(title)
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.snotel_marker)));

                    marks.setTitle(title);
                    marks.setDesc(desc);

                    markerInfo.put(m, marks);

                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                        @Override
                        public void onInfoWindowClick(Marker marker) {

                            MapMarkers markInfo = markerInfo.get(marker);

                            Intent i = new Intent(MainActivity.this,
                                    MarkerInformation.class);
                            i.putExtra("name", markInfo.getTitle()).putExtra(
                                    "description", markInfo.getDesc());
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);

                        }

                    });
                }

Does that seem correct?

¿Fue útil?

Solución

First of all in your setOnInfoWindowClickListener the "i" has no relation with the marker you have pressed, the only connection with it is the marker object that is passed in onInfoWindowClick method. you can get the data directly from this marker object which has all the data you need, if you don't want this please explain more your problem

Otros consejos

try this

Toast.makeText(MainActivity.this, "Title: " + m.getTitle(), Toast.LENGTH_SHORT).show();

Note that m must be declare outside.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top