ArcEngine based on C# realizes the personalized introduction window of clicking on map elements

Posted by pedro_silva on Thu, 17 Feb 2022 21:19:37 +0100

1, Introduction

The function of this blog post is to select an element on the map and pop up its corresponding information window. For example, my internship theme is the historical buildings in Wuhan University. I can choose Song Qing gymnasium, like this.

Then click "Introduction to architecture", and the software will pop up a window like the following.

2, Implementation introduction

2.1 selection of elements

        private void Element selection ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.axMapControl1.CurrentTool = null;
            //Definition and initialization of Tool
            ControlsSelectFeaturesToolClass pTool = new ESRI.ArcGIS.Controls.ControlsSelectFeaturesToolClass();
            //Tool is associated with MapControl through ICommand
            pTool.OnCreate(this.axMapControl1.Object);
            //The current tool of MapControl is set to tool 
            this.axMapControl1.CurrentTool = pTool as ITool; 
        }

However, after implementing the above method, you will be surprised to find that you just tick an element on the map, that is, highlight a good-looking one! You can highlight the real estate developers and tell them where you want to invest here. I don't know. In short, you must invest.

2.2 acquisition of selected elements

You now have a piece of land, but you don't know its name and have no control. You have nothing but money and determination to win it. OK, let me introduce how to obtain its information.

//Obtain the feature object of the selected feature (note that you can select multiple features. All the selected features will be stored in pEnumFeature, and you can continue to Next() until there is no next bit)

  IMap map = axMapControl1.Map;
            ISelection selection = map.FeatureSelection;
            IEnumFeatureSetup iEnumFeatureSetup = (IEnumFeatureSetup)selection;
            iEnumFeatureSetup.AllFields = true;
            IEnumFeature pEnumFeature = (IEnumFeature)iEnumFeatureSetup;
            pEnumFeature.Reset();
            IFeature pFeature = pEnumFeature.Next();
            
            //If the first element you get is strange, you may need the following one
            //pFeature = pEnumFeature.Next(); 

Now that you have obtained the pFeature object, you have obtained the real estate certificate and can view the properties of the feature.

2.3 binding of elements and corresponding introduction information

The key to dynamically loading the introduction window is to need an ID (identifier) to bind your pre stored introduction information (including pictures) with the selected map elements. I use the name of the building.

//Get the value of the field you need. 2 is the index number. Please modify it according to your own data
String name = pFeature.get_Value(2).ToString();

Then you need to instantiate an introduction window with feature and name.

2.4 introduction to window classes

Create a new Windows form class FormIntro;
Draw the Form template according to the display requirements.
My is this:

Add two member variables and a dictionary variable (take name as the key and the introduced text as the value to realize text retrieval)

//Form class variables
public IFeature pfeature;
public string bname;
Dictionary<string, string> dict = new Dictionary<string, string>();

The following is the method to retrieve relevant information and display it on the window:

        public void showDetails(string bname)
        {
            string intro = getInfo(bname);
            string pic = @"Own path"+bname+".jpg";
            //Set text information
            label1.Text= bname;
            label2.Text = arrlist[1].ToString();
            //Set the size of the picture to fit the pictureBox
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            //Set picture
            pictureBox1.Image = Image.FromFile(pic);

        }
        
        //This function is very simple and can not be written. However, when you need to call a lot of fields, you need to consider the writing method that looks smarter^^
	    public string getInfo(String bname)
        {
            string intro=dict[bname];
            return intro;
        }

Finally, just call the showDetails method in the response code after clicking the query button to set the window information and let the window Show();

		    FormIntro intro = new FormIntro();
            while(pFeature != null)
            {                             
                string name = null;
                try
                {
                    name = pFeature.get_Value(2).ToString();
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("didn't get buildingname");   
                }
                finally
                {
                }
                pFeature = pEnumFeature.Next(); 
                intro.showDetails(name);
                intro.Show();                              
            }
        }

So far, you can show your campus online to netizens. (FOG)

3, References and slogan

The implementation of this function has also received a lot of help from the Internet. You may need some extended knowledge:

[1] RTFM (*F=friendly)
[2] Imitate the Identify function of ArcEngine to display the attribute information of elements
[3] AE feature selection (click selection and pull box selection)
[4] How much do you know about Luojia historical sites: historical buildings inside and outside the "national security" list of Wuhan University

Topics: C#