1

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:

enter image description here

but when I try to add the same coordinates on my Gmap.Net Application the marker goes to the wrong place, like this:

enter image description here

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);

        }

    }
}
Community
  • 1
  • 1
Gabriel
  • 763
  • 1
  • 10
  • 28
  • 1
    Can you try moving `//Add the overlay on the gMapControl1(Map) gMapControl1.Overlays.Add(markersOverlay);`before you add markers in the for loop? this is how is it suggested in the other post – kaho Jul 31 '15 at 21:25
  • @kaho it doesn't work, I cannot even create a marker doing this. – Gabriel Jul 31 '15 at 22:08
  • What exactly is the result/error when you try to add the overlay first, and then the markers, as @kaho suggested? – rdoubleui Aug 19 '15 at 13:09
  • @rdoubleui I already updated my project and I don't remember the error that I found but since the problem is already fixed as I had explained with my answer to this same topic, I think that maybe to insist with this problem it is not the right choice to make. Thus, if you wanna try to fix my code or even understand better why your solution cannot be applied into this case I will be glad to listen your explanation. Thank you again for your interesting in my case. – Gabriel Aug 19 '15 at 13:51
  • Added simple demo, adapting the code you've posted. Just for completeness. – rdoubleui Aug 19 '15 at 19:47

2 Answers2

1

I discover a simple way to "fix the problem" by simulatin the zoom in and zoom out really fast and automaticaly.

Doing this, every new marker that I add will be already into the right position without the necessity of too much modification into the code.

1. Create the simulatin of the zoom in and zoom out.

First you will need to create the simulation of zoom in and zoom out that we usually do with th mouse scroll, this code should work just fine for this situation:

         double zoomAtual = gMapControl1.Zoom;
         gMapControl1.Zoom = zoomAtual + 1;
         gMapControl1.Zoom = zoomAtual;

2. Put the code at the end of the marker's function.

After that you will need to put this part of code at the end of the function that creates a new marker, for example, I will write it inside the loop of markers function:

        //Update the Map
        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);

            double zoomAtual = gMapControl1.Zoom;
            gMapControl1.Zoom = zoomAtual + 1;
            gMapControl1.Zoom = zoomAtual;

        }

Now the map will work just fine.

Gabriel
  • 763
  • 1
  • 10
  • 28
0

I took your sample and changed the suggested part:

    private void atualizarMapa()
    {
        //Clean the map
        gMapControl1.Overlays.Clear();

        //Create a new overlay 
        GMapOverlay markersOverlay = new GMapOverlay("markers");

        //Add the overlay on the gMapControl1(Map)
        // This makes all the difference: The marker is set in the correct position, if the overlay is added first.
        gMapControl1.Overlays.Add(markersOverlay);

        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);
        }
        //gMapControl1.UpdateMarkerLocalPosition(marker);

    }

The result is: Simply moving the statement

gMapControl1.Overlays.Add(markersOverlay);

in front of the foreach loop positions the marker correctly.

Your clearing the overlay in every call of atualizarMapa(). Are you still handling it this way? Don't think that is necessary.

rdoubleui
  • 3,554
  • 4
  • 30
  • 51
  • 1
    I had already tried exactly this. But for some reason that I don't know, the VS 2013 gave me an error into the compilation time. Anyway, thank you for your attention in this case. I will try, in my future codes, make the right thing instead of fix the code by simulation of zoom in and zoom out. – Gabriel Aug 19 '15 at 19:52