Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin-Forms | Android - Reference image in GroundOverlayOptions

I am using the Android.gms.maps class to create a custom renderer google map in my Xamarin Forms app, for android phones. I am looking to add an image to my map using GroundOverlayOptions

    GroundOverlayOptions newarkMap = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromResource(/*My image?*/))
    .position(e.Point, 8600f, 6500f);
     map.AddGroundOverlay(newarkMap);

I need the images resource ID in the commented section of my code. How do I find out an images' resource ID? Is there an alternative way to do it?

Upvotes: 1

Views: 177

Answers (1)

SushiHangover
SushiHangover

Reputation: 74144

Using BitmapDescriptorFactory you have multiple options available to how you obtain the BitmapDescriptor

  • FromResource
  • FromAsset
  • FromPath
  • FromBitmap

Couple of examples:

To use the FromResource, the image must be within your Resource tree, assumably within the Drawable subfolder, Xamarin will generate an id descriptor for you in the form of Resource.Drawable.XXXXX and that will be an integer constant:

var overlayOption = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromResource(Resource.Drawable.overlayface2))
    .Position(e.Point, 5000f, 5000f);

enter image description here

FromAsset loads the image from the "Assets" subfolder:

var overlayOption = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromAsset("OverlayFace.png"))
    .Position(e.Point, 5000f, 5000f);

enter image description here

FromPath loads the image from an arbitrary path, so you could load an image you downloaded and stored in the app's cache directory, or an image stored on an sdcard, etc...

var overlayOption3 = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromPath("/mnt/sdcard/Download/OverlayFace.png"))
    .Position(e.Point, 5000f, 5000f);

Note: The largest problem people run into is not setting the size of the image correctly, it is in kilometers. not pixels, not miles, etc...

Note: Also for speed, keep your image sizes in the power of 2, otherwise the the map library will have to transform it into a power of 2 image everytime it loads the overlay (memory and performance hit)

Upvotes: 1

Related Questions