ThinkGeo.com    |     Documentation    |     Premium Support

Set map to specific scale

I am trying to let the user setup a scale for a particular map. For example, he wants to click a scale button, enter 2in = 1mi and click OK. The map would them use the center of the current extent and set the scale to what he entered.

I am using PagePrinterLayer, so I believe the width of the drawing area will be set to 44 inches (landscape).
PagePrinterLayer pagePrinterLayer = new PagePrinterLayer(PrinterPageSize.AnsiE, PrinterOrientation.Landscape);

In other words, I know my MapUnit (feet), I know my screen width (44 inches) what I need to set is the scale. This particular example means I need my scale so 22 miles of map fit in the width of my screen.

Hi Jake,

Please see whether this code as below is helpful:

double Mile = 1;
double inchInMile = Conversion.ConvertMeasureUnits(2, DistanceUnit.Inch, DistanceUnit.Mile);
double scale = Mile / inchInMile;

Regards,

Don

Don,

Thanks for the scale calculation. However, I have been attempting to use it during printing and not getting the results I want. My base map is in projection 3857 with meter MapUnit. When I build my print layer I am using the following:

 mapPrinterLayer.SetPosition(42,32, pageBoundingbox.GetCenterPoint().X, pageBoundingbox.GetCenterPoint().Y, PrintingUnit.Inch);
                var currentExtent = ExtentHelper.GetDrawingExtent(MapkitMap.CurrentExtent, (float)mapPrinterLayer.GetBoundingBox().Width, (float)mapPrinterLayer.GetBoundingBox().Height);//MapkitMap.CurrentExtent;
                mapPrinterLayer.MapExtent = currentExtent;
                mapPrinterLayer.BackgroundMask = new AreaStyle(new GeoPen(GeoColor.StandardColors.Black, 1));
                double mile = 1.0;
                double inchInMile = Conversion.ConvertMeasureUnits(2, DistanceUnit.Inch, DistanceUnit.Mile);
                var scale = mile/inchInMile;
                mapPrinterLayer.MapExtent = ExtentHelper.ZoomToScale(scale, mapPrinterLayer.MapExtent, mapPrinterLayer.MapUnit, (float)mapPrinterLayer.GetBoundingBox().Width, (float)mapPrinterLayer.GetBoundingBox().Height);

This appears to set the scale correctly and my scale bar will read 1 inch = 2640 Feet. However, when I then send this to the printer and use a ruler to measure 1 mile, I find that each mile is printed at a 3 inch scale, not the 2 inch scale I thought was set. Does this have anything to do with the projection I am using or the map units? I know there was an issue with GetLength functions and you introduced a GetAccurateLength to help with.

In the end, what I am looking for is how to set my scale so when I print, the scale bar that you provide is accurate.

 ScaleTextPrinterLayer scaleBar = new ScaleTextPrinterLayer(PrintingUnit.Inch, DistanceUnit.Mile, new GeoFont("Arial", 9f),
                    new GeoSolidBrush(GeoColor.SimpleColors.White), mapPrinterLayer);

                scaleBar.BackgroundMask = new AreaStyle(new GeoPen(GeoColor.SimpleColors.Black, 1), new GeoSolidBrush(GeoColor.SimpleColors.White));
                scaleBar.WorldUnit = DistanceUnit.Feet;
                scaleBar.MapPrinterLayer = mapPrinterLayer;
                scaleBar.TextBrush = new GeoSolidBrush(GeoColor.SimpleColors.Black);
                scaleBar.SetPosition(1.5, .25, (mapPrinterLayer.GetBoundingBox().LowerLeftPoint.X / 96) + .78, (mapPrinterLayer.GetBoundingBox().LowerLeftPoint.Y / 96) + .25, PrintingUnit.Inch);
                scaleBar.PrinterWrapMode = PrinterWrapMode.WrapText;
                printerInteractiveOverlay.PrinterLayers.Add("ScaleBar", scaleBar);

Hi Jake,

Have you tried to read the current scale value after this sentence: mapPrinterLayer.MapExtent = ExtentHelper.ZoomToScale

I remembered our WPF map have auto snap to nearest zoomlevel, if the scale is different with what scale you set, that maybe caused by snap.

Regards,

Don

The scale is not changing after I set it, there is no snapping to the nearest zoom level in this case. I added a scale bar as well, as you can see from the code above and it reads 1 inch = 2640 feet (1/2 mile). This would make it appear that the scale I am setting is accurate since I want 1 mile = 2 inches.

The issue comes then when I try to print that. After sending it to the printer when taking actual measurements, we find 1 mile = 3 inches. This does not match what the scale bar says.

ServicesEditionSample_PrintPreview_CS_WPF_111115.zip (493.0 KB)

I have stripped down and modified the sample attached. Here is how to test the scale issue.

Build the project (may need to fix references)
Run the application, you should see Bing Roads as the background.
Click Zoom In, this will take you to a location where the roads are 1 mile apart.
You should see in the bottom left the scale of 1 inch = 2640 feet.
If you print this picture and measure the distance between the roads, it comes out at close to 3 inches.

Hi Jake,

Thanks for your sample, I make it run local and print that to a A4 paper, but it looks the line shows on screen just the same like it shows on A4 paper, could you please let me know the length of the line so I can calculate the scale, and why you think it render correct in screen but not correct when print by printer, whether I ignored something here?

Regards,

Don

The roads in my example are 1 mile apart. You should be able to take a ruler and measure the distance between them (on the printed piece of paper). I come up with a little less than 3 inches for 1 mile. However, when looking at the scale text bar, we see 1 inch = 2640 feet. This means that 1 mile should be 2 inches. So either the text scale bar is incorrect, or the map is not rendering at the correct scale. My first guess is that the projection being used is screwing up your calculations for scale and distance.

Hi Jake,

Thanks for your detail description, we will look into this problem and reply you in ticket.

Regards,

Don