0

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?

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82

2 Answers2

2

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

Omar HossamEldin
  • 3,033
  • 1
  • 25
  • 51
  • How do I get this info? visibleMarkers.??? I don't see anything that is giving me data. Thanks for the response. – jasonflaherty Oct 01 '13 at 00:45
  • Yea try what @MaciejGórski said 'marker.getTitle()' where marker is the object you received from 'onInfoWindowClick' method – Omar HossamEldin Oct 01 '13 at 09:43
  • @MaciejGórski Yes, that works. What isn't/wasn't was getting the objects. I did end up getting them by using another array in my onPostExecute... See edit... Does that look correct? – jasonflaherty Oct 01 '13 at 15:04
  • @MaciejGórski Does the Map Extension clustering cut down on the amount of data being added to the map or does it just cluster the data already there? Does it load the points only when the person is within the clustered area? – jasonflaherty Oct 01 '13 at 15:08
  • @MaciejGórski Where do I use this: .setClustering(new ClusteringSettings().enabled(false).addMarkersDynamically(true)); in order for the markers to be added dynamically? My loop adds them all to the map as it loops... do I need to change that? – jasonflaherty Oct 01 '13 at 15:37
  • 1
    @jasonflaherty I guess it would be better to ask your questions as SO questions, one question per problem (I track questions with [android-maps-extensions] tag). Your original problem seems to be solved and so you should accept and upvote answer from 3amoura. – MaciejGórski Oct 01 '13 at 15:44
1

try this

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

Note that m must be declare outside.

swati srivastav
  • 635
  • 4
  • 15