I have a text file with a list of GPS coordinates. I am trying to place a marker on each of the coordinates from the document. The problem is that the lengths of the documents change and the way I have it, the marker gets replaced with every iteration. How do I add a marker for each lat/lon point?
Here's the relevant code:
private GMapOverlay gMapOverlay;
private GMapMarker marker;
gmap.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
gmap.MinZoom = 2;
gmap.MaxZoom = 25;
gmap.Zoom = 5;
gmap.ShowCenter = false;
gmap.DragButton = MouseButtons.Left;
//setup the map overlay for displaying routes/points
gMapOverlay = new GMapOverlay("Path");
gmap.Overlays.Add(gMapOverlay);
gMapOverlay.Markers.Clear();
gMapOverlay.Routes.Clear();
//GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(0, 0), GMarkerGoogleType.green);
marker = new GMarkerGoogle(new PointLatLng(0, 0), GMarkerGoogleType.green);
marker.IsVisible = false;
marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
marker.ToolTipText = "Starting Point";
gMapOverlay.Markers.Add(marker);
private void btn_KMLFile_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog4.ShowDialog();
if (result == DialogResult.OK)
{
string filename = openFileDialog4.FileName;
string[] lines = System.IO.File.ReadAllLines(filename);
foreach (string line in lines)
{
string[] Data_Array = line.Split(',');
Double londecimal = Convert.ToDouble(Data_Array[0]);
Double latdecimal = Convert.ToDouble(Data_Array[1]);
marker.Position = new PointLatLng(latdecimal, londecimal);
marker.IsVisible = true;
gmap.Update();
}
}
}
private void openFileDialog4_FileOk(object sender, CancelEventArgs e)
{
OpenFileDialog openFileDialog4 = new OpenFileDialog();
}
The markers can go into the Markers collection:
public readonly ObservableCollection<GMapMarker> Markers;
Just add the markers to the collection as you do with your single marker.
EDIT
I was assuming a WPF client, so there's no Observable Collection if you are using WinForms. Have you tried to add a new marker to the collection as you do with your original marker? So in your loop:
string[] Data_Array = line.Split(',');
Double londecimal = Convert.ToDouble(Data_Array[0]);
Double latdecimal = Convert.ToDouble(Data_Array[1]);
// add a new one here
var marker = new GMarkerGoogle(new PointLatLng(latdecimal, londecimal), GMarkerGoogleType.green);
marker.IsVisible = true;
gMapOverlay.Markers.Add(marker);
I can fix with using list
List<GMapMarker> lMarks = new List<GMapMarker>();
int[] SelectedRows = gvProjects.GetSelectedRows();
map.Overlays.Clear();
GMapOverlay markers = new GMapOverlay("markers");
for (int i = 0; i < SelectedRows.Length; i++)
{
if (SelectedRows[i] >= 0)
{
double lat = Convert.ToDouble(gvProjects.GetRowCellValue(i-2,"Altitude"));
double lng = Convert.ToDouble(gvProjects.GetRowCellValue(i-2, "Longitude"));
PointLatLng point = new PointLatLng(lat, lng);
GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat,lng),GMarkerGoogleType.yellow_pushpin);
marker.ToolTipMode = MarkerTooltipMode.Always;
marker.Tag = gvProjects.GetRowCellValue(i-2, "ID").ToString();
marker.ToolTipText = gvProjects.GetRowCellValue(i-2, "ProjectName").ToString();
lMarks.Add(marker);
}
}
markers.Markers.AddRange(lMarks);
map.Overlays.Add(markers);
Related
I have a list of signals in a listview. When the user checks one, the values of the signals are being plotted on the chart. Moreover there is a vertical annotation which the user can drag across the graph and see the values for every x value. Each signal has one rectangle annotation that shows the Y value.
My problem is that when the user checks a new signal then the old rectangle annotations do not disappear.
Here is what I mean :
enter image description here
Here is my code so far :
List<RectangleAnnotation> anno = new List<RectangleAnnotation>();
List<Series> checkedItems = new List<Series>();
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (listView1.FocusedItem != null)
{
double xFactor = 0.03;
double yFactor = 0.02;
CA = chart1.ChartAreas[0];
if (e.NewValue == CheckState.Checked)
{
anno.Clear();
Series s12 = new Series();
s12 = chart1.Series.Add((listView1.Items[e.Index].Text).ToString());
s12.ChartType = SeriesChartType.Line;
s12.MarkerStyle = MarkerStyle.Circle; // make the points stand out
s12.MarkerSize = 3;
checkedItems.Add(s12);
for (int i = 0; i < chart1.Series.Count - 1; i++)
{
anno.Add(new RectangleAnnotation());
anno[i].AxisX = CA.AxisX;
anno[i].IsSizeAlwaysRelative = false;
anno[i].Width = 20 * xFactor;
anno[i].Height = 8 * yFactor;
// VA.Name = "myRect";
anno[i].LineColor = Color.Black;
anno[i].BackColor = Color.Black;
anno[i].AxisY = CA.AxisY;
anno[i].Y = -anno[i].Height;
// RA[i].X = VA.X - RA[i].Width / 2;
anno[i].Text = "Hello";
anno[i].ForeColor = Color.Black;
anno[i].Font = new System.Drawing.Font("Arial", 8f);
anno[i].Text = String.Format("{0:0.00}", 0);
chart1.Annotations.Add(anno[i]);
}
for (int r = 0; r < num_rows; r++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
// chart1.Series[checkedListBox1.Items[e.Index].ToString()].Points.Add(dp);
s12.Points.AddXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
}
}
}
}
private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
{
int pt1 = (int)e.NewLocationX;
int i = 0;
foreach (var signal in checkedItems) {
double val = signal.Points[pt1].YValues[0];
foreach (var sim in signal.Points[pt1].YValues)
{
anno[i].Y = sim + 0.5;
}
if (sender == VA) anno[i].X = VA.X - anno[i].Width / 2;
anno[i].Text = String.Format("{0:0.00}", val);
i++;
Console.WriteLine(anno.Count);
}
I have thought of adding
chart1.Annotations.clear();
But it deletes all Annotations including the vertical. I only want to delete the rectangle annotations.
I want show multi marker on Map , but seem can't , code follow :
button1_Click show Position ,button2_Click show markers !
anyone give me any instructions? thanks.
private void button1_Click(object sender, EventArgs e)
{
gmap.DragButton = MouseButtons.Left;
gmap.MapProvider = GMapProviders.GoogleMap;
gmap.Position = new PointLatLng(25.037531, 121.5639969);
gmap.MinZoom = 5;
gmap.MaxZoom = 100;
gmap.ShowCenter = false;
gmap.Zoom = 15;
}
private void button2_Click(object sender, EventArgs e)
{
Random r = new Random();
var marker = new GMarkerGoogle(new PointLatLng(r.Next(25, 500), 121), GMarkerGoogleType.green);
var marker1 = new GMarkerGoogle(new PointLatLng(r.Next(25, 500), 121), GMarkerGoogleType.pink);
var marker2 = new GMarkerGoogle(new PointLatLng(r.Next(25, 500), 121), GMarkerGoogleType.blue);
var marker3 = new GMarkerGoogle(new PointLatLng(r.Next(25, 500), 121), GMarkerGoogleType.yellow);
marker.IsVisible = true; marker1.IsVisible = true; marker2.IsVisible = true; marker3.IsVisible = true;
gMapOverlay.Markers.Add(marker);
gMapOverlay.Markers.Add(marker1);
gMapOverlay.Markers.Add(marker2);
gMapOverlay.Markers.Add(marker3);
gmap.Overlays.Add(gMapOverlay);
}
Your code is ok, but the problem with the random function that you use only generates integers, and a small change as 1 integer means changing a complete degree in either latitude or longitude and this is a hudge displacement.
So you have to generate a random Doubles number instead of Integers.
I edit the code to work correctly.
using GMap.NET;
using GMap.NET.WindowsForms.Markers;
public class Form1
{
private GMap.NET.WindowsForms.GMapOverlay gMapOverlay;
private Random rand = new Random();
private void Form1_Load(object sender, EventArgs e)
{
GMapControl1.DragButton = MouseButtons.Left;
GMapControl1.MapProvider = GMap.NET.MapProviders.GMapProviders.GoogleMap;
GMapControl1.Position = new PointLatLng(25.037531, 121.5639969);
GMapControl1.MinZoom = 5;
GMapControl1.MaxZoom = 20;
GMapControl1.ShowCenter = false;
GMapControl1.Zoom = 11;
gMapOverlay = new WindowsForms.GMapOverlay("markers");
gMapOverlay.IsVisibile = true;
}
private void button2_Click(object sender, EventArgs e)
{
var marker = new GMarkerGoogle(new PointLatLng(GetRandomDouble(24.8, 25.1), GetRandomDouble(121.3, 121.6)), GMarkerGoogleType.green);
var marker1 = new GMarkerGoogle(new PointLatLng(GetRandomDouble(24.8, 25.1), GetRandomDouble(121.3, 121.6)), GMarkerGoogleType.pink);
var marker2 = new GMarkerGoogle(new PointLatLng(GetRandomDouble(24.8, 25.1), GetRandomDouble(121.3, 121.6)), GMarkerGoogleType.blue);
var marker3 = new GMarkerGoogle(new PointLatLng(GetRandomDouble(24.8, 25.1), GetRandomDouble(121.3, 121.6)), GMarkerGoogleType.yellow);
marker.IsVisible = true;
marker1.IsVisible = true;
marker2.IsVisible = true;
marker3.IsVisible = true;
// Clear old markers
gMapOverlay.Markers.Clear();
gMapOverlay.Markers.Add(marker);
gMapOverlay.Markers.Add(marker1);
gMapOverlay.Markers.Add(marker2);
gMapOverlay.Markers.Add(marker3);
// Clear old overlay
GMapControl1.Overlays.Clear();
GMapControl1.Overlays.Add(gMapOverlay);
// Zoom the map to show all drawn markers
GMapControl1.ZoomAndCenterMarkers(gMapOverlay.Id);
}
public double GetRandomDouble(double min, double max)
{
return rand.NextDouble() * (max - min) + min;
}
I plotted a graph (mschart)and now my question is Clicking Discard button should delete the selected areas and refresh the graph with the remaining.In here user can select multiple chart area.I added multiple selection code also.but I do not know that is correct or not.my interface is like following.
my plotted graph code as follows;
private void Output_Load(object sender, EventArgs e)
{
List<Graph> ObservingData = new List<Graph>(); // List to store all available Graph objects from the CSV
// Loops through each lines in the CSV
foreach (string line in System.IO.File.ReadAllLines(pathToCsv).Skip(1)) // .Skip(1) is for skipping header
{
// here line stands for each line in the csv file
string[] InCsvLine = line.Split(',');
// creating an object of type Graph based on the each csv line
Graph Inst1 = new Graph();
Inst1.Date = DateTime.ParseExact(InCsvLine[0], dateFormatString, CultureInfo.InvariantCulture);
Inst1.AvE = double.Parse(InCsvLine[15]);
Inst1.AvI = double.Parse(InCsvLine[16]);
chart1.Series["Current"].YAxisType = AxisType.Primary;
chart1.Series["Current"].Points.AddXY(Inst1.AvI, Inst1.AvE);
chart1.Series["Current"].ChartType = SeriesChartType.FastLine;
ChartArea CA = chart1.ChartAreas[0];
CA.AxisX.ScaleView.Zoomable = false;
CA.AxisY.ScaleView.Zoomable = false;
CA.CursorX.AutoScroll = true;
CA.CursorX.IsUserSelectionEnabled = true;
}
}
Following code for my multiple selection for chart area.
SizeF curRange = SizeF.Empty;
List<SizeF> ranges = new List<SizeF>();
List<int> selectedIndices = new List<int>();
private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
ranges.Add(curRange);
selectedIndices.Union(collectDataPoints(chart1.Series[0],curRange.Width, curRange.Height)).Distinct();
StripLine sl = new StripLine();
sl.BackColor = Color.FromArgb(255, Color.LightSeaGreen);
sl.IntervalOffset = Math.Min(curRange.Width, curRange.Height);
sl.StripWidth = Math.Abs(curRange.Height - curRange.Width);
chart1.ChartAreas[0].AxisX.StripLines.Add(sl);
}
List<int> collectDataPoints(Series s, double min, double max)
{
List<int> hits = new List<int>();
for (int i = 0; i < s.Points.Count; i++)
if (s.Points[i].XValue >= min && s.Points[i].XValue <= max) hits.Add(i);
return hits;
}
private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
curRange = new SizeF((float)e.NewSelectionStart, (float)e.NewSelectionEnd);
}
Please give your kind help to resolve my question.
When my cursor is over a marker, it doesn't zoom. Is there any way around this?
Cheers
public MainForm(Dictionary<string, string> startupParams)
{
InitializeComponent();
gMapControl.MapProvider = GMapProviders.GoogleMap;
gMapControl.Position = new PointLatLng(-33.861468, 151.209179);
gMapControl.MinZoom = 0;
gMapControl.MaxZoom = 24;
gMapControl.Zoom = 9;
gMapControl.MarkersEnabled = true;
gMapControl.DragButton = MouseButtons.Left;
gMapControl.ShowCenter = false;
}
private void UpdateMap()
{
gMapControl.Position = new PointLatLng(Convert.ToDouble(LatLng.Split(',')[0]), Convert.ToDouble(LatLng.Split(',')[1]));
GMapOverlay markersOverlay = new GMapOverlay("markers");
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(LatLng.Split(',')[0]), Convert.ToDouble(LatLng.Split(',')[1])), GMarkerGoogleType.green);
markersOverlay.Markers.Add(marker);
gMapControl.Overlays.Clear();
gMapControl.Overlays.Add(markersOverlay);
}
Yes, there's a simple way around this:
gMapControl.IgnoreMarkerOnMouseWheel = true;
I have added a marker using GMap with the lat/long specified. When the application starts, the marker is placed in the incorrect position(at the center of the GMap control) and then when I zoom, it then goes to the specified coordinates. Is this a bug in GMap or am I doing something wrong? Here is the code.
GMapOverlay markersOverlay, mo2;
GMarkerGoogle marker, marker5;
GMapOverlay polyOverlay;
List<PointLatLng> points;
GMapRoute gr;
Graphics g;
bool start = true;
double move = .0001;
double lt = 73, lg = -180;
public Form1()
{
AllocConsole();
InitializeComponent();
try
{
System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com");
}
catch
{
gmap.Manager.Mode = AccessMode.CacheOnly;
MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
gmap.MapProvider = GMapProviders.BingHybridMap;
gmap.Position = new PointLatLng(32, -100);
gmap.MinZoom = 3;
gmap.MaxZoom = 15;
gmap.Zoom = 9;
markersOverlay = new GMapOverlay("markers");
mo2 = new GMapOverlay("markers5");
marker5 = new GMarkerGoogle(new PointLatLng(lt, lg), GMarkerGoogleType.orange_small);
g = this.CreateGraphics();
}
private void Form1_Load(object sender, EventArgs e)
{
gmap.DragButton = MouseButtons.Left;
gmap.ShowCenter = false;
points = new List<PointLatLng>();
polyOverlay = new GMapOverlay("polygons");
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Magenta));
polygon.Stroke = new Pen(Color.Magenta, 2);
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
MouseLatLong.Text = Convert.ToString(p);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
marker = new GMarkerGoogle(new PointLatLng(double.Parse(LattextBox.Text), double.Parse(LongtextBox.Text)), new Bitmap(#"C:\Users\Vaib\Documents\Visual Studio 2013\Projects\testGmap\testGmap\Resources\wpt.png"));
mo2.Markers.Add(marker);
gmap.Overlays.Add(mo2);
marker.ToolTip = new GMapToolTip(marker);
marker.ToolTipText = NametextBox.Text;
marker.ToolTipMode = MarkerTooltipMode.Always;
if (start)
{
gmap.Position = new PointLatLng(marker.Position.Lat, marker.Position.Lng);
start = false;
}
points.Add(new PointLatLng(marker.Position.Lat, marker.Position.Lng));
gr = new GMapRoute(points, "route");
gr.Stroke = new Pen(Color.Magenta, 2);
polyOverlay.Routes.Add(gr);
gmap.Overlays.Add(polyOverlay);
ga = new GMarkerArrow(new PointLatLng(gr.From.Value.Lat, gr.From.Value.Lng));
if (points.Count >= 2)
{
ga.Bearing = (float)final(gr.From.Value.Lat, gr.From.Value.Lng, points[1].Lat, points[1].Lng);
}
markersOverlay.Clear();
markersOverlay.Markers.Add(ga);
gmap.Overlays.Add(markersOverlay);
}
The trick is to first add overlay and then the marker:
gMapControl.Overlays.Add(markersOverlay);
markersOverlay.Markers.Add(marker);
Solution
Like you can read in the comments: Adding
gmap.Overlays.Clear()
at the very beginning of the method
private void SubmitButton_Click(object sender, EventArgs e)
was the answer to his problem.
I'm working in MSVC2010 (C++) on a WinForms app and had the same problem - took most of the afternoon to resolve.
This thread was useful, but I find all you need to do is (sorry it's not C#) is comment out the first time you add the marker - see
// DO NOT ADD... line
// Make marker
WindowsForms::Markers::GMarkerGoogle ^MyMarker;
WindowsForms::Markers::GMarkerGoogleType MyType = safe_cast<WindowsForms::Markers::GMarkerGoogleType>(3); // Blue marker 3
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
// MyOverlay->Markers->Add(MyMarker); // DO NOT ADD THE MARKER!!!
gMapControl1->Overlays->Add(MyOverlay);
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
MyOverlay->Markers->Add(MyMarker);
gMapControl1->Overlays->Add(MyOverlay);
gMapControl1->ReloadMap();