ObservableCollection is not updating (using xamarin.forms.maps)

Alexandru STOICA (94595) 1 Reputation point
2021-01-15T16:37:18.327+00:00

i made a custom map thanks to Bind to https://stackoverflow.com/questions/28098020/bind-to-xamarin-forms-maps-map-from-viewmodel. but when i add pins from the viewmodel i dont see them in the view.

<maps:BindableMap MapSpan="{Binding MapSpan}" PinsSource="{Binding Pins}"/>
and the viewmodel code

    public ObservableCollection<Pin> Pins
    {
        get
        {
            return _pins;
        }
        set
        {
            _pins = value;
            OnPropertyChanged(nameof(Pins));
        }
    }

Pins.Add(new Pin { Address=address, Position =position, Label=label, Type=type});
still can't see no pins..

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Essentials;
using System.ComponentModel;
using System.Collections.ObjectModel;
using FoodApp.Utils;
using FoodApp.Services.Maps;
using System.Linq;
using System.Collections.Specialized;

namespace FoodApp.ViewModels.Maps
{
    public class MapViewModel : INotifyPropertyChanged
    {
        private IDependencyService _dependencyService;
        private MapSpan _mapSpan;
        private ObservableCollection<Pin> _pins;
        private Location _location;

        public event PropertyChangedEventHandler PropertyChanged;

        public MapSpan MapSpan
        {
            get => _mapSpan;
            set
            {
                _mapSpan = value;
                OnPropertyChanged(nameof(MapSpan));
            }
        }

        public ObservableCollection<Pin> Pins
        {
            get
            {
                return _pins;
            }
            set
            {
                _pins = value;
                OnPropertyChanged(nameof(Pins));
            }
        }

        public MapViewModel() : this(new DependencyServiceWrapper())
        {

        }

        public MapViewModel(IDependencyService dependencyService)
        {
            _dependencyService = dependencyService;
            _location = new Location();
            MapSpan = MapSpan.FromCenterAndRadius(new Position(0,0), new Distance());
            _pins = new ObservableCollection<Pin>();

            UpdateStartLocation();
            UpdatePins();
        }

        public async void UpdateStartLocation()
        {
            try
            {
                _location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Best));

                if (_location != null)
                {
                    MapSpan = MapSpan.FromCenterAndRadius(new Position(_location.Latitude, _location.Longitude), new Distance());
                    AddPin("", new Position(_location.Latitude, _location.Longitude), "You", PinType.Generic);
                } else
                {
                    // Could not get the location
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception
            }
            catch (FeatureNotEnabledException fneEx)
            {
                // Handle not enabled on device exception
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception
            }
            catch (Exception ex)
            {
                // Unable to get location
            }
        }

        public async void UpdatePins()
        {
            var apiPins = await _dependencyService.Get<IMapPinService>().GetPins();
            foreach(var pin in apiPins)
            {
                AddPin(pin);
            }
        }

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void AddPin(string address, Position position, string label, PinType type)
        {
            Pin newPin = new Pin { Address = address, Position = position, Label = label, Type = type };
            if (CanAddPin(newPin))
            {
                Pins.Add(newPin);
            }
        }

        public void AddPin(Pin pin)
        {
            if (CanAddPin(pin))
            {
                Pins.Add(pin);
            }
        }

        public bool CanAddPin(Pin pin)
        {
            return true;
        }
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,298 questions
{count} votes