3

I am working on an Android app and I have two markers i want to place on a google map. The idea is that the user can see the two locations at a glance without having to interact with the map.

 LatLngBounds.Builder b = new LatLngBounds.Builder();
 b.include(userPos);
 b.include(cardPos);
 LatLngBounds bounds = b.build();
 int width = (int) (0.7 * infoView.getWidth());
 int height = (int) (0.7 * mapView.getHeight());
 CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, 5);
 googleMap.animateCamera(cu);

The code works fine when the two locations are not very far apart. But when the locations are, for example, Singapore and USA, the two markers cannot be seen together. I have tried to manually set the zoom level to 0 with the same result. Is there any way that I can show the entire world map on the android device at once (i.e without the need for scrolling on the user's part)?

Update: I have tried setting the map to zoom level 0 explicitly. The map does not fit the View I have created. Is it not possible to have the full world view of the map on the screen?

Hassan
  • 31
  • 2
  • check it out http://stackoverflow.com/questions/9893680/google-maps-api-v3-show-the-whole-world. Hope this helps – Mohd. Shariq Jan 15 '17 at 09:18
  • 1
    Isn't it the same thing I have tried? The solution specifies two LatLng positions and sets the bound according to that. In fact, my two positions are less extreme – Hassan Jan 15 '17 at 09:32

1 Answers1

0

You should use the CameraUpdate class to do (probably) all programmatic map movements.

To do this, first calculate the bounds of all the markers like so:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
    builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

Then obtain a movement description object by using the factory: CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

Finally move the map:

googleMap.moveCamera(cu);

Or if you want an animation:

googleMap.animateCamera(cu);

That's all :)

  • 1
    That's the same thing I have tried.. Works for locations relatively closer together. But if the two locations are far apart, the map needs to be scrolled to be seen. Btw, the map is already at the lowest zoom. – Hassan Jan 22 '17 at 13:50