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