ThinkGeo.com    |     Documentation    |     Premium Support

How to update a feature in a layer

Hi

At the moment we are moving or updating the information or the style of a feature in a layer replacing the feature in the InternalFeatures collection.
Something similar to:
private bool UpdateNodePosition()
{
if (m_UpdatingNodes)
{
return false;
}

    m_UpdatingNodes = true;

    bool isOpen = m_LayerNodes.IsOpen;
    if (!isOpen)
    {
        m_LayerNodes.Open();
    }

    try
    {
        GeoCollection<Feature> coll = m_LayerNodes.InternalFeatures;

        try
        {
            string nodeState = NODESTATUS_OK;

            if (m_CurrExtendLatLon != null)
            {
                double angle = Math.PI * m_PosState / 180.0;

                m_PosState += 15;
                if (m_PosState >= 360)
                {
                    m_PosState = 0;
                }

                double latitude = m_CurrExtendLatLon.GetCenterPoint().Y;
                double longitude = m_CurrExtendLatLon.GetCenterPoint().X;

                latitude += Math.Sin(angle) * NODE_MOVE_RADIUS;
                longitude += Math.Cos(angle) * NODE_MOVE_RADIUS;

                Feature feature = new Feature(longitude, latitude, KEY_Node);
                feature.ColumnValues[NODE_TITLE_COLNAME] = "Test";

                if (!String.IsNullOrEmpty(nodeState))
                {
                    feature.ColumnValues[NODE_STATE_COLNAME] = nodeState;
                }

                if (coll.Contains(feature.Id))
                {
                    coll.Remove(feature.Id); // remove the old feature if it already exists
                }

                coll.Add(feature.Id, feature);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(String.Format("Unable to create the feature from the element\n{0}", ex));
        }
    }
    finally
    {
        if (!isOpen)
        {
            m_LayerNodes.Close();
        }

        m_UpdatingNodes = false;
    }

    return true;
}

The UI presentation of the feature is obtained by a ValueStyle linked to the Column value.

After the features are updated then I need to call a refresh to the overlay contaning the layer:

private async Task TestMoveNodeAsync()
{
    if (UpdateNodePosition())
    {
        await m_OverlayNodes.RefreshAsync();

    }
}

where m_OverlayNodes is:

private readonly FeatureLayerWpfDrawingOverlay m_OverlayNodes;

If the refresh is not called then the feature is not moved/updated.

Is there a better way to move/update the feature without calling the refresh ?

Thanks
Domenico

Hi Domenico,

I’m wondering why you have concerns calling await m_OverlayNodes.RefreshAsync(); FeatureLayerWpfDrawingOverlay.Refresh() is really efficient for your case (where it doesn’t have lots of features in the layer), or did you see some issues calling this method?

Thanks,
Ben

Hi Ben

No. I see no issue calling the RefreshAsync. And it is working OK in our application.

I was in doubt when the RefreshAsync is necessary. For example in an other thread if learn that it was not necessary to call RefreshAsync after the visibility was changed.

Do you have some suggestions about when it is necessary to RefreshAsync in order to show the changes and when it is not necessary ?

Thanks

Best regards

Domenico

Hi Domenico,

I see. In general, RefreshAsync is needed whenever you want to force the overlay to render its changes on the screen. The only exception I can think of is when you change overlay.IsVisible . In that case, RefreshAsync is called internally—otherwise, the previously rendered content would be visible and cause the map to display incorrectly.

Thanks,
Ben