2

I am trying focus the map on the marker at a certain zooming ratio. I've looked at a couple posts in stackoverflow and I couldn't see any reason why I am getting the following error:

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.

The code that I am using is:

private void setUpMap(String address) throws IOException {
  Geocoder gc = new Geocoder(this);
  List<Address> list = gc.getFromLocationName(address, 1);
  Address add = list.get(0);
  double Longitude = add.getLongitude();
  double Latitude= add.getLatitude();
  Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));
  LatLngBounds.Builder builder = new LatLngBounds.Builder();
  builder.include(m.getPosition());
  LatLngBounds bounds = builder.build();
  mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
}

I debugged the code, and it is throwing the previous exception on the line

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));

Any suggestion here? thanks...

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
Ray
  • 781
  • 2
  • 17
  • 42
  • Possible duplicate of http://stackoverflow.com/questions/13692579/movecamera-with-cameraupdatefactory-newlatlngbounds-crashes – Harsh Dattani Apr 30 '15 at 21:03
  • @HarshDattani. This did not answer or solve my problem.. I did everything correct.. but still getting this exception.. – Ray Apr 30 '15 at 21:34
  • I think the issue maybe related to passing only one location to the LatLngBounds.builder, I would think you need at least two locations for it to provide a size larger than 0. – faljbour Apr 30 '15 at 22:12

1 Answers1

0

In order to use just one location from an address, and set the zoom on that one marker, you can use CameraPosition instead of LatLngBounds.

I just tested this, and it works:

private void setUpMap(String address) throws IOException {
        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(address, 1);
        Address add = list.get(0);
        double Longitude = add.getLongitude();
        double Latitude= add.getLatitude();
        Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));

        //not needed:
        //LatLngBounds.Builder builder = new LatLngBounds.Builder();
        //builder.include(m.getPosition());
        //LatLngBounds bounds = builder.build();
        //mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));

        //use CameraPosition instead:
        LatLng latLng = new LatLng(Latitude, Longitude);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLng).zoom(14).build();

        //use animateCamera instead of moveCamera:
        mMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
    }

Calling the method:

try {
    setUpMap("100 Market Street San Francisco CA");
} catch (IOException e) {
    e.printStackTrace();
}

Result:

enter image description here

Note that usually when using LatLngBounds, you would initially provide two points that are the Northeast and Southwest corners of the bounds.

Once those two bounds have been set, you can use the include() method to expand the bounds if necessary.

See Documentation:

https://developer.android.com/reference/com/google/android/gms/maps/model/LatLngBounds.html

https://developers.google.com/maps/documentation/android/views

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank you @DanielNugent for the answer, it seems that every time I launch the emulator it crashes (NOT THE APP). I tried to launch it 3 times and still crashes.. maybe there is something happening in the background. Any idea why – Ray Apr 30 '15 at 23:11
  • Yes, and I enabled the debugging mode and android studio still couldn't catch my mobile!! – Ray Apr 30 '15 at 23:22
  • I have oneplusone, and yes i tried it before and it worked.. Android KitKat – Ray Apr 30 '15 at 23:24
  • I will, and let you know.. Thanks a lot – Ray Apr 30 '15 at 23:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76685/discussion-between-daniel-nugent-and-riad). – Daniel Nugent Apr 30 '15 at 23:32
  • The emulator fails each time I launch the app and still did not catch my mobile device. – Ray Apr 30 '15 at 23:48
  • Thanks a lot Daniel for your efforts.. yes it's working on my mobile device :) – Ray May 01 '15 at 00:02