Command AddVertex

4889
12
11-12-2014 12:41 AM
JonasLeksell
New Contributor II

Hi All,

We are trying to use the
command AddVertex to edit existing polygons and like to add this using XAML
code: e.g. <Button Content="Add Vertex" Command="{Binding
AddVertex}" CommandParameter="{Binding GPSPosition}" … /> where
GPSPosition is a function in the code behind. However using the command
parameter with the gps function or not we seem not to be able to get this
button to activate when editing.

Has anyone looked at this
Before and have a solution on this? Or know if we are addressing this the wrong way?

Tags (1)
0 Kudos
12 Replies
AnttiKajanus1
Occasional Contributor III

What DataContext you are using here? Are you using Editor.AddVertex command?

If you have map in the same view, try to use elementName binding to the myMapView.Editor.AddVertex and then bind normally to your command parameter that is coming from DataContext.

0 Kudos
JonasLeksell
New Contributor II

Hi Antti,

Thx, for checking in to this question. Yes I am using the ICommand AddVertex from MapView.Editor and
are using the following DataContext:

DataContext="{Binding ElementName=mapView, Path=Editor}"

I am also using command as Undo, Redo and Cancel, and those are activated when I editing my map.

By some reason the button connected to the AddVertex command never gets in a valid state because it
will not become clickable.

Do you have a sample showing how this should work?

Regards,

Jonas

0 Kudos
AnttiKajanus1
Occasional Contributor III

You can do it something like this, not very nice sample but you get the point

View : Notice binding

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

     <esri:MapView x:Name="MyMapView"

        LayerLoaded="MyMapView_LayerLoaded">

        <esri:Map>

        <layers:ArcGISTiledMapServiceLayer ID="Basemap"

            ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>

        </esri:Map>

    </esri:MapView>

       

    <Button Command="{Binding ElementName=MyMapView, Path=Editor.AddVertex}" CommandParameter="{Binding GPSLocation}"

                Background="Green"

                Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Top"

                >add vertex</Button>

</Grid>

ViewModel

public class MainPageViewModel : INotifyPropertyChanged

    {

        DispatcherTimer timer;

        public MainPageViewModel()

        {

            timer = new DispatcherTimer()

            {

                Interval = new TimeSpan(0, 0, 3)

            };

            timer.Tick += timer_Tick;

            timer.Start();

            GPSLocation = new MapPoint(0, 0, SpatialReferences.WebMercator);

        }

        void timer_Tick(object sender, object e)

        {

            var random = new Random(DateTime.Now.Millisecond);

            GPSLocation = new MapPoint(random.Next(-10000000, 10000000), random.Next(-1000000,1000000), GPSLocation.SpatialReference);

        }

        private MapPoint _gpsLocation;

        public MapPoint GPSLocation

        {

            get { return _gpsLocation; }

            set { _gpsLocation = value; RaisePropertyChanged("GPSLocation"); }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string name)

        {

            if (PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(name));

            }

        }

    }

And set DataContext to new MainPageViewModel and activate editor. For example like this

public MainPage()

{

    this.InitializeComponent();

  DataContext = new MainPageViewModel();

  MyMapView.NavigationCompleted += MyMapView_NavigationCompleted;

}

private async void MyMapView_NavigationCompleted(object sender, EventArgs e)

{

    MyMapView.NavigationCompleted -= MyMapView_NavigationCompleted;

    try

  {

  var geom = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon);

  }

  catch (Exception)

  {

  // todo

  }

}

Hope this helps.

JonasLeksell
New Contributor II

Hi Antti,

Thx, great sample I could get it to work, and I tried to modify the code I have to use DataContext="{Binding
ElementName=mapView, Path=Editor.AddVertex}" instead of calling on Command=”AddVertex”.

What I the saw was that the AddVertex button is always active, meaning it does not check if it is an
edit session as I get with the other commands.

Thanks for the help, we will do some more testing on this,

Regards,
Jonas

AnttiKajanus1
Occasional Contributor III

Hmm, it should check it. Are you able to create a repro for me to look at?

0 Kudos
JonasLeksell
New Contributor II

Hi Antti,

Forget it...

I did set it in DataContext instead of Command. That´s why I got that behavior.

Regards,

Jonas

0 Kudos
AnttiKajanus1
Occasional Contributor III

Cheers. Remember to mark topic answered.

0 Kudos
MartinBrzezinka
New Contributor

Dear Antti

I'm the reason for Jonas' question.
We have found you answer very helpful and I want to thank you for that.

However, it worked for us in your stand-alone application but not in our big project.
It turns out the difference was that you were always drawing new polygons
while we have been trying to edit present ones...

After some more digging, we have found out that the AddVertex button won't get enabled
if MapView.Editor.EditorMode == EditGeometry. It works only for EditorMode == Draw.

Could you please confirm this and accept as a bug?

Another question regarding AddVertex
The newly added point ends up at the end of the vertex list (last in a line)
regadless of what has been highlighted by the user.
Is there any way of telling to the Editor that the point should be added after marked VertexPosition?
If not, I think it should be very high on your todo - list.

Best regards,
Martin Brzezinka
Sweden

0 Kudos
AnttiKajanus1
Occasional Contributor III

Hey Martin,

Editor is designed to perform edits on a map. This means that when EditorMode is set to Draw, it enables Editors functionality and in this case enables AddVertex. If you are editing geometry without performing it on the map, then Editor most likely isn't correct way to do it. If you're making changes to the geometry outside of the map, you should be modifying geometry directly using builders like PolygonBuilder Class

Here is small example how to modify existing polygon

PolygonBuilder builder = new PolygonBuilder(polygon); // Create builder from existing polygon
builder.AddPoint(new MapPoint(x, y)); // Add point to end
builder.Parts[0].InsertPoint(index, new MapPoint(x, y)); // Insert point into specific location
graphic.Geometry = builder.ToGeometry(); // Create geometry and assign it back to the graphic / feature

I noticed that we haven't documented using Builders very well but we are going to improve that part.

About the second part, can you define a bit more detail the use case behind it and what is the workflow that you are looking from the editor?

0 Kudos