Move marker

Topics: Windows Forms
Jul 4, 2011 at 9:04 AM

hello,

thank you again and again for your great control :)

in MainMap_MouseMove event i add function to move marker with holding mouse left click and move.

its hard to move the marker, and the cursor lose the marker some times when speed up the movement of mouse.

how to solve this issue?

thank you in advance ;)

Coordinator
Jul 4, 2011 at 9:17 AM

// move current marker with left holding
void MainMap_MouseMove(object sender, MouseEventArgs e)
{
  if(e.Button == MouseButtons.Left && isMouseDown)
  {
    if(selectedMarker.IsVisible)
    {
       selectedMarker.Position = MainMap.FromLocalToLatLng(e.X, e.Y);
    }
  }
}

all you need ensure is selectedMarker instance

Jul 4, 2011 at 11:21 AM

i face the problem when move CurentRectMarker

the code:

               // move rect marker

               PointLatLng pnew = MainMap.FromLocalToLatLng(e.X, e.Y);

                 int? pIndex = (int?)CurentMarker.Tag;

                   if (pIndex.HasValue)

                   {

                       if (pIndex < polygon.Points.Count)

                       {

                           polygon.Points[pIndex.Value] = pnew;

                           MainMap.UpdatePolygonLocalPosition(polygon);

                       }

                   }

 

                   if (currentMarker.IsVisible)

                   {

                       currentMarker.Position = pnew;

                   }

                   CurentMarker.Position = pnew;

 

                   if (CurentMarker.InnerMarker != null)

                   {

                      CurentMarker.InnerMarker.Position = pnew;

                   }   

   

 

Jul 4, 2011 at 11:25 AM

CurentMarker equivalent to CurentRectMarker

:))

Coordinator
Jul 4, 2011 at 12:27 PM

that's not essential part of code, it's just for demo, i recommend to make your own custom logic

Jul 4, 2011 at 12:46 PM

i just try this code, the problem still appear, the mouse lose the rect Marker when speed up the movement of mouse       

void MainMap_MouseMove(object sender, MouseEventArgs e)
{

    if (e.Button == MouseButtons.Left && isMouseDown)

           {  

               if (CurentMarker == null)

              {

                   if (currentMarker.IsVisible)

                   {

                       currentMarker.Position = MainMap.FromLocalToLatLng(e.X, e.Y);

                   }

               }

               else // move rect marker

                  {

                     CurentRectMarker.Position = MainMap.FromLocalToLatLng(e.X, e.Y);

 

                 } 

               }

Coordinator
Jul 4, 2011 at 12:51 PM

i know, so don't speed up ;}

Jul 5, 2011 at 5:51 AM

:)

May 20, 2013 at 4:35 PM
Hello Radioman

There is something strange with the code you propose

1- isMouseDown is always false, so I remove this test
2- I use OnMarkerEnter and OnMarkerLeave event to set the selectedMarker

BUT
When I click on a marker it automaticaly jump a few pixel up without moving the mouse !
And it is quite impossible that way to move the marker

For now I'm trying to understand that behaviour
Any other suggestion ?
   // ********************************************************************************
    // move current marker with left holding
    void MainMap_MouseMove(object sender, MouseEventArgs e)
    {
      if (selectedMarker == null)
      {
        return;
      }
      if (e.Button == MouseButtons.Left ) // && isMouseDown)
      {
        if (selectedMarker.IsVisible)
        {
          selectedMarker.Position = MainMap.FromLocalToLatLng(e.X, e.Y);
        }
      }
    }
    // ********************************************************************************
    private void MainMap_OnMarkerEnter(GMapMarker item)
    {
      selectedMarker = item;
    }
    // ********************************************************************************
    private void MainMap_OnMarkerLeave(GMapMarker item)
    {
      selectedMarker = null;
    }
Sep 10, 2013 at 9:49 AM
Edited Sep 11, 2013 at 7:02 AM
Hello there. I'm new here, but i really like this GMap.NET project. I want to offer my solution of moving markers, where they don't jump a few pixel without moving mouse. It works just how you want it to work. Position changes by as much as you move mouse.
        private Point _mouseDownPosition = new Point(0, 0);
        private GPoint _markerStartPosition = new GPoint(0, 0);

        private void GmapBoxOnMouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (_selectedMarker == null)
            {
                return;
            }
            _mouseDownPosition = Mouse.GetPosition(GmapBox);
            _markerStartPosition = GmapBox.FromLatLngToLocal(_selectedMarker.Position);
        }

        private void GmapBoxOnMouseMove(object sender, MouseEventArgs mouseEventArgs)
        {
            if (_selectedMarker == null)
            {
                return;
            }
            if (mouseEventArgs.LeftButton ==  MouseButtonState.Pressed)
            {
                if (_selectedMarker.Shape.Visibility == Visibility.Visible)
                {
                    var p = Mouse.GetPosition(GmapBox);
                    var dx = p.X - _mouseDownPosition.X;
                    var dy = p.Y - _mouseDownPosition.Y;

                    _selectedMarker.Position = GmapBox.FromLocalToLatLng((int)(_markerStartPosition.X + dx), (int)(_markerStartPosition.Y + dy));
                    
                }
            }
        }
GmapBox is my Gmap control.
Don't use Enter and Leave events if you don't want mouse to loose marker. Use MouseDown and MouseUp:
                marker.Shape.MouseDown += (sender, args) =>
                    {
                        _selectedMarker = marker;
                    };
                marker.Shape.MouseUp += (sender, args) =>
                    {
                        _selectedMarker = null;
                    };