ReactiveUI Google 地图

ReactiveUI Google Maps

问题:我的 ViewModel 中有一个 IReactiveDerivedList<SensorViewModel>,我想订阅它的更改,包括在我观察它之前列表中已经存在的内容。然后将其输入 GoogleMaps for Xamarin Android.

当我添加一些东西时这似乎有效:

public void OnMapReady(GoogleMap map)
{
    _itemsAdded = this.ViewModel.Sensors.ItemsAdded
            .ObserveOn(RxApp.MainThreadScheduler)
            .Select(s => new CircleOptions()
                .InvokeCenter(new LatLng(s.Latitude, s.Longitude))
                .InvokeRadius(1000)
                .InvokeFillColor(Color.Blue.ToArgb())
                .InvokeStrokeColor(Color.Red.ToArgb()))
            .Subscribe(
                Observer.Create<CircleOptions>(options => map.AddCircle(options)));
}

但我还需要跟踪从 map.AddCircle 返回的 Circle,以便在对象消失时将其从地图中删除。处理这种情况的反应方式是什么?

我不知道这是否是最被动的方式,但我想出了一些似乎可行的方法:

    private IDisposable _itemsAdded;
    private IDisposable _itemsRemoved;

    private readonly Dictionary<string, Circle> _circleMap = new Dictionary<string, Circle>();

    public void OnMapReady(GoogleMap map)
    {
        _circleMap.Clear();

        _itemsAdded = this.ViewModel.Sensors.ItemsAdded
            .StartWith(this.ViewModel.Sensors)
            .Subscribe(s =>
            {
                var options = new CircleOptions()
                    .InvokeCenter(new LatLng(s.Latitude, s.Longitude))
                    .InvokeRadius(1000)
                    .InvokeFillColor(Color.Blue.ToArgb())
                    .InvokeStrokeColor(Color.Red.ToArgb());

                var circle = map.AddCircle(options);
                this._circleMap.Add(s.Id, circle);
            });

        _itemsRemoved = this.ViewModel.Sensors.ItemsRemoved
            .Subscribe(s =>
            {
                Circle circle = null;
                if (this._circleMap.TryGetValue(s.Id, out circle))
                {
                    this._circleMap.Remove(s.Id);
                    circle.Remove();
                }
            });
    }