Domanda

I have a bunch of markers in which I load onto a google map. My problem is that, the camera is not ontop of the markers. Can I center it on top of all my markers?

I am adding my markers with this code:

for(int i = 0; i < jsonArray.length(); i++) {

                String breweryName = jsonArray.getJSONObject(i).getString("brewery");
                String lat = jsonArray.getJSONObject(i).getString("lat");
                String lng = jsonArray.getJSONObject(i).getString("lng");
                String bID = jsonArray.getJSONObject(i).getString("breweryID");

                double latD = Double.parseDouble(lat);
                double lngD = Double.parseDouble(lng);



                m.addMarker(new MarkerOptions()
                        .position(new LatLng(latD, lngD))
                        .title(breweryName));


            }

I looked on stack and found this question, which is the same as mine:

Android Google maps API V2 center markers

But there isnt much context behind it and am not sure how to implement it.

È stato utile?

Soluzione

You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)

List<MarkerOptions> markers = new ArrayList<MarkerOptions>();

You can then make sure they are all visible on your map by doing the following

LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
    position = markers.get(i).getPosition();
    builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top