VladislavAntonyuk/MauiSamples

Resolved issue with Android not placing center image

GenCodeInc opened this issue · 0 comments

For me, the Android CustomPin was centering right left, but top bottom it was anchored to the bottom, unlikes ios
Added these lines to resolve the issue in Android AddPins

---- markerOption.Anchor(0.5f, 0.5f); // Set the anchor point to the center
---- markerOption.Anchor(0.5f, 0.5f); // Set the anchor point to the center for non-custom pins as well

private void AddPins(IEnumerable<IMapPin> mapPins)
{
    if (Map is null || MauiContext is null)
    {
        return;
    }

    foreach (var pin in mapPins)
    {
        var pinHandler = pin.ToHandler(MauiContext);
        if (pinHandler is IMapPinHandler mapPinHandler)
        {
            var markerOption = mapPinHandler.PlatformView;
            if (pin is CustomPin cp)
            {
                cp.ImageSource.LoadImage(MauiContext, result =>
                {
                    if (result?.Value is BitmapDrawable { Bitmap: not null } bitmapDrawable)
                    {
                        var bitmap = GetMaximumBitmap(bitmapDrawable.Bitmap, 100, 100);
                        markerOption.SetIcon(BitmapDescriptorFactory.FromBitmap(bitmap));
                        markerOption.Anchor(0.5f, 0.5f); // Set the anchor point to the center
                    }

                    AddMarker(Map, pin, markerOption);
                });
            }
            else
            {
                markerOption.Anchor(0.5f, 0.5f); // Set the anchor point to the center for non-custom pins as well
                AddMarker(Map, pin, markerOption);
            }
        }
    }
}