I've been working on a Windows Forms Application with a Gmap.Net controller, but every time I try to add a new marker, it goes to the wrong position.
I saw a similar question here saying that when the map is zoomed the marker goes to right place, but I wanna fix it since it does not work if I simply add the map's overlays before add the marker as the answer of that question proposed, thats why it is not a duplicate question 'cause I wanna a different solution.
How can I fix the problem of the wrong position of the markers from the global list I created?
For example: I got the coordinates (-22.913715, -43.164096) from google maps of this specific place:
but when I try to add the same coordinates on my Gmap.Net Application the marker goes to the wrong place, like this:
So I really don't know if the global list of markers that I had created is working fine.
Here is my code:
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BarcoSolar_UFF
{
public partial class Form1 : Form
{
//Create a global list
List<PointLatLng> Pontos = new List<PointLatLng>();
public Form1()
{
InitializeComponent();
gMapControl1.MapProvider = GMapProviders.GoogleMap;
gMapControl1.MinZoom = 0;
gMapControl1.MaxZoom = 24;
gMapControl1.Zoom = 10;
gMapControl1.SetPositionByKeywords("niteroi");
gMapControl1.ShowCenter = false;
gMapControl1.DragButton = MouseButtons.Left;
}
//Function to remove all the Markers
public void button1_Click(object sender, EventArgs e)
{
//Clean the map
gMapControl1.Overlays.Clear();
//Clean the list of markers
Pontos.Clear();
//Updates the map
atualizarMapa();
}
private void gMapControl1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void atualizarMapa()
{
//Clean the map
gMapControl1.Overlays.Clear();
//Create a new overlay
GMapOverlay markersOverlay = new GMapOverlay("markers");
foreach (PointLatLng p in Pontos)
{
//Create a red marker
GMarkerGoogle marker1 = new GMarkerGoogle(p, GMarkerGoogleType.red);
//Add a marker on the overlay
markersOverlay.Markers.Add(marker1);
}
//Add the overlay on the gMapControl1(Map)
gMapControl1.Overlays.Add(markersOverlay);
//gMapControl1.UpdateMarkerLocalPosition(marker);
}
//Function to Add Markers
private void button1_Click_1(object sender, EventArgs e)
{
//Leitura de variavel formato .txt
string Lat_s = textBox1.Text;
string Lng_s = textBox2.Text;
//Convert to Double
double Lat = double.Parse(Lat_s);
double Lng = double.Parse(Lng_s);
//Add Markers to a Global List
PointLatLng ponto = new PointLatLng(Lat, Lng);
Pontos.Add(ponto);
atualizarMapa();
}
//Add the Route on the Map
private void button1_Click_2(object sender, EventArgs e)
{
GMapOverlay polyOverlay = new GMapOverlay("polygons");
GMapPolygon polygon = new GMapPolygon(Pontos, "rota");
polygon.Fill = new SolidBrush(Color.Transparent);
float[] dashValues = { 5, 5 };
polygon.Stroke = new Pen(Color.Red, 1);
polygon.Stroke.DashPattern = dashValues;
polyOverlay.Polygons.Add(polygon);
gMapControl1.Overlays.Add(polyOverlay);
}
}
}