xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Map always centers on coords 0,0 when used inside ListView/CollectionView and HasZoomEnabled = false on Android #11115

Open TimLariviere opened 4 years ago

TimLariviere commented 4 years ago

Description

When using a Xamarin.Forms.Maps.Map control inside a row of ListView or CollectionView, with HasZoomEnabled = false, Android centers the map on 0,0 instead of using the given initial region. This also happens if I try to use MoveToRegion before the Map is fully initialized.

But if HasZoomEnabled is true, it works as expected.

This doesn't happen on iOS, nor does it happen when using the same Map control outside a ListView/CollectionView.

Steps to Reproduce

  1. (To avoid x:Arguments) Create a CustomMap class inheriting from Map

    public class CustomMap : Map
    {
    public CustomMap()
        : base(new MapSpan(new Position(48.866667,  2.333333), 5.0, 5.0)) // Should be centered on Paris
    {
    }
    }
  2. Add a CollectionView with an ItemTemplate using the CustomMap with HasZoomEnabled = false

    <CollectionView x:Name="CollectionView">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <local:CustomMap HeightRequest="150" HasZoomEnabled="False" />
        </DataTemplate>
    </CollectionView.ItemTemplate>
    </CollectionView>
  3. Pass some data in the ItemsSource property so the rows are displayed

CollectionView.ItemsSource = new[] {1,2,3,4,5,6};

Expected Behavior

The maps should all be centered on the position given (Paris in the example). Just like on iOS:

Actual Behavior

The maps are centered on the default coordinates.

Basic Information

Workaround

No workaround known.

johannperez commented 3 years ago

I am able to reproduce this issue without a CollectionView, just by having the Zoom disabled and setting the new coordinates using MoveToRegion, it won't update.

wocar commented 3 years ago

Is this getting fixed?

xleon commented 2 years ago

Super hacky workaround: Set HasZoomEnabled=True And then:

Device.InvokeOnMainThreadAsync(async () =>
{
    await Task.Delay(100);
    Map.HasZoomEnabled = false;
});