Zoom in to markers google map V2

android, google-maps, java

Solution

The problem is that the map is not loaded yet to the layout that is why you got that error

Solution:

 map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }
});

It will call that method when the map is already loaded to the layout.

Problem

I have an `ArrayList` of a custom object with lat and long properties. I have managed to put markers on the map. What I want is to be able to zoom in to those markers when the map loads. What I have tried below causes the app to crash. Here is the code for loading the map: ``` public void getBridgeData() { db = new DbHelperClass(this); bridges = db.getAllBridges(); DecimalFormat dc = new DecimalFormat("#.000"); Builder builder = new LatLngBounds.Builder(); for (int i= 0;i<bridges.size();i++) { Bridge b= (Bridge)bridges.get(i); double lati= b.getLatitude(); double longo= b.getLongitude(); double reflat= b.getReflat(); double reflong= b.getReflong(); LatLng start = new LatLng(reflat,reflong); LatLng end = new LatLng(lati,longo); GeneralUtils uts = new GeneralUtils(); double ch= uts.calculateDistance(start, end); String chainage = dc.format(ch); String mysnipet = "Location:" + b.getRoadname() + " " + chainage; Marker mm=map.addMarker(new MarkerOptions().position(new LatLng(lati, longo)) .title(b.getBridgename()) .snippet(mysnipet)); builder.include(mm.getPosition()); } LatLngBounds bounds= builder.build(); map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15)); map.setMapType(GoogleMap.MAP_TYPE_HYBRID); } ``` Here also part of the code ``` public class MainActivity extends FragmentActivity { private GoogleMap map; private DbHelperClass db; private List<Bridge> bridges = new ArrayList<Bridge>(); CameraUpdate cu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_map); //AddSomeData(); map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); if(map!=null) { getBridgeData(); } } } ``` Here is the error log: ``` 05-30 22:19:28.894: E/AndroidRuntime(5593): FATAL EXCEPTION: main 05-30 22:19:28.894: E/AndroidRuntime(5593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bridge.bridgeinventory/com.bridge.bridgeinventory.MainActivity}: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions. 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1755) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1774) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.access$1500(ActivityThread.java:157) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Handler.dispatchMessage(Handler.java:130) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Looper.loop(SourceFile:351) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.main(ActivityThread.java:3841) 05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invokeNative(Native Method) 05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invoke(Method.java:538) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 05-30 22:19:28.894: E/AndroidRuntime(5593): at dalvik.system.NativeStart.main(Native Method) 05-30 22:19:28.894: E/AndroidRuntime(5593): Caused by: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions. 05-30 22:19:28.894: E/AndroidRuntime(5593): at kbh.b(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at mas.a(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at mal.a(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at mbi.a(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at fms.onTransact(SourceFile:83) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Binder.transact(Binder.java:310) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.getBridgeData(MainActivity.java:148) 05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.onCreate(MainActivity.java:44) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1719) 05-30 22:19:28.894: E/AndroidRuntime(5593): ... 11 more ``` How can I make it work?

Original source