我使用MVVMCross用GoogleMaps开发了简单的Xamarin.Forms应用程序。我的目标是在启动 map 时将 map 的位置居中到用户的当前位置。不幸的是,我不知道如何将这些值绑定(bind)到GoogleMaps的构造函数。现在它只是静态值,但我想从CurrentLocation对象(具有这些属性)传递纬度和经度值。

看法:

<ContentPage.Content>
    <maps:Map MapType="Street" IsShowingUser="True" HasZoomEnabled="True">
        <x:Arguments>
            <maps:MapSpan>
                <x:Arguments>
                    <maps:Position>
                        <x:Arguments>
                            <x:Double>56.368533</x:Double>
                            <x:Double>3.258646</x:Double>
                        </x:Arguments>
                    </maps:Position>
                    <x:Double>0.01</x:Double>
                    <x:Double>0.01</x:Double>
                </x:Arguments>
            </maps:MapSpan>
        </x:Arguments>
    </maps:Map>
</ContentPage.Content>

这是我的ViewModel的一部分:
public class NearbyBollardsMapViewModel : MvxViewModel
{
    private Location _currentLocation;

    public Location CurrentLocation
    {
        get => _currentLocation;
        set
        {
            _currentLocation = value;
            RaisePropertyChanged(() => CurrentLocation);
        }
    }

    public NearbyBollardsMapViewModel(ILocationService locationService)
    {
        this._locationService = locationService;
        CurrentLocation = _locationService.GetCurrentUsersLocation().Result;
    }
}

最佳答案

您需要创建一个从Maps扩展的PCL类,并向其中添加BlindableProperty。

1º-创建一个类,并从Xamarin.Forms.Maps.Map进行扩展

(我包括了Bindable属性的代码,您可以阅读更多Here)

public class BindableMap : Xamarin.Forms.Maps.Map
{
    public BindableMap()
    {

    }

    public MapSpan MapSpan
    {
        get { return (MapSpan)GetValue(MapSpanProperty); }
        set { SetValue(MapSpanProperty, value); }
    }

    public static readonly BindableProperty MapSpanProperty = BindableProperty.Create(
                                                     propertyName: "MapSpan",
                                                     returnType: typeof(MapSpan),
                                                     declaringType: typeof(BindableProspectDetailMap),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay,
                                                     validateValue: null,
                                                     propertyChanged: MapSpanPropertyChanged);

    private static void MapSpanPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        try
        {
            var thisInstance = bindable as BindableProspectDetailMap;
            var newMapSpan = newValue as MapSpan;

            thisInstance?.MoveToRegion(newMapSpan);
        }
        catch (Exception ex)
        {

        }
    }

}

2º-在ViewModel中添加将用于绑定(bind)MapSpan的属性
private MapSpan _mapSpanView;
public MapSpan MapSpanView
{
    get { return _mapSpanView; }
    set { SetProperty(ref _mapSpanView, value); }
}

...

Position position = new Position((double)Latitude, (double)Longitude);
MapSpanView = MapSpan.FromCenterAndRadius(
                            prospectPosition,
                            Distance.FromMeters(2500)
                        );

3º-然后将其绑定(bind)到xaml中
xmlns:map="clr-namespace:YourNameSpace.Mobile.TheFolderWhereTheClassIs"

...

<StackLayout x:Name="mapHolder">
    <map:BindableMap
                    x:Name="map"
                    MapSpan="{Binding MapSpanView}"
                    MapType="Street" IsShowingUser="True" HasZoomEnabled="True"/>
</StackLayout>

关于c# - 将构造函数参数绑定(bind)到Xamarin.Forms的GoogleMaps,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60515196/

10-12 07:40
查看更多