judero01col / GMap.NET

GMap.NET Windows Forms & Presentation is an excellent open source, powerful, free and cross-platform .NET control. Allows the use of routing, geocoding, directions and maps from Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac, Yendux, Mapy.cz, Maps.lt, iKarte.lv, NearMap, HereMap, CloudMade, WikiMapia, MapQuest and many more.
Other
440 stars 197 forks source link

Calculate zoom #173

Open samperizal opened 1 year ago

samperizal commented 1 year ago

Greetings.

It is possible to zoom automatically so that a polygon is visible in the control

Thank you

rlkollman commented 1 year ago

I don't think there's an extremely easy method for this, but, if you can determine the left-most, right-most, top-most, and bottom-most points of the polygon, you can create a rectangle (GMap.NET.RectLatLng()), then you should be able to use the MapControl.SetZoomToFitRect() method.

Below is what I came up with - there is likely a better way to capture the max/min of the lat/lng than the Linq statements I have commented out below. I tested it with the static values listed below and it worked for me. You may want to add/subtract to the max/min values a tad just to have a buffer, depending how you want it to appear.

// double latTop = gMapControl1.Overlays[0].Polygons[0].Points.Select(f => f.Lat).Min(); // double latLeft = gMapControl1.Overlays[0].Polygons[0].Points.Select(f => f.Lng).Min(); // double heightLat = latTop - (gMapControl1.Overlays[0].Polygons[0].Points.Select(f => f.Lat).Max()); // double widthLng = lngLeft - (gMapControl1.Overlays[0].Polygons[0].Points.Select(f => f.Lng).Max());

double latTop = 37.834143; double lngLeft = -95.202629; double heightLat = latTop - (34.779870); double widthLng = lngLeft - (-90.184173); GMap.NET.RectLatLng rect = new GMap.NET.RectLatLng(latTop, lngLeft, widthLng, heightLat); gMapControl1.SetZoomToFitRect(rect);