Gmap.net marker removal

I use GMap.NET code for maps and markers. Adding markers and doing something with the map was not a problem when deleting one marker from the map I am having problems with. I researched everywhere on Google to remove a marker from a map, but deleting a marker doesn't seem like a hot topic.

Here is a piece of code that I use to add.

private void AddMarker_ButtonClick(object sender, AddMarkerEventArgs e) { DBDictAdd("Marker", " ",e.Latitude, e.Longitude, true, "192.168.1.1"); m_dbMarkers.Insert(_table, dbmarkertable); dbmarkertable.Clear(); GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(e.Latitude, e.Longitude), GMarkerGoogleType.green); marker.ToolTip = new GMapRoundedToolTip(marker); marker.ToolTipMode = MarkerTooltipMode.OnMouseOver; marker.Tag = MarkerIndex; marker.ToolTipText = (marker.Position.ToString()); markersOverlay.Markers.Add(marker); MarkerIndex++; } 

As you can see, I use SQLite to store ID and other information in the database for tokens. I use the database approach because I will need to store additional information with the location of the marker. This piece of code works.

The problem I'm having is causing the marker information, so I can delete it. I know that these 2 challenges exist.

 markersOverlay.Markers.Remove(); markersOverlay.Markers.RemoveAt(); 

I used the removeAt command, but I had to do it manually and noticed that with the removeAt(0) command, it takes it from the first record of this array every time.

This is what I have tried so far:

 private void uxRemoveMarkerButton_Click(object sender, EventArgs e) { MessageBox.Show("Select the marker to remove."); uxGmap.OnMarkerClick += new MarkerClick(uxGmap_OnMarkerClick); } private void uxGmap_OnMarkerClick(object sender, EventArgs e) { //int? mID = null; //DataTable da = m_dbMarkers.GetDataTable("SELECT * from markers"); //GMapMarker marker = null; //PointLatLng p = new PointLatLng(lat, lng); //foreach (GMapMarker m in markersOverlay.Markers) //{ // if (m.Position == p) // marker = m; //} //markersOverlay.Markers.Remove(marker); //MessageBox.Show("done"); //foreach (DataRow dr in da.Rows) //{ // if (Convert.ToDouble(dr[3]) == lat) // { // MessageBox.Show(dr[3].ToString()); // //mID = Convert.ToInt32(dr[0]); // } //} //markersOverlay.Markers.IndexOf(marker); //mID= Convert.ToInt32(m_dbMarkers.ExecuteScalar("SELECT ID FROM markers")); //m_dbMarkers.Delete(_table, String.Format("markers.ID = {0} ", mID)); //markersOverlay.Markers.RemoveAt(mID); //MessageBox.Show(mID.ToString()); uxGmap.OnMarkerClick -= new MarkerClick(uxGmap_OnMarkerClick); } 

Any suggestions or recommendations on how I can get the identifier of the marker or the marker itself when I click on it to remove it from the map?

EDIT: I updated AddMarker_ButtonClick

+6
source share
1 answer

The solution I discovered to remove a marker from the map:

GMap.Net has a MarkerClick event that you can subscribe to. In my form, this is the code I used and where I put it in the form code.

 Public Form1() { uxGmap.OnMarkerClick += new MarkerClick(uxGmap_OnMarkerClick); } 

Code inside uxGmap_OnMarkerClick

 private void uxGmap_OnMarkerClick(GMapMarker item, MouseEventArgs e) { currentMarker = item; uxRemoveMarkerButton.Enabled = true; } 

I have a global GMapMarker called currentMarker that stores the item marker that you click on. I also allow the button to remove the marker in my form.

Then I use the button to remove the marker from the map using:

 private void uxRemoveMarkerButton_Click(object sender, EventArgs e) { int? mID = null; if(currentMarker != null) { mID = Convert.ToInt32(currentMarker.Tag); markersOverlay.Markers.Remove(currentMarker); currentMarker = null; } m_dbMarkers.Delete(_table, String.Format("markers.ID = {0} ", mID)); uxRemoveMarkerButton.Enabled = false; } 

GMapMarker has the Tag property, where I can store the marker identifier of the marker. Then I extract this identifier from my global currentMarker.Tag and save it in a local int mID . I use this mID to remove the row with the identifier of the token that I saved in the sqlite database that I have. I also turned off the button to prevent user error until the marker is pressed.

+2
source

Source: https://habr.com/ru/post/988025/


All Articles