0

I have 3 markers in a map. I get marker data through a server and associate it through each marker. Due to data amount, I only get a subset of data for each marker (for development I have 3 markers, but there may be many more). So I associate an ID to each marker, and for the next activity, I get that ID and retrieve the complete data set for the next activity to be displayed.

Unfortunately all markers end up being associated with the same ID. What am I missing here?

Initially I was passing a final variable to the inner class in order to assign the ID. Finding this question: How to pass parameters to anonymous class? (kudos!) I introduced an initializer to the inner class, thinking the final attribute might have been the problem. But nope. Still getting the same ID on all markers.

   private class Getter extends AsyncTask<String, Void, JSONArray> {

            private Exception exception;
            protected static final String TAG = "Getter";

            protected JSONArray doInBackground(String... urls) {
                try {
                    try {
                        //get JSON data from server
                        return json_objects.getJSONArray("objects");
                    } catch (JSONException jse) {
                        //more error handling
                    }
                }catch(Exception e) {
                    this.exception = e;
                    return null;
                }
            }

            protected void onPostExecute(JSONArray obj_arr) {
                try {
                    if (this.exception != null) {
                        //some error handling
                    } else if (obj_arr != null) {                    
                      for (int i = 0; i < obj_arr.length(); i++) {
                          JSONObject obj = obj_arr.getJSONObject(i);
                          create_marker(obj);
                      }

                    }
                } catch(JSONException jse) {
                  //error handling
                }
            }

            private void create_marker(JSONObject obj) {
                try {
                    JSONObject location = obj.getJSONObject("location");
                    double lat = location.getDouble("lat");
                    double lng = location.getDouble("lng");
                    String id = obj.getString("id");

                    LatLng pos = new LatLng(lat, lng);
                    mMap.addMarker(new MarkerOptions().position(
                            pos).title(obj.getString("name")).                        
                    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                        String local_id;

                        public GoogleMap.OnMarkerClickListener init(String id) {
                            local_id = id;
                            return this;
                        }

                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            Intent i = new Intent(MapActivity.this, NextActivity.class);
                            i.putExtra("obj_id", local_id);
                            startActivity(i);
                            return false;
                        };
                    }.init(id));
                } catch (JSONException e) {
                   //error handling
                }

            }
        }
Community
  • 1
  • 1
transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • Looks like the `onMarkerClick` handler only can access the last local_id - is the constructor not constructing new instances for each anonymous listener? – transient_loop Apr 09 '15 at 18:00
  • Dam it. I realize now the listener is on the **map**, not the marker....I need a different approach. Posting solution when I've found one. – transient_loop Apr 09 '15 at 18:04
  • Ofcourse, It will take the last id only at every time. Because Listener may invoked at only once. – user370305 Apr 09 '15 at 18:11
  • 1
    If you want to associate only Id (Or in future Object) with Clicked marker. Just implement `WeakHashMap markerMap` and put marker entry every time when you add it to map with Object, in your case it will ID. – user370305 Apr 09 '15 at 18:13
  • yes @user370305 I found a link doing just that. :) – transient_loop Apr 09 '15 at 18:53

0 Answers0