Santhosh
Santhosh

Reputation: 5016

Set zoom level on google map in android

I need to show my current and target locations on google map with max zoom level. Here is my code to show both locations with max zoom. But its zooming to center of both locations and my locations are going out of the sight. I want my both locations should be in view bounds and set the zoom as maximum as possible. Please help me. Thanks in advance.

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapMarker = getResources().getDrawable(R.drawable.map_marker);  

    overlayList = mapView.getOverlays();
    myMap = new MyLocationOverlay(this, mapView);
    myMap.enableMyLocation();   

    mapController = mapView.getController(); 

    GeoPoint geoPoint = new GeoPoint((int) (targetLatitude * 1E6), (int) (targetLongitude * 1E6));

    mapController.animateTo(new GeoPoint(((int) (targetLatitude * 1E6) + (int) (currentLatitude * 1E6))/2, ((int) (targetLongitude * 1E6) + (int) (curLongitude * 1E6))/2 )); 
    mapController.zoomToSpan((int)(targetLatitude - currentLatitude), (int)(targetLongitude - curLongitude));

    mapController.setZoom(12);

    OverlayItem overlayItem = new OverlayItem(geoPoint,"xxx", "yyyy");

    CustomPinpoint customPinpoint = new CustomPinpoint(mapMarker, this);
    customPinpoint.insertPinpoint(overlayItem);

    overlayList.clear();
    overlayList.add(myMap);
    overlayList.add(customPinpoint); 

Upvotes: 1

Views: 1662

Answers (2)

Anirudh
Anirudh

Reputation: 3358

Reduce the zoom level and animate to the geopoint where you want your marker to be fixed.

Upvotes: 0

Carnal
Carnal

Reputation: 22064

    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;

    for (GeoPoint point : points) {
        minLat = Math.min(point.getLatitudeE6(), minLat);
        minLong = Math.min(point.getLongitudeE6(), minLong);
        maxLat = Math.max(point.getLatitudeE6(), maxLat);
        maxLong = Math.max(point.getLongitudeE6(), maxLong);
    }

    MapController controller = view.getController();
    controller.zoomToSpan(
                       Math.abs(minLat - maxLat), Math.abs(minLong - maxLong));
    controller.animateTo(new GeoPoint((maxLat + minLat) / 2,
        (maxLong + minLong) / 2));

Upvotes: 2

Related Questions