博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArcGis API FOR Silverlight 做了个导航工具~
阅读量:6831 次
发布时间:2019-06-26

本文共 9609 字,大约阅读时间需要 32 分钟。

原文

转载请注明文章出处:

地图是读取的谷歌图层。。

主要是导航工具 做出来费劲呀。。

马上上代码

namespace ZoomwayWebGis.Controls.Generic{    [TemplatePart(Name = "PanLeft", Type = typeof(FrameworkElement))]    [TemplatePart(Name = "PanRight", Type = typeof(FrameworkElement))]    [TemplatePart(Name = "PanUp", Type = typeof(FrameworkElement))]    [TemplatePart(Name = "PanDown", Type = typeof(FrameworkElement))]    [TemplatePart(Name = "ZoomSlider", Type = typeof(Slider))]    [TemplatePart(Name = "ZoomInButton", Type = typeof(Button))]    [TemplatePart(Name = "ZoomOutButton", Type = typeof(Button))]    [TemplatePart(Name = "InstanceMark", Type = typeof(Button))]    public class MapNavigator : ContentControl    {        FrameworkElement PanLeft;        FrameworkElement PanRight;        FrameworkElement PanUp;        FrameworkElement PanDown;        Button ZoomInButton;        Button ZoomOutButton;        Slider ZoomSlider;        private double _panFactor = 0.5;        private const double MaxResolution = 23218.433769164774;        private const double MinResolution = 0.59716428347743467;        public MapNavigator()        {            this.DefaultStyleKey = typeof(MapNavigator);        }        public override void OnApplyTemplate()        {            base.OnApplyTemplate();            PanLeft = GetTemplateChild("PanLeft") as FrameworkElement;            PanRight = GetTemplateChild("PanRight") as FrameworkElement;            PanUp = GetTemplateChild("PanUp") as FrameworkElement;            PanDown = GetTemplateChild("PanDown") as FrameworkElement;            ZoomSlider = GetTemplateChild("ZoomSlider") as Slider;            ZoomInButton = GetTemplateChild("ZoomInButton") as Button;            ZoomOutButton = GetTemplateChild("ZoomOutButton") as Button;            enablePanElement(PanLeft);            enablePanElement(PanRight);            enablePanElement(PanUp);            enablePanElement(PanDown);            // Set control's flow direction to LTR to avoid flipping East and West keys:            this.FlowDirection = System.Windows.FlowDirection.LeftToRight;            if (ZoomSlider != null)            {                if (Map != null)                {                    SetupZoom();                }                ZoomSlider.Minimum = 0;                ZoomSlider.Maximum = 1;                ZoomSlider.SmallChange = .01;                ZoomSlider.LargeChange = .1;                ZoomSlider.LostMouseCapture += ZoomSlider_LostMouseCapture;                ZoomSlider.LostFocus += ZoomSlider_LostMouseCapture;            }            if (ZoomInButton != null)                ZoomInButton.Click += ZoomInButton_Click;            if (ZoomOutButton != null)                ZoomOutButton.Click += ZoomOutButton_Click;            bool isDesignMode = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this);            if (isDesignMode)                mouseOver = isDesignMode;            ChangeVisualState(false);        }             private void ZoomOutButton_Click(object sender, RoutedEventArgs e)        {            Map.Zoom(1 / Map.ZoomFactor);        }        private void ZoomInButton_Click(object sender, RoutedEventArgs e)        {            Map.Zoom(Map.ZoomFactor);        }        private void Map_ExtentChanged(object sender, ExtentEventArgs args)        {            if (!double.IsNaN(Map.Resolution) && ZoomSlider != null)                ZoomSlider.Value = ResolutionToValue(Map.Resolution);        }        #region State management        private bool mouseOver = false;        protected override void OnMouseLeave(MouseEventArgs e)        {            mouseOver = false;            if (!trackingRotation)                ChangeVisualState(true);            base.OnMouseLeave(e);        }        protected override void OnMouseEnter(MouseEventArgs e)        {            mouseOver = true;            ChangeVisualState(true);            base.OnMouseEnter(e);        }        private void ChangeVisualState(bool useTransitions)        {            if (mouseOver)            {                GoToState(useTransitions, "MouseOver");            }            else            {                GoToState(useTransitions, "Normal");            }        }        private bool GoToState(bool useTransitions, string stateName)        {            return VisualStateManager.GoToState(this, stateName, useTransitions);        }        #endregion        #region Rotation        Point startMousePos;        private double angle = 0;        private bool trackingRotation = false;        private double GetAngle(Point a, Point b)        {            if (a == null || b == null) return 0;            return Math.Atan2((b.X - a.X), (a.Y - b.Y)) / Math.PI * 180;        }        #endregion        #region Zoom        private void ZoomSlider_LostMouseCapture(object sender, EventArgs e)        {            Map.ZoomToResolution(ValueToResolution(ZoomSlider.Value));        }        #endregion        private void enablePanElement(FrameworkElement element)        {            if (element == null) return;            element.MouseLeave += panElement_MouseLeftButtonUp;            element.MouseLeftButtonDown += panElement_MouseLeftButtonDown;            element.MouseLeftButtonUp += panElement_MouseLeftButtonUp;        }        private void panElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            if (Map == null || sender == null) return;            Envelope env = Map.Extent;            if (env == null) return;            double x = 0, y = 0;            MapPoint oldCenter = env.GetCenter();            MapPoint newCenter = null;            var height = env.Height * _panFactor;            var width = env.Width * _panFactor;            // if units are degrees (the default), limit or alter panning to the lat/lon limits            if (sender == PanUp) // North            {                y = oldCenter.Y + height;                newCenter = new MapPoint(oldCenter.X, y);            }            else if (sender == PanRight) // East            {                x = oldCenter.X + width;                newCenter = new MapPoint(x, oldCenter.Y);            }            else if (sender == PanLeft) // West            {                x = oldCenter.X - width;                newCenter = new MapPoint(x, oldCenter.Y);            }            else if (sender == PanDown) // South            {                y = oldCenter.Y - height;                newCenter = new MapPoint(oldCenter.X, y);            }            if (newCenter != null)                Map.PanTo(newCenter);        }        private void panElement_MouseLeftButtonUp(object sender, MouseEventArgs e)        {            if (Map == null) return;        }        private double ValueToResolution(double value)        {            double max = Math.Log10(MaxResolution);            double min = Math.Log10(MinResolution);            double resLog = (1 - value) * (max - min) + min;            return Math.Pow(10, resLog);        }        private double ResolutionToValue(double resolution)        {            double max = Math.Log10(MaxResolution);            double min = Math.Log10(MinResolution);            double value = 1 - ((Math.Log10(resolution) - min) / (max - min));            return Math.Min(1, Math.Max(value, 0)); //cap values between 0..1        }        public void SetupZoom()        {            if (ZoomSlider != null && Map != null)            {                if (!double.IsNaN(MinResolution) && !double.IsNaN(MaxResolution) &&                    MaxResolution != double.MaxValue &&                    MinResolution != double.Epsilon &&                    !double.IsNaN(Map.Resolution))                {                    ZoomSlider.Value = ResolutionToValue(Map.Resolution);                }            }        }        #region Properties        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(MapNavigator), new PropertyMetadata(OnMapPropertyChanged));        private static void OnMapPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            MapNavigator nav = d as MapNavigator;            Map map = e.NewValue as Map;            Map oldmap = e.OldValue as Map;            if (oldmap != null)            {                oldmap.RotationChanged -= nav.Map_RotationChanged;                oldmap.ExtentChanged -= nav.Map_ExtentChanged;                oldmap.ExtentChanging -= nav.Map_ExtentChanged;                if (oldmap.Layers != null)                    oldmap.Layers.LayersInitialized -= nav.Layers_LayersInitialized;            }            if (map != null)            {                map.RotationChanged += nav.Map_RotationChanged;                map.ExtentChanged += nav.Map_ExtentChanged;                map.ExtentChanging += nav.Map_ExtentChanged;                if (map.Layers != null)                    map.Layers.LayersInitialized += nav.Layers_LayersInitialized;                nav.SetupZoom();            }        }        private void Layers_LayersInitialized(object sender, EventArgs args)        {            SetupZoom();        }        public Map Map        {            get { return GetValue(MapProperty) as Map; }            set { SetValue(MapProperty, value); }        }        public Double PanFactor { get { return _panFactor; } set { _panFactor = value; } }        private void Map_RotationChanged(object sender, DependencyPropertyChangedEventArgs e)        {            double value = (this.FlowDirection == System.Windows.FlowDirection.LeftToRight) ? (double)e.NewValue : -(double)e.NewValue;            angle = (double)e.NewValue;        }        #endregion    }

 

                            
你可能感兴趣的文章
3D文本悬停改变效果
查看>>
递归算法的时间复杂度
查看>>
有点不安全却又一亮的 Go unsafe.Pointer
查看>>
Linux安装mysql 8.0
查看>>
Webpack vs Rollup
查看>>
Springboot 前后端参数交互方式
查看>>
px、em、rem、%、vw、vh、vm等单位有什么区别?
查看>>
滴滴出行基于RocketMQ构建企业级消息队列服务的实践
查看>>
如何理解git rebase?
查看>>
程序部署到服务器服务无法启动问题
查看>>
以太坊源码分析—p2p节点发现与协议运行
查看>>
在MaxCompute上分析IP来源的方法
查看>>
JavaScript对象内部属性及其特性总结
查看>>
python学习笔记(二)
查看>>
css3动画效果抖动解决
查看>>
在React中你可以停止使用这五种常见写法
查看>>
为什么要用Redis
查看>>
SpringMVC学习笔记
查看>>
JDK源码学习1-ThreadPoolExecutor学习,先看注释
查看>>
使用VUE搭建后台管理系统(登陆篇)
查看>>