3

I have implemented Google Maps in my app and have added 2 markers on it.

Here's how:

LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));
mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mainUserLocation))
mMap.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);

LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);
mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));

The problem is that the camera is zooming and getting focused on one marker and the other marker is getting out of sight!

I want both of the markers or all the markers to remain in sight. How to achieve this?

Please let me know.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • Possible duplicate of [Android map v2 zoom to show all the markers](http://stackoverflow.com/questions/14828217/android-map-v2-zoom-to-show-all-the-markers) – Nishant Dubey Sep 28 '16 at 16:46

2 Answers2

4

So this question has already been answered here and not mentioning it would be violation of good community ethics.

Still, here I will try to break it down in your context. So that you understand better what steps you need to take.

LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));

LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);

    public void showMap() {

        mMap.clear();
        //Create your Markers List
        List<Marker> markersList = new ArrayList<Marker>();
        Marker youMarker = mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
        Marker playerMarker = mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));

        //Add them to your list
        markersList.add(youMarker);
        markersList.add(playerMarker);


//get the latLngbuilder from the marker list
        builder = new LatLngBounds.Builder();
        for (Marker m : markersList) {
            builder.include(m.getPosition());
        }

//Bounds padding here
        int padding = 50;

        //Create bounds here
        LatLngBounds bounds = builder.build();

//Create camera with bounds
        cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

//Check map is loaded
        mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                //animate camera here
                mMap.animateCamera(cu);

            }
        });


}

So in your code you need to setup a method like this and just call showMap().

Hope this info helps.

Community
  • 1
  • 1
Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18
0

First Way

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 each (Marker m : markers) {
    builder.include(m.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);

Another Way

Find Average of all latlongs i.e

double avg_Lat = 23.521252;
double avg_Lng = 72.521252;

Then Make a new Latlng Object and assign it

LatLng latLng = null;
latLng = new LatLng(avg_Lat, avg_Lng);

map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));

It will include all latlngs

Mohit Trivedi
  • 699
  • 3
  • 13
  • 1
    Great answer. The point is that you have to select boundaries that will contain both markers. – Edo user1419293 Sep 28 '16 at 14:18
  • @MohitTrivedi please explain the first way more briefly... I'm unable to understand it! :/ – Hammad Nasir Sep 28 '16 at 15:45
  • @MohitTrivedi If you want to refer to an answer from other question. Please mention the question or try to modify the answer in current context. That is more useful rather than a not so thoughtful copy paste. Because that can confuse the current person asking the question if he has no idea about what you just wrote. – Nishant Dubey Sep 28 '16 at 16:51
  • @nishant Dubey it was not copied answer look at the carefully I suggested 2 answers already and it was cleared mentioned – Mohit Trivedi Sep 28 '16 at 16:58