How to set MapProvider in xaml?

Topics: WPF
Mar 14, 2012 at 9:36 AM

Is it no longer  possible to set the MapProvider in xaml like you could with the MapType property?

If possible, how? I just end up with XamlParseExceptions whatever I try...

Mar 14, 2012 at 11:09 AM

Hi, try this code

    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private MapProvidersHelper _mapProvidersHelper = new MapProvidersHelper() { MapProvider = OpenStreetMapProvider.Instance};
        public MapProvidersHelper MapProvidersHelper
        {
            get { return _mapProvidersHelper; }
            set
            {
                if (ReferenceEquals(value, _mapProvidersHelper)) return;
                _mapProvidersHelper = value;
                NotifyPropertyChanged("MapProvidersHelper");

            }
        }
    }


    public sealed class MapProvidersHelper:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }


        private GMapProvider _provider;

        public GMapProvider MapProvider
        {
            get { return _provider; }
            set
            {
                if(ReferenceEquals(value,_provider)) return;
                _provider = value;
                NotifyPropertyChanged("MapProvider");
            }
        }
    }
And xaml 
<Window x:Class="MapProvider.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:GMap="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation"               
        DataContext="{Binding RelativeSource={RelativeSource Self},Path=MapProvidersHelper}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <GMap:GMapControl Margin="1.25" MapProvider="{Binding MapProvider}" MinZoom="1" MaxZoom="30" Zoom="12"/>
    </Grid>
</Window>