0

Hi I'm currently attempting to display multiple points on google maps. Everything works fine apart for the zooming feature. I cannot work out how to set the zoom so it displays all the points. The code displays well if the points are close, but if there far away it doesn't. I've searched around but cant find anything.

Any help?

        Marker storeMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(name).snippet("test"));

        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);

1 Answers1

0

You should calculate the LatLngBounds that will contain all of your points and then call CameraUpdateFactory.newLatLngBounds(...) and update your map with this update.

See: http://developer.android.com/reference/com/google/android/gms/maps/CameraUpdateFactory.html#newLatLngBounds(com.google.android.gms.maps.model.LatLngBounds, int)

You shouldn't need to update the map twice - there are functions to create zoom+center at the same time.

Calculating the bounds is done using the LatLngBounds.Builder and including all the points in it.

Raanan
  • 4,777
  • 27
  • 47
  • http://pastebin.com/L6tcuPW9 Is there any chance you could have a quick look at that and just point me in the right direction? Im currently calling DownloadTask downloadTask = new DownloadTask(); every time I get a new postcode so im not to sure how to calculate the multiple points. If that makes sense? ha – user3237406 Jan 30 '14 at 18:34
  • You need to include all the points into a single LatLngBounds then build and zoom - what you are doing now is creating a new bounds for each point. Check this answer with code: http://stackoverflow.com/questions/16416041/zoom-to-fit-all-markers-on-map-google-maps-v2 – Raanan Jan 31 '14 at 09:17