Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map camera position on multiple markers

I am using google map API 2 and showing multiple markers on it. My problem it that I want my camera to be positioned on all my markers. I have done it for single marker as

 myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MyLocation, 22));
like image 427
Andy Avatar asked Oct 27 '25 17:10

Andy


1 Answers

I have Used Below Way to see all markers in view. It will Bound your Mapview including all the Lat-Long Location you have in your arraylist.

 // Pan to see all markers in view.
 // Cannot zoom to bounds until the map has a size.
    final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
    if (mapView.getViewTreeObserver().isAlive()) {
        mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @SuppressLint("NewApi")
            @Override
            public void onGlobalLayout() {
                LatLngBounds.Builder bld = new LatLngBounds.Builder();
    for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {           
            LatLng ll = new LatLng(YOUR_ARRAYLIST.get(i).getPos().getLat(), YOUR_ARRAYLIST.get(i).getPos().getLon());
            bld.include(ll);            
    }
    LatLngBounds bounds = bld.build();          
    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
                mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            }
        });
    }

Hope it Will Help.

like image 103
Android123 Avatar answered Oct 30 '25 07:10

Android123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!