I am using the GMap.NET code for maps and markers. Adding markers and doing anything with the map hasn't been the problem its removing a single marker from the map I am having issues with. I researched everywhere on Google to remove a marker from the map but removing a marker doesn't seem to be a hot topic.
Here is the snippet from the code 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 am using SQLite
to store the ID
and other information into the database for markers. I am using the database approach because I will need to store more information with the marker location. This part of the code works.
The problem I am having is calling the marker information so I can remove it. I know these 2 calls exist.
markersOverlay.Markers.Remove();
markersOverlay.Markers.RemoveAt();
I have used the removeAt
command but I had to do it manually and noticed using the removeAt(0)
command takes it from the first entry of that 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 guidance on how I can get the marker ID or marker itself when I click on it to actually remove it from the map?
EDIT:
I updated the AddMarker_ButtonClick
The solution I found out to removing the 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 placed it in the form code.
Public Form1()
{
uxGmap.OnMarkerClick += new MarkerClick(uxGmap_OnMarkerClick);
}
The code inside the 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 enable the button to remove the marker on my form.
Then I use a button click 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;
}
The GMapMarker
has a Tag
property to where I can store the marker ID to the marker. I then extract that ID from my global currentMarker.Tag
and store it to a local int mID
. I use this mID
to delete the row with the marker ID I stored in the sqlite database I have. I also disable the button to not allow any user error until a marker is clicked.