How to add a graphics layer to a webmap.map

2788
4
08-03-2015 08:59 AM
MattMiley1
New Contributor

I just need to add all my layers from a webmap to a map then add a graphics layer, is this possible?

I keep getting this error "cannot modify layers collection directly when managed by webmapviewmodel"

0 Kudos
4 Replies
MattMiley1
New Contributor

I found a work around

// Loads the given webmap

        private async Task LoadWebMapAsync(string wmId)

        {

            try

            {

                _portal = await ArcGISPortal.CreateAsync();

                var item = await ArcGISPortalItem.CreateAsync(_portal, wmId);

                var webmap = await WebMap.FromPortalItemAsync(item);

                var vm = await WebMapViewModel.LoadAsync(webmap, _portal);

              

                foreach (var layer in vm.Map.Layers)

                {

                    //Add each layer individually

                    Map.Layers.Add(layer);

                }

            }

            catch (Exception ex)

            {

                var vmr = new ErrorMessagesClass();

                vmr.ErrorMessages(ex.Message);

            }

        }

0 Kudos
MattMiley1
New Contributor

My work around doesnt work, it crash the next time i to any kind of async after loading the web map. Does anyone have any ideas how to mix webmap layers with graphic layers?

0 Kudos
MattBryant1
New Contributor

I struggled with the same issue.  The map on the WebMapViewModel is readonly.  I think the proper approach is to add graphic overlays to the map view control itself.  The nicest part is this can be done with xaml.

Capture.PNG

You'll need to create a renderer for each graphic which also can be done with xaml.

Capture.PNG

You can add and remove individual graphics in code behind by accessing the graphic overlays collection.

Capture.PNG

Hope that helped.

MichaelBranscomb
Esri Frequent Contributor

Hi,

Matt is correct that graphics overlays are the way to overlay graphics on a webmap. The webmap spec in fact does not contain a definition for graphics, instead these are handled as feature collections. In order to persist client-side graphics in a webmap they need to be push in as a featurecollection (ArcGIS web map JSON format​).

On the roadmap for the .NET API is full support for reading and writing webmaps (in the runtime they are the 'map') at which time we plan to have a featurecollection type which is effectively an in-memory feature layer and will be persisted in any maps you save.

Cheers

Mike