User Tools

Site Tools


source_code_webeditionsample_customscalebar_cs_100810.zip

Source Code WebEditionSample CustomScaleBar CS 100810.zip

CustomScaleBar.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using ThinkGeo.MapSuite.Core;
namespace CustomScaleBar
{
    public class CustomScaleBar : ScaleBarAdornmentLayer
    {
        private int width = 200;
        private string unitText;
        private double meterToUnit;
        private double unitRoundValue;
        private ManagedProj4Projection proj4;
        //Maximum width of the scale bar in screen coordinate
        public int MaxWidth
        {
            get { return width; }
            set { width = value; }
        }
        //Text to be displayed for the unit.
        public string UnitText
        {
            get { return unitText; }
            set { unitText = value; }
        }
        //Ratio of meter to the unit.
        public double MeterToUnit
        {
            get { return meterToUnit; }
            set { meterToUnit = value; }
        }
        //Projection for the map (internal) and the projection the scale bar is based on (external).
        public ManagedProj4Projection Projection
        {
            get { return proj4; }
            set { proj4 = value; }
        }
        protected override void DrawCore(GeoCanvas canvas, Collection<SimpleCandidate> labelsInAllLayers)
        {
            //Set the position of the scale bar on canvas.
            float Xpos = 15;
            float Ypos = canvas.Height - 40;
            //Gets the left and right location of the scale bar in world coordinate according to the maximum width and X and Y position.
            ScreenPointF screenLocation = GetDrawingLocation(canvas, width, Ypos);
            PointShape scaleBarMapPointShape = ExtentHelper.ToWorldCoordinate(canvas.CurrentWorldExtent, screenLocation.X, screenLocation.Y, canvas.Width, canvas.Height);
            PointShape scaleBarMapRightPointShape = ExtentHelper.ToWorldCoordinate(canvas.CurrentWorldExtent, screenLocation.X + this.width, screenLocation.Y, canvas.Width, canvas.Height);
            //Applies the reprojection so that the scale bar is not based on the displayed projection of the map (internal) but on the desired projection (external)
            //Typically the external projection will have distance characteristics more accurate than the displayed projection for a more reliable scalebar.
            proj4.Open();
            PointShape scaleBarMapPointShapeGeo = (PointShape)proj4.ConvertToExternalProjection(scaleBarMapPointShape);
            PointShape scaleBarMapRightPointShapeGeo = (PointShape)proj4.ConvertToExternalProjection(scaleBarMapRightPointShape);
            proj4.Close();
            //Gets the length of the scale bar according to the unit and the maximum width of the scale bar.
            double fullBarLength = scaleBarMapPointShapeGeo.GetDistanceTo(scaleBarMapRightPointShapeGeo,GeographyUnit.DecimalDegree, DistanceUnit.Meter);
            //Adjusts the length of the scale bar in order to have a round number.
            unitRoundValue = GetRoundValue(fullBarLength / meterToUnit);
            double barLength = ((unitRoundValue * meterToUnit) * this.width) / fullBarLength;
            //Draw the line of the scale bar according to the adjusted length.
            GeoPen pen = new GeoPen(GeoColor.StandardColors.Black, 2F);
            canvas.DrawLine(new ScreenPointF[]  {new ScreenPointF(Xpos, Ypos - 10), new ScreenPointF(Xpos, Ypos), new ScreenPointF((float)barLength + Xpos,
                        Ypos), new ScreenPointF((float)barLength + Xpos, Ypos - 10) }, pen, DrawingLevel.LevelOne, 0, 0);
            //Displays the text for the value and unit text.
            canvas.DrawText(Convert.ToString(unitRoundValue) + " " + unitText, new GeoFont("Arial", 8,DrawingFontStyles.Bold), new GeoSolidBrush(GeoColor.StandardColors.Black),
                new ScreenPointF[] { new ScreenPointF((float)(barLength / 2) + Xpos, Ypos - 10) }, DrawingLevel.LevelOne);
        }
        //Function to round down the length to fit within the maximum width of the scale bar.
        private double GetRoundValue(double unitValue)
        {
            double result;
            double interval = GetRoundingValue(unitValue);
            result = FloorNumber(unitValue, interval);
            return result;
        }
        //Gets the rounding value to be used according to the length of the unit value.
        private double GetRoundingValue(double UnitValue)
        {
            double result = 0;
            if (UnitValue > 100000) { result = 50000; }
            else if (UnitValue > 10000) { result = 5000; }
            else if (UnitValue > 1000) { result = 500; }
            else if (UnitValue > 100) { result = 50; }
            else if (UnitValue > 10) { result = 5; }
            else if (UnitValue > 1) { result = 0.5; }
            else if (UnitValue > 0.1) { result = 0.05; }
            else if (UnitValue > 0.01) { result = 0.005; }
            return result;
        }
        //Function used for rounding down a number according to the rounding value.
        private double FloorNumber(double Number, double Rounding)
        {
            double result = 0;
            double IEEERemainder = Math.IEEERemainder(Number, Rounding);
            if (IEEERemainder > 0)
                result = Number - IEEERemainder;
            else if (IEEERemainder < 0)
                result = (Number + Math.Abs(IEEERemainder)) - Rounding;
            else
                result = Number;
            return result;
        }
    }
}

TestForm.aspx.cs

using System;
using System.Configuration;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
namespace CustomScaleBar
{
    public partial class TestForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
                Map1.CurrentExtent = new RectangleShape(-341587,7130628,-333208,7125517);
                Map1.MapUnit = GeographyUnit.Meter;
                Map1.MapTools.MouseCoordinate.Enabled = true;
                BingMapsOverlay ve = new BingMapsOverlay("VirtualEarth Map");
                ve.MapType = BingMapsStyle.Road;
                Map1.CustomOverlays.Add(ve);
                LayerOverlay adornmentOverlay = new LayerOverlay("AdornmentOverlay", false, TileType.SingleTile);
                adornmentOverlay.TransitionEffect = TransitionEffect.None;
                //Projection to be used for the scale bar based on Google Map projection (Spherical Mercator)for the map projection and Geodetic (EPSG 4326) or decimal degrees to
                //calculate the scale bar.
                ManagedProj4Projection proj4 = new ManagedProj4Projection();
                proj4.InternalProjectionParametersString = ManagedProj4Projection.GetGoogleMapParametersString();
                proj4.ExternalProjectionParametersString = ManagedProj4Projection.GetEpsgParametersString(4326);
                CustomScaleBar customScaleBar = new CustomScaleBar();
                // Text to be displayed on the scale bar for kilometers.
                customScaleBar.UnitText = "Kilometers";
                //Ratio of meters to kilometes.
                customScaleBar.MeterToUnit = 1000;
                customScaleBar.Projection = proj4;
                adornmentOverlay.Layers.Add(customScaleBar);
                Map1.CustomOverlays.Add(adornmentOverlay);
            }
        }
    }
}

TestForm.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="CustomScaleBar.TestForm" %>
<%@ Register Assembly="WebEdition" Namespace="ThinkGeo.MapSuite.WebEdition" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <link href="~/theme/default/samplepic/style.css" rel="stylesheet" type="text/css" />
    <title>Custom Scale Bar</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <cc1:Map ID="Map1" runat="server" Height="100%" Width="100%">
    </cc1:Map>
   <%-- <description:descriptionpanel ID="DescPanel" runat="server">
        This sample displays a google map. Click buttons below to switch between different Google maps.
        <br />
        <br />
        <asp:Button CssClass="btn" ID="btnRoad" runat="server" Text="Normal" OnClientClick="Map1.SetCurrentBackgroundMapType(G_NORMAL_MAP); return false;" />
        <asp:Button CssClass="btn" ID="btnAerial" runat="server" Text="Hybrid" OnClientClick="Map1.SetCurrentBackgroundMapType(G_HYBRID_MAP); return false;" /><br />
        <asp:Button CssClass="btn" ID="btnSatellite" runat="server" Text="Satellite" OnClientClick="Map1.SetCurrentBackgroundMapType(G_SATELLITE_MAP); return false;" />
        <asp:Button CssClass="btn" ID="btnPhysical" runat="server" Text="Physical" OnClientClick="Map1.SetCurrentBackgroundMapType(G_PHYSICAL_MAP); return false;" />
    </description:descriptionpanel>--%>
    </form>
</body>
</html>
source_code_webeditionsample_customscalebar_cs_100810.zip.txt · Last modified: 2015/09/09 03:37 by admin