Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom in to markers google map V2

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?

like image 302
user3079559 Avatar asked May 30 '14 19:05

user3079559


People also ask

Can you zoom in on Google Maps?

You can zoom in and out of the map with one hand. Double tap a spot on the map, and then: Drag down to zoom in. Drag up to zoom out.

How do I custom zoom on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

How do I change the max zoom level in Google Maps?

Setting up the minimum or maximum Zoom value for Google Maps can be done by adding the shortcode attribute with Store Locator Shortcode. Furthermore, the maximum value of Google Maps Zoom level is 22. Therefore, the defined maximum value must be below or equal to 22, which is the default.

How do I zoom in Google Maps API?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.


1 Answers

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.

like image 173
Rod_Algonquin Avatar answered Sep 30 '22 13:09

Rod_Algonquin