惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
V
Visual Studio Blog
有赞技术团队
有赞技术团队
U
Unit 42
D
Docker
小众软件
小众软件
F
Full Disclosure
I
Intezer
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
B
Blog
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
S
Security Affairs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
量子位
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Heimdal Security Blog
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
S
Schneier on Security

博客园 - Bēniaǒ

简化工作流程,10款必备的HTML5开发工具 Bing Maps开发扩展三:Bing Maps中渲染ESRI SHP空间数据 Bing Maps开发扩展二:基于Oracle Spatial的空间数据分析 Flash中各种图形的绘制 ASP.NET 4.5新特性一:强类型数据绑定(Strongly-Type Data-Bindings) 产品管理,明天会如何发展? 产品经理的34个感想 Bing Maps开发扩展一:Oracle Spatial的空间数据渲染 研发管理:产品研发团队的早会 中国式产品管理,探索中前行 Bing Maps WPF Control SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 SQL Server 2008空间数据应用系列十一:提取MapInfo地图数据中的空间数据解决方案 【动画技巧】GIF动画转SWF小技巧 【动画技巧】在Flash中自定义鼠标外观 SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息 Bing Maps Android SDK Released SQL Server 2008空间数据应用系列九:使用空间工具(Spatial Tools)导入ESRI格式地图数据 SQL Server 2008空间数据应用系列八:基于Bing Maps(Silverlight)的空间数据存储
DeepEarth自定义图元的中心位置纠偏
Bēniaǒ · 2011-11-19 · via 博客园 - Bēniaǒ

DeepEarth为B/S的地图应用开发提供了非常完善的解决方案,对于不熟悉Silverlight的同学来说,在开发中难免遇到各种大大小小的问题,本篇借鉴于最近网上一个朋友给我发邮件提出的问题,详细介绍下在DeepEarth中开发自定义图元中,如何去处理中心位置偏差的问题。

对于熟悉Silverlight的同学来说是一件很简单的事情,所谓的偏差无非就是坐标的偏移,通过坐标偏移技术(TranslateTransform)就可以解决此问题。首先我们先看看DeepEarth中对于默认的圆形图元(PointControl)和线条(LineStringControl)的渲染。

 

var dpoint1 = new PointControl()

{
    Location = new Location { Latitude = 40.554000079631, Longitude = 116.05599966645 }
};
var dpoint2 = new PointControl()
{
    Location = new Location { Latitude = 38.92, Longitude = 121.62 }
};
var dpoint3 = new PointControl()
{
    Location = new Location { Latitude = 31.23, Longitude = 121.47 }
};
var dpoint4 = new PointControl()
{
    Location = new Location { Latitude = 29.57, Longitude = 106.55 }
};
geometryLayer.Add(dpoint1);
geometryLayer.Add(dpoint2);
geometryLayer.Add(dpoint3);
geometryLayer.Add(dpoint4);
var dline = new LineStringControl
{
    Locations = new ObservableCollection<Location>
    {
        new Location { Latitude = 40.554000079631, Longitude = 116.05599966645 },
        new Location { Latitude = 38.92, Longitude = 121.62 },
        new Location { Latitude = 31.23, Longitude = 121.47 },
        new Location { Latitude = 29.57, Longitude = 106.55 }
    }
};
geometryLayer.Add(dline);

  以上使用DeepEarth内脏的PointControl和LineStringControl可以正常的渲染图元点和线条,且线条的连线也处于图元点的中心。默认图元不具备标签功能,最终要实现图元下面显示标签功能,我们可以通过扩展Pointcontrol来实现。

[TemplatePart(Name = "tbName", Type = typeof(TextBlock))]
public class PointComponent : PointControl
{
    protected  TextBlock tbName;
    private string nameValue = string.Empty;

    public PointComponent(string name)
        : base()
    {
        DefaultStyleKey = typeof(PointComponent);
        this.Style = Application.Current.Resources["PointStyle"as Style;
        this.nameValue = name;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.tbName = GetTemplateChild("tbName"as TextBlock;
        this.tbName.Text = nameValue;
    }

} 

  以上扩展控件(PointComponent)通过继承PointControl实现,其引用了样式PointStyle来定制控件呈现的外观。样式代码如下:

<Style TargetType="app:PointComponent" x:Key="PointStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="app:PointComponent">
                <Grid>
                    <Grid.RenderTransform>
                        <CompositeTransform x:Name="PART_CompositeTransform" />
                    </Grid.RenderTransform>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"/>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Ellipse Grid.Row="0" Height="16" Width="16" Fill="Yellow" Stroke="RoyalBlue" StrokeThickness="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Text="测试" Grid.Row="1" x:Name="tbName"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style> 

此时就可以使用扩展的控件进行图元渲染了,下面模拟坐标对扩展控件进行测试。

//北京

var point1 = new PointComponent("北京")
{
    Location = new Location { Latitude = 40.554000079631, Longitude = 116.05599966645 }
};
            
//大连
var point2 = new PointComponent("大连")
{
    Location = new Location { Latitude = 38.92, Longitude = 121.62 }
};

//上海
var point3 = new PointComponent("上海")
{
    Location = new Location { Latitude = 31.23, Longitude = 121.47 }
};

//重庆
var point4 = new PointComponent("重庆")
{
    Location = new Location { Latitude = 29.57, Longitude = 106.55 }
};
geometryLayer.Add(point1);
geometryLayer.Add(point2);
geometryLayer.Add(point3);
geometryLayer.Add(point4);

var line = new LineStringControl
{
    Locations = new ObservableCollection<Location>
    {
        new Location { Latitude = 40.554000079631, Longitude = 116.05599966645 },
        new Location { Latitude = 38.92, Longitude = 121.62 },
        new Location { Latitude = 31.23, Longitude = 121.47 },
        new Location { Latitude = 29.57, Longitude = 106.55 }
    }
};

geometryLayer.Add(line); 

 

仔细观察上图可发现,线条所连接的点并非是图元点的中心,而是整个扩展控件的中心,于此我们需要多扩展控件的位置进行适当的调整,以适应线条连接在圆形图元点的中心位置。PS:要处理这个位置偏差其实是很简单的事情,直接使用Silverlight的偏移技术(TranslateTransform)就搞定,不知为什么,会有那么多的同学提问如何解决。

可以通过工具Microsoft Expression Blend,合理调整扩展控件(PointComponent)的样式中圆形图元的偏移值,达到线条的链接指向扩展控件的圆形图元中心点。

调整的后的控件样式和最终效果图如下:

<Style TargetType="app:PointComponent" x:Key="PointStyle">

    

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="app:PointComponent">
                <Grid>
                    <Grid.RenderTransform>
                        <CompositeTransform x:Name="PART_CompositeTransform" />
                    </Grid.RenderTransform>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20"/>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Ellipse Grid.Row="0" Height="16" Width="16" Fill="Yellow" Stroke="RoyalBlue" StrokeThickness="2" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Ellipse.RenderTransform>
                            <TranslateTransform X="0" Y="8"></TranslateTransform>
                        </Ellipse.RenderTransform>
                    </Ellipse>
                    <TextBlock Text="测试" Grid.Row="1" x:Name="tbName">
                            <TextBlock.RenderTransform>
                                <TranslateTransform X="0" Y="4"></TranslateTransform>
                            </TextBlock.RenderTransform>
                    </TextBlock>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style> 

 

 

推荐阅读:

[1]、Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)

[2]、[Silverlight]Bing Maps开发系列文章