Saturday, December 8, 2018

Android : How to show user image on the marker, zoom to fit all the markers

Below is the code to do this!

public void updateMapWithUserLocations(){
        final ArrayList markers = new ArrayList();
        targets = new ArrayList();

        for (int i = 0 ; i < users.length; i++){
            final LatLng location = locations[i];
            final String imageURL = images[i];
            Target target = null;
            Marker userMarker = mMap.addMarker(new MarkerOptions()
                    .position(location)
                    .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_icon)))
                    .title("test")
                    .snippet("test address")
            );
            markers.add(userMarker);
            PicassoTarget pTarget = new PicassoTarget(userMarker);
            targets.add(pTarget);
            Picasso.with(this)
                    .load(imageURL)
                    .resize(100,100)
                    .centerCrop()
                    .transform(new BubbleTransformation(5))
                    .into(pTarget);

        }
        fitAlMarkersInZoom(mMap,markers);
    }

    class PicassoTarget implements Target{
        private Marker marker;

        PicassoTarget(Marker marker){
            this.marker = marker;
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            this.marker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    }


    private void fitAlMarkersInZoom(GoogleMap googleMap, ArrayList markers){

        try{
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (Marker marker : markers) {
                builder.include(marker.getPosition());
            }
            final LatLngBounds bounds = builder.build();

            try {
                mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
            } catch (IllegalStateException e) {
                // layout not yet initialized
                final View mapView = getFragmentManager()
                        .findFragmentById(R.id.map).getView();
                if (mapView.getViewTreeObserver().isAlive()) {
                    mapView.getViewTreeObserver().addOnGlobalLayoutListener(
                            new ViewTreeObserver.OnGlobalLayoutListener() {
                                @SuppressWarnings("deprecation")
                                @SuppressLint("NewApi")
                                // We check which build version we are using.
                                @Override
                                public void onGlobalLayout() {
                                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                        mapView.getViewTreeObserver()
                                                .removeGlobalOnLayoutListener(this);
                                    } else {
                                        mapView.getViewTreeObserver()
                                                .removeOnGlobalLayoutListener(this);
                                    }
                                    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
                                }
                            });
                }
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }

No comments:

Post a Comment