Raheel Sadiq
Raheel Sadiq

Reputation: 9867

mapviewballoon - how to set zoom when multiple markers are added

I used a library name mapviewballoon in my project mapviewballon .

when I set 20,30 or more pins the map on different locations it zooms to the last pin in the center, what I want is that the zoom level is such that it show up all pins in the map view.

how can I solve that, or calculate the right zoom, currently I given 14.

doing things as follow:

OverlayItem overlayItem = null;
GeoPoint point = null;
for (int i = 0; i < nearbyObjMapList.size(); i++) {
       if (!nearbyObjMapList.get(i).latitude.equals("0") && !nearbyObjMapList.get(i).longitude.equals("0")) {
           Latitude = Float.parseFloat(nearbyObjMapList.get(i).latitude);
           Longitude = Float.parseFloat(nearbyObjMapList.get(i).longitude);
           point = new GeoPoint((int)(Latitude*1E6),(int)(Longitude*1E6));
           overlayItem = new OverlayItem(point, nearbyObjMapList.get(i).venue_name,                            "");
           itemizedOverlay.addOverlay(overlayItem);
       }

                    }
if (thisState == null) {

      final MapController mc = mapView.getController();
      mc.animateTo(point);
      mc.setZoom(14);

 } else {

      // example restoring focused state of overlays
      int focused;
      focused = thisState.getInt("focused_1", -1);
      if (focused >= 0) {
                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
          }
      focused = thisState.getInt("focused_2", -1);
           if (focused >= 0) {
                 temizedOverlay2.setFocus(itemizedOverlay2.getItem(focused));
            }

  }

Upvotes: 1

Views: 381

Answers (2)

Hesham Saeed
Hesham Saeed

Reputation: 5378

You will need to span the zoom in order to achieve that, I updated your code in order to do it as follows (It might have errors since I cannot debug it):

int minLat=Integer.MAX_VALUE;
int maxLat=Integer.MIN_VALUE;
int minLng=Integer.MAX_VALUE;
int maxLng=Integer.MIN_VALUE;

OverlayItem overlayItem = null;
GeoPoint point = null;
for (int i = 0; i < nearbyObjMapList.size(); i++) {
       if (!nearbyObjMapList.get(i).latitude.equals("0") && !nearbyObjMapList.get(i).longitude.equals("0")) {
           Latitude = Float.parseFloat(nearbyObjMapList.get(i).latitude);
           Longitude = Float.parseFloat(nearbyObjMapList.get(i).longitude);
           point = new GeoPoint((int)(Latitude*1E6),(int)(Longitude*1E6));
           overlayItem = new OverlayItem(point, nearbyObjMapList.get(i).venue_name,                            "");
           itemizedOverlay.addOverlay(overlayItem);

           maxLat = Math.max((int) (Latitude*1.0E6), maxLat);
           minLat = Math.min((int) (Latitude*1.0E6), minLat);
           maxLng = Math.max((int) (Longitude*1.0E6), maxLng);
           minLng = Math.min((int) (Longitude*1.0E6), minLng);
       }

                    }
if (thisState == null) {

      final MapController mc = mapView.getController();
      mc.zoomToSpan(Math.abs(maxLat-minLat), Math.abs(maxLng-minLng));
      mc.animateTo(new GeoPoint((minLat+maxLat)/2,(minLng+maxLng)/2));

 } else {

      // example restoring focused state of overlays
      int focused;
      focused = thisState.getInt("focused_1", -1);
      if (focused >= 0) {
                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
          }
      focused = thisState.getInt("focused_2", -1);
           if (focused >= 0) {
                 temizedOverlay2.setFocus(itemizedOverlay2.getItem(focused));
            }

  }

Note: you can add extra fixed value or the radius of the marker at zoomToSpan() or animateTo() functions (like +0.001 or +radius) in order to get bigger range to fit your markers (this will work as padding).

Upvotes: 1

TNR
TNR

Reputation: 5869

Don't use setZoom() and it is always better to use zoomToSpan when you have many pins to be pointed. You add below code after your for loop, which works perfect for me.

                   if(nearbyObjMapList.length!=0)
                   {
                    mapOverlays.add(itemizedoverlay);
                    // showing all overlay items
                    itemizedoverlay.populateNow();
                   // Adjusting the zoom level so that you can see all the markers on map
                    mv.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
                    // Showing the center of the map
                    mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
                   }
                   mv.postInvalidate();

Upvotes: 1

Related Questions