amay077 / Xamarin.Forms.GoogleMaps

Map library for Xamarin.Forms using Google maps API
https://www.nuget.org/packages/Xamarin.Forms.GoogleMaps/
MIT License
546 stars 346 forks source link

Custom BitmapDescriptor caching not working when FromView used #619

Open jocontacter opened 5 years ago

jocontacter commented 5 years ago

VERSIONS

PLATFORMS

ACTUAL BEHAVIOR

Too low performance map.Add(pin) when used BitmapDescriptorFactory.FromView(on Samsung 8 for 95 pins ~14sec)

My workaround to not to use BitmapDescriptorFactory.FromView() but use BitmapDescriptorFactory.FromStream(null, some_id). You hid Utils class so I was forced to copypaste that code to my project and in CachingNativeBitmapDescriptorFactory cache Created AndroidBitmapDescriptor's from view. In such way performance is very well.

public AndroidBitmapDescriptor ToNative(BitmapDescriptor descriptor)
{
    if (!string.IsNullOrEmpty(descriptor.Id))
    {
        var discountText = descriptor.Id.Split("%");

        var cacheEntry = _cache.GetOrAdd(descriptor.Id, _ => Convert(new PinView(discountText[0]+"%", discountText[1] == "1" ? true : false)));
        return cacheEntry;
    }

    return DefaultBitmapDescriptorFactory.Instance.ToNative(descriptor);
}

private AndroidBitmapDescriptor Convert(Xamarin.Forms.View iconView)
{
    var nativeView = Utils.ConvertFormsToNative(
        iconView,
        new Xamarin.Forms.Rectangle(0, 0, Utils.DpToPx((float)iconView.WidthRequest), Utils.DpToPx((float)iconView.HeightRequest)),
        Xamarin.Forms.Platform.Android.Platform.CreateRendererWithContext(iconView, CrossCurrentActivity.Current.Activity));
    var otherView = new FrameLayout(nativeView.Context);
    nativeView.LayoutParameters = new FrameLayout.LayoutParams(Utils.DpToPx((float)iconView.WidthRequest), Utils.DpToPx((float)iconView.HeightRequest));
    otherView.AddView(nativeView);
    return Utils.ConvertViewToBitmapDescriptor(otherView);
}
mehmetulutug commented 5 years ago

hello jocontacter;

can u share the utils library ?

jocontacter commented 5 years ago

hello jocontacter;

can u share the utils library ?

check message attaches If you want some example - welcome

AndroidUtils.txt iOSUtils.txt

dejanbasic commented 4 years ago

Hey @jocontacter could you please show me example of using the utils library?