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();
Related
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;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var cp = new Point(Width / 2, Height / 2);
DrawGradientCircle(e.Graphics, cp, 100);
}
private void DrawGradientCircle(Graphics gr, Point cp, float radius)
{
var path = new GraphicsPath();
path.AddEllipse(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius);
using (var brush = new PathGradientBrush(path))
{
var blends = new ColorBlend(7);
blends.Colors[0] = Color.Violet;
blends.Positions[0] = 0;
blends.Colors[1] = Color.Blue;
blends.Positions[1] = 0.16f;
blends.Colors[2] = Color.Aqua;
blends.Positions[2] = 0.32f;
blends.Colors[3] = Color.Lime;
blends.Positions[3] = 0.48f;
blends.Colors[4] = Color.Yellow;
blends.Positions[4] = 0.64f;
blends.Colors[5] = Color.Orange;
blends.Positions[5] = 0.82f;
blends.Colors[6] = Color.Red;
blends.Positions[6] = 1;
brush.InterpolationColors = blends;
gr.FillPath(brush, path);
}
}
There is my code - i just want to draw the circle after button click, but how to do it?
But I don't know how make a link
If I understood you right, you could have a boolean variable and set it to true when you click the button... something like:
private bool _buttonClicked = false;
void myButton_Click(object sender, EventArgs e)
{
_buttonClicked = true;
this.Invalidate(); // <-- invalidate the form so it's repainted
this.Update(); // <-- optional: force a synchronous repaint
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if(!_buttonClicked) return;
// this will only happen after button is clicked
var cp = new Point(Width / 2, Height / 2);
DrawGradientCircle(e.Graphics, cp, 100);
}
Don't forget to assign myButton_Click to the button's Click event
I am creating a program that can track multiple objects and get their centroid eventually I would use these centroids to connect to the nearest centroid of another object. But my problem is, my program tracks only one significant object on the video and it doesn't seem to display the centroid of this object. anyone can help me?
namespace Video_Processing_fixed_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap video;
Graphics g;
bool OnOff;
int mode;
int thoigiandemnguoc = 5;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame+=new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
g = Graphics.FromImage(video2);
g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
g.Dispose();
if (mode == 1)
{
// create filter
EuclideanColorFiltering filter = new EuclideanColorFiltering();
// set center colol and radius
filter.CenterColor = Color.FromArgb(215, 30, 30);
filter.Radius = 100;
// apply the filter
filter.ApplyInPlace(video2);
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinWidth = 5;
blobcounter.MinHeight = 5;
blobcounter.FilterBlobs = true;
blobcounter.ObjectsOrder = ObjectsOrder.Area;
blobcounter.ProcessImage(video2);
Blob[] blobs = blobcounter.GetObjectsInformation();
AForge.Point Center = new AForge.Point();
if (blobs.Length > 0)
{
Center.X = blobs.Average(c => c.CenterOfGravity.X);
Center.Y = blobs.Average(c => c.CenterOfGravity.Y);
}
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach(Rectangle recs in rects)
if (rects.Length > 0)
{
foreach (Rectangle objectRect in rects)
{
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
{
graphic.DrawRectangle(pen, objectRect);
}
graphic.Dispose();
}
}
( Pen pen = new Pen(Color.White,3))
pictureBox2.Image = video2;
pictureBox1.Image = video;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning==true)
{
FinalFrame.SignalToStop();
FinalFrame.WaitForStop();
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void ButObjTrack_Click(object sender, EventArgs e)
{
mode = 1;
}
private void StopButt_Click(object sender, EventArgs e)
{
FinalFrame.SignalToStop();
}
}
}
You do not display the average center value. Change these lines:
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach(Rectangle recs in rects)
if (rects.Length > 0)
{
foreach (Rectangle objectRect in rects)
{
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
{
graphic.DrawRectangle(pen, objectRect);
}
graphic.Dispose();
}
}
To these:
Rectangle[] rects = blobcounter.GetObjectsRectangles();
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Graphics graphic = Graphics.FromImage(video2);
foreach (Rectangle objectRect in rects)
{
graphic.DrawRectangle(Pens.LightGreen, objectRect);
}
graphic.DrawRectangle(Pens.Red, Center.X - 4, Center.Y - 4, 8, 8);
graphic.Dispose();
}
}
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);
I would like to know how to set my form position. i have tried to do the following:
this.Location = Point;
or:
Form2.Left = Point.X;
Form2.Top = Point.Y;
Form2.ShowDialog();
This is does not work. What do I do wrong?
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form();
form2.StartPosition = FormStartPosition.Manual;
form2.Left = 500;
form2.Top = 500;
form2.ShowDialog();
}
to set the position programmatically , you should set StartPosition to FormStartPosition.Manual as shown below :
Form myform = new Form()
{
Size = new Size(200,200),
StartPosition = FormStartPosition.Manual,
Location = new Point(10,10) // or Cursor.Position if you want to set it to cursor position
}
Try this:
private void Form_Load(object sender, EventArgs e)
{
this.SetDesktopLocation(x, y);
}
The first approach of you works anyways.
this.Location = new Point(/*XPosition*/, /*YPosition*/);
You can set it like this:
form1.Location = new Point(4, 370);
// Point(specify location of x, specify location of y)
// with object initializer
var frmUsers = new FrmUsers
{
StartPosition = FormStartPosition.Manual,
Location = new Point(0, 0)
};
// or
var frmUsers = new FrmUsers();
frmUsers.StartPosition = FormStartPosition.Manual;
frmUsers.Location = new Point(0, 0);