Moving map to center on new marker location

Topics: Windows Forms
Feb 21, 2013 at 9:48 PM
Edited Feb 21, 2013 at 10:16 PM
I am displaying my map and marker with the following code snippet.....
gMapControl1.Position = new PointLatLng(38.497501, -120.564003);
overlayOne = new GMapOverlay(gMapControl1, "OverlayOne");
marker1 = new GMap.NET.WindowsForms.Markers.GMapMarkerGoogleRed(new PointLatLng(0, 0));
marker1.Position = new PointLatLng(38.497501, -120.564003);
That's all fine. I then click on a button to execute the following code.
        private void button1_Click(object sender, EventArgs e)
            marker1.Position = new PointLatLng(38.4937, -120.582);
            if (marker1.IsVisible==false)
                MessageBox.Show("Not Visible");
                gMapControl1.Position = marker1.Position;
            else MessageBox.Show("Visible");
The marker will move to the new location. It disappears from view, and when I drag the map to the new location, I can see the marker where I intended it to be.

To my surprise, marker1.IsVisible is TRUE! I would expect it to be false once the new marker location is set. Am I not understanding how this works?

My ultimate goal is to move the marker, and if it is no longer visible in the current view, move the map so that the marker is once again centered on the map.

What am I not getting and what do I need to do different?


Edit #1:

I just figured out IsVisible doesn't do what I thought it does - it basically turns the marker off and on. OK, I am now a little smarter.

The discussions I've found on how to detect whether or not a particular marker is actually in view on the currently displayed map didn't seem to clear things up for me. So in short, my two questions are:

1) How can I tell when a marker is in the currently displayed map or not, and
2) How do I move the map to center on a marker?

I will continue to play with it while watching for answers. Thanks.

Edit #2:

OK, I can move my map to center on the new marker location. I just need to figure out how to detect it is out of view.

Still playing and looking for replies.
Feb 22, 2013 at 12:01 AM
OK, solution in hand. This routine works. It will need some refinement to make it a function that works in the general case, but it's a start.
        private void button4_Click(object sender, EventArgs e)
            double ULLat = gMapControl1.CurrentViewArea.Lat;
            double ULLng = gMapControl1.CurrentViewArea.Lng;
            double LRLat = ULLat - gMapControl1.CurrentViewArea.HeightLat;
            double LRLng = ULLng + gMapControl1.CurrentViewArea.WidthLng;

            if ((LRLng > marker1.Position.Lng) && (marker1.Position.Lng > ULLng) &&
                (LRLat < marker1.Position.Lat) && (marker1.Position.Lat < ULLat))
                MessageBox.Show("In View");
            else MessageBox.Show("Out of View");
UL = Upper Left
LR = Lower Right
Feb 22, 2013 at 12:39 PM
How about:
        private bool IsMarkerVisibleInMap(GMapControl map, GMapMarker marker)
            return map.CurrentViewArea.Contains(marker.Position);
Feb 22, 2013 at 4:49 PM
That's alot easier. Thank-you, Xandolph. -Chuck
Feb 22, 2013 at 7:53 PM
So, here's my final solution. It also takes into account the size of the marker, making sure it is entirely in view.

Thanks again to Xandolph.
        private Boolean IsInView(GMapControl Map, GMapMarker Marker)
            if ((Map.CurrentViewArea.Contains(Marker.Position)) &&
               (Marker.LocalPosition.X > 0) &&
               (Marker.LocalPosition.X < (Map.Bounds.Size.Width - Marker.Size.Width)) &&
               (Marker.LocalPosition.Y > 0) &&
               (Marker.LocalPosition.Y < (Map.Bounds.Size.Height - Marker.Size.Height)))
                return true;
            else return false;