Search code examples
androidgoogle-mapsparse-platformparse-android-sdk

OnMarkerClickListener shows the same image for all markers


I have a function that gets location and its image from a parse server in a for loop then adds them as markers on the google map, the thumbnail as markers icon is working just fine the problem is when I click on any marker it shows the last image that was sent with putExtra().

Is there a way to make it show the right image or another approach than sending it with putExtra() to show it in pop up style .

Here is the code in the actvitymap:

public void getimagesLocation(){

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Image");
        ParseGeoPoint geoPointLocation = new ParseGeoPoint();
        query.whereNear("location", geoPointLocation);
        query.setLimit(100);
        Log.i("imageslocation", "limit set to 100 image on map");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    Log.i("imageslocation", "no errors");
                }

                if (objects.size() > 0) {
                    for (ParseObject object : objects) {
                        //This is the loop function to get all images from parseserver
                        final ParseGeoPoint point = object.getParseGeoPoint("location");
                        ParseFile file = (ParseFile) object.get("image");
                        file.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {
                                if (e == null && data != null);
                                Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);


                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
                                final byte[] b = baos.toByteArray();


                                // thumbnail that will replace the marker icon
                                Bitmap thumbnail = ThumbnailUtils.extractThumbnail(getCroppedBitmap(bitmap),200,200);

                                Double lat = point.getLatitude();
                                Double lng = point.getLongitude();

                                LatLng marker = new LatLng(lat, lng);
                                Log.i("imageslocation", "Latitude :" + lat + " Longitude: " + lng);


                                mMap.addMarker(new MarkerOptions()
                                        .title("Another Image")
                                        .icon(BitmapDescriptorFactory.fromBitmap(thumbnail))
                                        //.infoWindowAnchor(0.5f, 0.5f)                                //.snippet("You can and will achieve")
                                        .position(marker));
                                mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                    @Override
                                        public boolean onMarkerClick (Marker marker){

                                        Intent intent = new Intent(MapScreen.this, FullimageScreen.class);
                                        intent.putExtra("picture", b);
                                        startActivity(intent);

                                        return true;                                   }  });  }  });   }  }  });  }

Solution

  • Try:

    public void getimagesLocation(){
    
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Image");
        ParseGeoPoint geoPointLocation = new ParseGeoPoint();
        query.whereNear("location", geoPointLocation);
        query.setLimit(100);
        Log.i("imageslocation", "limit set to 100 image on map");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    Log.i("imageslocation", "no errors");
                }
    
                if (objects.size() > 0) {
                    for (ParseObject object : objects) {
                        //This is the loop function to get all images from parseserver
                        final ParseGeoPoint point = object.getParseGeoPoint("location");
                        ParseFile file = (ParseFile) object.get("image");
                        file.getDataInBackground(new GetDataCallback() {
                            @Override
                            public void done(byte[] data, ParseException e) {
                                if (e == null && data != null);
                                Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
    
    
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
                                final byte[] b = baos.toByteArray();
    
    
                                // thumbnail that will replace the marker icon
                                Bitmap thumbnail = ThumbnailUtils.extractThumbnail(getCroppedBitmap(bitmap),200,200);
    
                                Double lat = point.getLatitude();
                                Double lng = point.getLongitude();
    
                                LatLng marker = new LatLng(lat, lng);
                                Log.i("imageslocation", "Latitude :" + lat + " Longitude: " + lng);
    
    
                                Marker newMarker = mMap.addMarker(new MarkerOptions()
                                        .title("Another Image")
                                        .icon(BitmapDescriptorFactory.fromBitmap(thumbnail))
                                        //.infoWindowAnchor(0.5f, 0.5f)                                //.snippet("You can and will achieve")
                                        .position(marker));
                                newMarker.setTag(b);
                                  }  });   }
                    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                        @Override
                            public boolean onMarkerClick (Marker marker){
    
                            Intent intent = new Intent(MapScreen.this, FullimageScreen.class);
                            intent.putExtra("picture", (byte[]) marker.getTag());
                            startActivity(intent);
    
                            return true;                                   }  });
                }  });  }