Dynamic pins & ShapeIndex

I am putting pins on a map dynamically and it appears that ShapeIndex is always zero…and I am creating a new layer for every pin?
I want to do a identify shape and get a field of data based on the ShapeIndex but it is always zero with the code below…is there a better way to dynamically place pins?

Blockquote

    public void mapPins(string callLabel, double gridLat, double gridLong)
    { 
        var places = new[]

         {
            new { Lat = gridLat, Lng = gridLong, Name = callLabel },

        };
       
        //151N - 33.5

        // it's default setting but just in casevar gs = new GlobalSettings() {AllowLayersWithoutProjections = true};


        _shape = new Shapefile();

        // use empty string to create in-memory shapefile
        _shape.CreateNewWithShapeID("", ShpfileType.SHP_POINT);
        int fieldIndex = _shape.EditAddField("Name", FieldType.STRING_FIELD, 0, 20);
        
        
            foreach (var place in places)
        {

            // convert our degrees to meters in map projection
            double projX = 0.0, projY = 0.0;
            axMap1.DegreesToProj(place.Lng, place.Lat, ref projX, ref projY);

            // create shapes for each location
            shape = new Shape();
            shape.Create(ShpfileType.SHP_POINT);
            shape.AddPoint(projX, projY);

            // add it to shapefile along with name
            shapeIndex = _shape.EditAddShape(shape);
            _shape.EditCellValue(fieldIndex, shapeIndex, place.Name);
        }
        //sf.Labels.Generate("[Name]", tkLabelPositioning.lpCenter, true);
        //sf.Labels.AvoidCollisions = false;
        //sf.Labels.FontSize = 6;
        layerHandle = axMap1.AddLayer(_shape, true);
       //MessageBox.Show(shapeIndex.ToString());

    }

Hello @Daniac

Well, we don’t see all of your code, but in the simplest terms, you want to get the Shapefile operations out of the mapPins method, something like:

int _fieldIndex = -1;
int _layerHandle = -1;

public void higherLevelMethod()
{
    _shape = new Shapefile();

    // use empty string to create in-memory shapefile
    _shape.CreateNewWithShapeID("", ShpfileType.SHP_POINT);
    _fieldIndex = _shape.EditAddField("Name", FieldType.STRING_FIELD, 0, 20);
    
    _layerHandle = axMap1.AddLayer(_shape, true);

    // now do whatever you are doing to add the map pins
}

public void mapPins(string callLabel, double gridLat, double gridLong)
{ 
    var places = new[]
    {
        new { Lat = gridLat, Lng = gridLong, Name = callLabel },
    };
   
    //151N - 33.5

    // it's default setting but just in casevar gs = new GlobalSettings() {AllowLayersWithoutProjections = true};
    
    foreach (var place in places)
    {
        // convert our degrees to meters in map projection
        double projX = 0.0, projY = 0.0;
        axMap1.DegreesToProj(place.Lng, place.Lat, ref projX, ref projY);

        // create shapes for each location
        shape = new Shape();
        shape.Create(ShpfileType.SHP_POINT);
        shape.AddPoint(projX, projY);

        // add it to shapefile along with name
        shapeIndex = _shape.EditAddShape(shape);
        _shape.EditCellValue(_fieldIndex, shapeIndex, place.Name);
    }
    //sf.Labels.Generate("[Name]", tkLabelPositioning.lpCenter, true);
    //sf.Labels.AvoidCollisions = false;
    //sf.Labels.FontSize = 6;
   //MessageBox.Show(shapeIndex.ToString());
}

If you still need more direction, please provide more of your code so that we can better see what is going on.

Regards,
Jerry.

Hi Jerry, thanks
I took a slightly different approach where I did separate out the shapefile creation into a different method that just gets called once. Then I consolidated the mapPins method into the addPins method. So now the same method is reading the db and writing the pins…this occurs every 20 seconds but its still not correct because each 20 second cycle adds memory usage…I think I am adding new markers on top of existing markers on each cycle maybe ?
Should I be removing the existing marker before writing a new one?

    private void addPins()
    {


        string cs = @"URI=file:C:\JS8Map\callarchive.db3";
        var con = new SQLiteConnection(cs);
        con.Open();
        {
            SQLiteCommand cmd = new SQLiteCommand(con);
            cmd.CommandText = "SELECT call, gridlat, gridlong FROM Call_Data ";
            SQLiteDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {

              // mapPins(Convert.ToString(reader["call"]), Convert.ToDouble(reader["gridlat"]), Convert.ToDouble(reader["gridlong"]));


                //places =  [Lat = Convert.ToDouble(reader["gridlat"]), Lng = Convert.ToDouble(reader["gridlong"]), Name = Convert.ToString(reader["call"])];




                
                double lat = Convert.ToDouble(reader["gridlat"]);
                double lng = Convert.ToDouble(reader["gridlong"]);
                string call1 = Convert.ToString(reader["call"]);
               
                
                // convert our degrees to meters in map projection
                double projX = 0.0, projY = 0.0;
                axMap1.DegreesToProj(lng, lat, ref projX, ref projY);

                // create shapes for each location
                shape = new Shape();
                shape.Create(ShpfileType.SHP_POINT);
                shape.AddPoint(projX, projY);

                // add it to shapefile along with name

                shapeIndex = _shape.EditAddShape(shape);
                _shape.EditCellValue(_fieldIndex, shapeIndex, call1);
                
                //_shape.Labels.Generate("[Name]", tkLabelPositioning.lpCenter, true);
                //sf.Labels.AvoidCollisions = false;
                //sf.Labels.FontSize = 6;
                // MessageBox.Show(_shape.);
                //layerHandle = axMap1.AddLayer(_shape, true);



            }

Hello again.

I’d say that if each update is effectively replacing the previous set of pins, then it would probably make sense to clear the layer each iteration. Within your routine, do something like:

. . .
if (_shape.StartEditingShapes())
{
    // clear previous pins
    _shape.EditClear();
    // now read and add new pins
    while (reader.Read())
    {
        // do what you're currently doing to add the new shapes
        . . .
    }
    // save edits
    _shape.StopEditingShapes();
}

See if this makes a difference with the memory.

Regards,
Jerry.

Thanks Jerry, that solves a couple issues, the memory is behaving now and shapeidentity is also behaving now as I am not writing shapes on top of each other.

Thanks for your help!