Is it possible to set dimensions to a groundOverla

2019-02-14 03:26发布

问题:

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);

回答1:

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 :)



回答2:

I had the same issue. If you want to place an image, you can use the default markers provided by Google maps API. You can set a custom image as the marker icon, which will remain the same size.

private final List<BitmapDescriptor> mImages = new ArrayList<BitmapDescriptor>();
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.YOUR_IMAGE));
Marker marker = map.addMarker(new MarkerOptions().icon(mImages.get(0)));

This example shows how to set the icon of the marker, from an element of an image list. Hope it helps.