Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set dimensions to a groundOverlay that is independent of the map zoom level?

I have a GroundOverlay on my GoogleMap and I want that its dimensions to not change when I zoom in/out on map. Exact like default map markers that always keep their dimensions. I have tried with both forms of the GroundOverlay.setDimensions() but the image is still resize on zoom. Here is my code:

 Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.map_arrow);
 BitmapDescriptor arrow = BitmapDescriptorFactory.fromBitmap(btm);
 float w = btm.getWidth();
 float h = btm.getHeight();

if (groundOverlay != null) groundOverlay.remove();
 groundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
            .image(arrow).position(meLoc, w,h).bearing(bearAngle));
 groundOverlay.setDimensions(1000);
like image 404
AlexAndro Avatar asked Nov 03 '22 02:11

AlexAndro


1 Answers

you have your width and heigth of overlay and placed it on the map according to your zoom level. It seems good for that zoom level. Right? Now you can calculate radius and get meters of your map screen. Because map background overlay width and height values are meters. We have to go with meter not zoom level or anything else. Maybe someone can find a better solution but I have tried too many ways, and end up with this solution and it worked very well.

    float zoomLevel=mMap.getCameraPosition().zoom;
    //calculate meters*********************         
    myBounds = mMap.getProjection().getVisibleRegion().latLngBounds;
    myCenter=  mMap.getCameraPosition().target;
    if (myCenter.latitude==0 || myCenter.longitude==0) {
        myCenter=new LatLng(myLocation.getLatitude(),myLocation.getLongitude());
    }

    LatLng ne = myBounds.northeast;

    // r = radius of the earth in statute miles
    double r = 3963.0;  

    // Convert lat or lng from decimal degrees into radians (divide by 57.2958)
    double lat1 = myCenter.latitude / 57.2958; 
    double lon1 = myCenter.longitude / 57.2958;
    final double lat2 = ne.latitude / 57.2958;
    final double lon2 = ne.longitude / 57.2958;

    // distance = circle radius from center to Northeast corner of bounds
    double dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) + 
      Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));

    //1 Meter = 0.000621371192237334 Miles
    double meters_calc=dis/0.000621371192237334;

    float factor=1;  
    if (zoomLevel==15) {  // my default zoom level yours can be different
        metersoverlay=meters_calc;  // global variable metersoverlay set
    }
    else { // if my zoom level change then I have to calculate dimension scale factor
        factor=(float) (meters_calc/metersoverlay); 
    }

    //******************************* now we are ready to set dimension of background overlay
    float dimensions=1000*factor; 
    loadingGroundOverlayBg.setDimensions(dimensions);

I hope it works for all of you :)

like image 116
ferhatbalaban Avatar answered Nov 15 '22 13:11

ferhatbalaban