GMapControl Position property

Topics: Feature Requests
Oct 27, 2011 at 10:22 AM

I have problem with Position property in GMapControl. I can't set binding to this property because it is not DependencyProperty. Why it's not DependencyProperty?

Coordinator
Oct 27, 2011 at 11:32 AM

make it so ;}

Oct 28, 2011 at 10:07 PM

We are working on a friendly WPF/XAML GMapControl on the fork WPFMVVM. Check it out

Oct 31, 2011 at 12:42 PM
radioman wrote:

make it so ;}

Im afraid to crash something. :( 

Oct 31, 2011 at 12:43 PM
JLuisEstrada wrote:

We are working on a friendly WPF/XAML GMapControl on the fork WPFMVVM. Check it out

Position still not DependencyProperty.

Oct 31, 2011 at 2:51 PM

Working on it

Oct 31, 2011 at 3:47 PM

Still working on a few changes, but if you got some urgency in the implementation, remove the property Position on your GMapControl and replace it with this

 

/// <summary>
        /// current coordinates of the map center
        /// </summary>
        [Category("GMap.NET")]
        public PointLatLng Position
        {
            get { return (PointLatLng)GetValue(PositionProperty); }
            set { SetValue(PositionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Position.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PositionProperty =
            DependencyProperty.Register("Position", typeof(PointLatLng), typeof(GMapControl), new UIPropertyMetadata(PointLatLng.Zero,new PropertyChangedCallback(OnPositionChanged)));


        public static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            GMapControl map = (GMapControl)d;
            if (map != null)
            {
                map.Core.CurrentPosition = (PointLatLng)e.NewValue;
                map.UpdateMarkersOffset();
            }
        }

That should do the trick.

Oct 31, 2011 at 5:52 PM

Uploaded the code here http://greatmaps.codeplex.com/SourceControl/network/Forks/rog1039/WPFMVVM/changeset/changes/9937f502800a

Also, added a sample window in the WPF Demo

 

cheers

Nov 1, 2011 at 10:39 AM

Thanks a lot! It works fine.

Nov 2, 2011 at 12:20 PM

I am also changed Markers field of GMapControl.

From this:

 

public readonly ObservableCollection<GMapMarker> Markers = new ObservableCollection<GMapMarker>();

 

To this:

 

public ObservableCollection<GMapMarker> Markers
{
	get { return (ObservableCollection<GMapMarker>)GetValue(MarkersProperty); } 
	set { SetValue(MarkersProperty, value); } 
}

public static readonly DependencyProperty MarkersProperty = DependencyProperty.Register("Markers", typeof(ObservableCollection<GMapMarker>), typeof(GMapControl));

 

Not tested yet.

Nov 2, 2011 at 1:26 PM
Edited Nov 2, 2011 at 1:27 PM

Markers binding doesn't work.

I write in XAML something like that:

 

Markers="{Binding Path=Markers, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"

 

In my ViewModel class i have this property:

 

public ObservableCollection<GMapMarker> Markers { get; private set; }

 

When i add markers using Markers.Add(marker) in ViewModel it doesn't actually update the map.

Any solutions?

Nov 2, 2011 at 1:43 PM
Edited Nov 2, 2011 at 1:44 PM

Also it doesn't works in code. I tried to add  marker directly on map:

Map.Markers.Add(marker);

Not works!

But if i change back from DependencyProperty to simple field, the same code works fine.

Whats the trick? :(

Coordinator
Nov 2, 2011 at 1:47 PM

because by default, map.ItemSource is set to Markers field

Nov 2, 2011 at 2:06 PM
Edited Nov 2, 2011 at 2:06 PM

Is it possible to make Markers dependency property?

Coordinator
Nov 2, 2011 at 2:11 PM

just set ItemSource to your dependency collection

Nov 2, 2011 at 2:54 PM
One of the changes I did first was to change the ItemsSource reference to Items reference, and managed to copy the Markers to Items, so, you can add markes with Items (in xaml), ItemsSource (through binding) or code with markers (which i dont recommend on wpf because breaks the MVVM phylosophy)

Check out our fork

Sent from my iPhone.
Excuse the typos

On 02/11/2011, at 08:11, "radioman"<notifications@codeplex.com> wrote:

From: radioman

just set ItemSource to your dependency collection

Nov 3, 2011 at 5:50 AM

So i found solution. I rollback all changes to Markers in GmapControl and just set binding to ItemsSource property in XAML to my Markers collection (in ViewModel). It works. Thank you guys.

<gmap:GMapControl MaxZoom="17" MinZoom="1" 
MapProvider="{Binding MapProvider}" Zoom="{Binding Zoom}" Position="{Binding Position}" 
ItemsSource="{Binding Path=Markers, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

Nov 16, 2011 at 1:03 PM

JLuisEstrada, I found bug with Position dependency property. When i drag map Position should change, but it is not. I set breakpoint in method OnPositionChanged. It raise only when i set Position manually in my code, but not when i just drag the map. Any ideas how to fix it?

Coordinator
Nov 16, 2011 at 1:11 PM

try OnMapDrag

Nov 16, 2011 at 3:30 PM

I noticed that at least in the WPF control, the Markers collections seems out of place. It breaks with phylosophy of WPF (because I suppose, Markers property comes from the winforms control). So, my suggestion is to remove it and start promoting use the ItemsSource property for binding and the Items property for internal iterations

Nov 17, 2011 at 5:22 AM

radioman

I try to handle this event and OnCurrentPositionChanged event also by using EventToCommand (MVVM Light Toolkit). Binding works fine, but these events never raised.

Here is my XAML for GMapControl:

 

<i:Interaction.Triggers>
	<i:EventTrigger EventName="OnMapDrag">
		<cmd:EventToCommand Command="{Binding MapDragCommand}"/>
	</i:EventTrigger>
	<i:EventTrigger EventName="OnCurrentPositionChanged">
		<cmd:EventToCommand Command="{Binding CurrentPositionChangedCommand}"/>
	</i:EventTrigger>
</i:Interaction.Triggers>

Here is my code in ViewModel:

protected DelegateCommand CommandMapDrag;

public ICommand MapDragCommand
{
	get { return CommandMapDrag ?? (CommandMapDrag = new DelegateCommand(DragMap)); }
}

private void DragMap()
{
	OnPropertyChanged("Position");
}

protected DelegateCommand CommandCurrrentPositionChanged;

public ICommand CurrentPositionChangedCommand
{
	get { return CommandCurrrentPositionChanged ?? (CommandCurrrentPositionChanged = new DelegateCommand(CurrentPositionChanged)); }
}

private void CurrentPositionChanged()
{
	OnPropertyChanged("Position");
}