本文章是搜索各网站集合获得的 关于WindowsPhone7的Map控件基本应用资料:(均以表明出处,资料内容繁杂请各位读者各取所需自己整理。)
Windows Phone7开发之:地图控件
2010年11月22日00:48IT168
字号:T|T
本文是“Windows Phone 7 开发 31 日谈”系列的第20日。更多相关资料请进入移动开发论坛,以及Windows Phone 7入门开发专题。
昨天的长文是关于如何使用推送通知向你的手机发送数据的,即便程序没有运行。今天,我们重新回到控件,更具体的说,是地图控件。现在地理定位服务在移动应用程序中越来越流行,它的这种能告知用户身处何地以及身边有何物的能力变得越来越重要。
使用地图控件
作为Visual Studio 2010工具箱的一部分,你只需要将一个地图控件拖到页面中即可。这样做时,你会注意到在页面中添加了另一个XML名称空间。
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->xmlns:map=clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps
这是在我的例子中默认添加的XAML(在我调整好位置和尺寸后):
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
最后,来一张程序中地图的截图:
你会注意到在上图中央的白色文字写着“无效的证书。注册一个开发人员账户”。本文的剩余部分,我会说一下我们能对这个地图控件进行的所有不同的操作,包括获得一个有效地开发人员API密钥。
创建你的开发人员账户
在构建你的地图程序之前首先要做的就是获得一个Bing Map API密钥。这很简单,并且是免费的,并且可以将上面那行丑陋的白字去掉。要得到它,需要前往Bing地图站点并注册。完成后,你需要创建一个API密钥。窗体看起来是这样的:
当你填好后,他们会给你一个看起来像这样的API密钥:
AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9
(这不是我的API密钥。我替换了很多字符。但它看起来应该像你见到的这样。)
使用Credentials Provider属性
既然你已经有一个API密钥了,我们需要将它插入到我们的程序中。如果你的程序中只有一个地图控件,向下面这样使用就非常好:
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
如果你想重用这个值,应该将它保存到别的地方,比如App.xaml文件。像下面这个例子。我提供了你会在App.xaml文件和实际的地图控件中都能用到的代码。我们在App.xaml文件中创建了一个静态的CredentialsProvider,并在我们的页面中访问它。
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->App.xaml
Map Control
改变地图控件的特性
有很多选项可以改变地图的外观。例如,将道路模式改为空中模式,或者决定是否显示缩放级别选择器。你有很多很多可以设置的选项,它们都在Bing Maps Silverlight Control Interactive SDK中。我就不必在这里把所有选项都罗列一遍了(他们已经做了很多工作了),但我要告诉你如何从地图中获取你的数据。重点只讲两件事:在地图中添加图钉和自定义的形状。
在地图中添加图钉
在C#中,添加一个图钉就是创建一个Pushpin对象,设置它的位置,然后将它添加到地图中。在XAML中也可以实现。很明显,XAML为你提供了更快捷的方式,但其实哪种方式都不复杂。
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->XAML
C#
Pushpin pushpin = new Pushpin();
Location location = new Location();
location.Latitude = 40.1449;
location.Longitude = -82.9754;
pushpin.Location = location;
pushpin.Background = new SolidColorBrush(Colors.Orange);
pushpin.Content = 1;
pushpin.FontSize = 30;
MapControl.Children.Add(pushpin);
在上面的例子中,无论哪种方式,都会将一个图钉钉到我的办公室,在8800 Lyra Drive, Columbus可以找到。在我的程序中看起来是这样的:
如果你想知道如何将你的地址转换为经纬度,请参见我的这篇Silverlight 31日谈系列中的文章。它包含了地理地址编码以及你要在手机程序中所做的内容。
在地图中添加自定义XAML
在Map程序集中有一个小控件叫MapPolygon。向它提供一系列位置,它就会在你的地图中画出一个自定义的多边形,当用户缩放和移动地图时它仍会固定在那个位置。由于它是基于经纬度而绑定到地图的,所以程序中有需要,可以很容易地用它勾勒出国家,州,地区,甚至一个停车场的轮廓。下面是实现方法:
#div_code img{border:0px;}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->XAML
C#
MapPolygon mapPolygon = new MapPolygon();
mapPolygon.Fill = new SolidColorBrush(Colors.Purple);
mapPolygon.Stroke = new SolidColorBrush(Colors.White);
mapPolygon.Opacity = .7;
LocationCollection locations = new LocationCollection();
Location location = new Location();
location.Latitude = 40.1449;
location.Longitude = -82.9754;
Location location1 = new Location();
location1.Latitude = 40.1449;
location1.Longitude = -12.9754;
Location location2 = new Location();
location1.Latitude = 10.1449;
location1.Longitude = -82.9754;
locations.Add(location);
locations.Add(location1);
locations.Add(location2);
mapPolygon.Locations = locations;
MapControl.Children.Add(mapPolygon);
就是这些。我们已经将一个图钉和一个自定义多边形覆盖到了地图中。关于这个控件的更多功能请参见Bing Maps Silverlight Control Interactive SDK(下面是MapPolygon的截图)
在这个例子中,你可以找到用XAML和C#将图钉和多边形添加到地图中的例子。可能你不需要全部内容,可以取其一,这完全由你决定。
一起学windows phone7开发:Map控件的简单使用
作者:Free 文章来源:Free 点击数: 18 更新时间:2011-3-25
1. 注册地图:
在使用地图之前必须先申请register key https://www.bingmapsportal.com/
将申请到的key填到这个属性,地图才可以正常使用。
CredentialsProvider 属性:填写申请到的Register key。
2.设置中心点:
<my:Map Height=”607″ Mode=”Aerial” HorizontalAlignment=”Left” Name=”map1″ VerticalAlignment=”Top” Width=”468″ CredentialsProvider=”申请的map id号 ” ZoomBarVisibility=”Visible” ScaleVisibility=”Collapsed” Center=”30.259497,120.129798″ ZoomLevel=”15″/>
也可以用代码来实现
map1.Center = new GeoCoordinate(30.259497,120.129798);
map1.ZoomLevel = 15;
3.显示模式设置
Mode 属性:Road、Aerial,可以设置两种显示方式。
Aerial显示模式
Road显示模式
4.显示缩放按钮:
ZoomBarVisibility 属性:Visible、Collapsed。
5.比例尺显示:
ScaleVisibility 属性:Visible、Collapsed。
本文来自小镇的博客,原文地址:http://www.cnblogs.com/randylee/archive/2010/10/27/1862287.ht
Windows Phone 7 Silverlight控件之--Map入门
时间:2010-12-26 07:09来源:博客园 作者:永恒的记忆 点击: 161次
Map控件是微软专门为在Windows Phone 7手机上使用BingMap地图而开发的控件,在前面的博客中已写过如何使用在Silverlight的BingMap控件,今天讲一下如何使用Map控件,相比之下使用Map控件比使用Silverlight的BingMap控件简单一点,没有那么繁琐的步骤。 一、申请使用BingMap地图的Key。 在前面的博客中已写过,在这里不再赘
Map控件是微软专门为在Windows Phone 7手机上使用BingMap地图而开发的控件,在前面的博客中已写过如何使用在Silverlight的BingMap控件,今天讲一下如何使用Map控件,相比之下使用Map控件比使用Silverlight的BingMap控件简单一点,没有那么繁琐的步骤。
一、申请使用BingMap地图的Key。
在前面的博客中已写过,在这里不再赘述。即使没有Key也可以进行开发BingMap地图但是会出现下图提示,在地图中央会显示“Invail......”这么一段提示
如果使用Key的话就没有这段提示
二、重要方法和属性
1.说明
为在Windows Phone 7平台使用BingMap提供接口。
2.继承关系
Microsoft.Phone.Controls.Maps.Core.MapBase
Microsoft.Phone.Controls.Maps.Core.MapBase
System.Windows.Controls.ContentControl
System.Windows.Controls.Control
System.Windows.FrameworkElement
System.Windows.UIElement
System.Windows.DependencyObject
2.重要属性
CredentialsProvider:申请到的Key值
Mode:地图的显示模式,包含三种(Road路况模式、AerialWithLabels卫星有路标模式、Aerial卫星无路标模式)下图是三种模式的视图
AerialWithLabels卫星有路标模式
Road路况模式
Aerial卫星无路标模式
三、应用
<my:Map CredentialsProvider="AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC" Mode="Aerial" CopyrightVisibility="Collapsed"/>
CredentialsProvider="AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC"是Key
Mode="Aerial" 指定视图模式是路况模式
CopyrightVisibility="Collapsed" 是隐藏Copyright文本
效果如图
本节就介绍到这里,下一讲是map的基本使用,谢谢阅读!
本文来自永恒的记忆的博客,原文地址:http://www.cnblogs.com/salam/archive/2010/12/26/1917286.html
Windows Phone 7 Silverlight控件之--Map的基本控制
时间:2010-12-26 07:12来源:博客园 作者:永恒的记忆 点击: 294次
这节讲解Map的基本控制,根据经纬度定位,改变地图的焦距。 效果图 定位通过经纬度来控制。具体方法是SetView(); 它有以下几种类型参数 SetView(LocationRect boundingRectangle); SetView(GeoCoordinate center, double zoomLevel); SetView(GeoCoordinate center, dou
这节讲解Map的基本控制,根据经纬度定位,改变地图的焦距。
效果图
定位通过经纬度来控制。具体方法是SetView();
它有以下几种类型参数
SetView(LocationRect boundingRectangle);
SetView(GeoCoordinate center, double zoomLevel);
SetView(GeoCoordinate center, double zoomLevel, double heading);
SetView(GeoCoordinate center, double zoomLevel, double heading, double pitch);
ZoomLevel属性用于控制地图的焦距。
在本例中使用的是第一种参数方法。
1.MapBaseControl.xaml
<phone:PhoneApplicationPage
xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
x:Class="WindowsPhoneBingMap.MapBaseControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<my:Map x:Name="myMap" CredentialsProvider="AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC" Mode="Road" CopyrightVisibility="Collapsed"/>
<Canvas Background="Black" x:Name="locate" Width="300" Height="320" Visibility="Collapsed" HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock TextAlignment="Center" FontSize="32" Text="输入经纬度定位地图" Grid.Row="0" Grid.ColumnSpan="2"></TextBlock>
<TextBlock Text="经度:" FontSize="32" Grid.Row="1" Grid.Column="0" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="txtLongitude" Grid.Row="1" Grid.Column="1" Width="220"></TextBox>
<TextBlock Text="纬度:" FontSize="32" Grid.Row="2" Grid.Column="0" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="txtLatitude" Grid.Row="2" Grid.Column="1"></TextBox>
<TextBlock Text="焦距:" FontSize="32" Grid.Row="3" Grid.Column="0" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="txtZoomlevel" Grid.Row="3" Grid.Column="1"></TextBox>
<Button x:Name="btnLocate" Content="定位" Grid.Row="4" Grid.ColumnSpan="2" Width="100" Click="btnLocate_Click"/>
</Grid>
</Canvas>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="ok.png" Text="定位" Click="ApplicationBarIconButton_Click"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
2.MapBaseControl.cs
public partial class MapBaseControl : PhoneApplicationPage
{
public MapBaseControl()
{
InitializeComponent();
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
this.locate.Visibility = Visibility.Visible;
}
private void btnLocate_Click(object sender, RoutedEventArgs e)
{
//纬度
double latitude = 0;
//经度
double longitude = 0;
//焦距
double zoomLevel = 1d;
double.TryParse(txtLatitude.Text, out latitude);
double.TryParse(txtLongitude.Text, out longitude);
double.TryParse(txtZoomlevel.Text, out zoomLevel);
this.myMap.SetView(new LocationRect(new GeoCoordinate(latitude, longitude), 80, 80));
this.myMap.ZoomLevel = zoomLevel;
this.locate.Visibility = Visibility.Collapsed;
}
}
本文来自永恒的记忆的博客,原文地址:http://www.cnblogs.com/salam/archive/2010/12/26/1917327.html
北京 北纬39.55 东经116.24
济南 北纬36.40 东经117.00
台北 北纬25.03 东经121.30
香港 北纬21.23 东经115.12
澳门 北纬21.33 东经115.07
1. 加一个标记:
加标记,是地图中最常用的方法。它和bing map又有所不同,首先所在的命名空间不同;其次显示方式不同;但总之都是可以在模拟上运行的。
Pushpinpin = new Pushpin();
pin.Location = new GeoCoordinate(30.259497, 120.129798);
pin.Width = 200;
pin.Height = 200;
pin.Content = "test";
pin.Background = new SolidColorBrush(Colors.Red);
Map控件显示的标记
bing map控件显示的标记
2. 绘制多边型区域:
MapPolygonpolygon = new MapPolygon();
polygon.Locations = new LocationCollection() { new GeoCoordinate(30.259497, 120.129798),
new GeoCoordinate(30.359497, 120.329998),
new GeoCoordinate(30.379497, 120.529798),
new GeoCoordinate(30.389497, 120.729798) };
3. 绘制多边线:
MapPolylinepolyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Red);
polyline.Locations = new LocationCollection() {
new GeoCoordinate(30.259497, 120.129798),
new GeoCoordinate(30.289497, 120.120998)
};
4.在地图上增加图片:
MapLayerimagelayer = new MapLayer();
imagelayer.AddChild(image, new GeoCoordinate(30.259497, 120.129798), PositionOrigin.BottomLeft);
编缉推荐阅读以下文章
Windows Phone7开发之:地图控件
2010年11月22日00:05 it168网站原创 作者:张崟 编辑:景保玉 评论:0条
本文Tag: 微软移动 Windows Phone 移动开发
【IT168技术】本文是“Windows Phone 7 开发 31 日谈”系列的第20日。更多相关资料请进入移动开发论坛,以及Windows Phone 7入门开发专题。
昨天的长文是关于如何使用推送通知向你的手机发送数据的,即便程序没有运行。今天,我们重新回到控件,更具体的说,是地图控件。现在地理定位服务在移动应用程序中越来越流行,它的这种能告知用户身处何地以及身边有何物的能力变得越来越重要。
使用地图控件
作为Visual Studio 2010工具箱的一部分,你只需要将一个地图控件拖到页面中即可。这样做时,你会注意到在页面中添加了另一个XML名称空间。
xmlns:map="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
这是在我的例子中默认添加的XAML(在我调整好位置和尺寸后):
<map:Map Height="607" HorizontalAlignment="Left" Name="myMap" VerticalAlignment="Top" Width="456" />
最后,来一张程序中地图的截图:
你会注意到在上图中央的白色文字写着“无效的证书。注册一个开发人员账户”。本文的剩余部分,我会说一下我们能对这个地图控件进行的所有不同的操作,包括获得一个有效地开发人员API密钥。
创建你的开发人员账户
在构建你的地图程序之前首先要做的就是获得一个Bing Map API密钥。这很简单,并且是免费的,并且可以将上面那行丑陋的白字去掉。要得到它,需要前往Bing地图站点并注册。完成后,你需要创建一个API密钥。窗体看起来是这样的:
当你填好后,他们会给你一个看起来像这样的API密钥:
AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9
(这不是我的API密钥。我替换了很多字符。但它看起来应该像你见到的这样。)
使用Credentials Provider属性
既然你已经有一个API密钥了,我们需要将它插入到我们的程序中。如果你的程序中只有一个地图控件,向下面这样使用就非常好:
<map:Map CredentialsProvider="AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9">
如果你想重用这个值,应该将它保存到别的地方,比如App.xaml文件。像下面这个例子。我提供了你会在App.xaml文件和实际的地图控件中都能用到的代码。我们在App.xaml文件中创建了一个静态的CredentialsProvider,并在我们的页面中访问它。
App.xaml
<Application.Resources>
<map:ApplicationIdCredentialsProvider ApplicationId="AsWlUnHEvLgHlLHaRqTZLslewv1QIdGppxOqyL-7He2jxyHvLAjutrcntemUih-w9" x:Key="BingMapsAPIKey"></map:ApplicationIdCredentialsProvider>
</Application.Resources>
Map Control
<map:Map CredentialsProvider="{StaticResource BingMapsAPIKey}">
改变地图控件的特性
有很多选项可以改变地图的外观。例如,将道路模式改为空中模式,或者决定是否显示缩放级别选择器。你有很多很多可以设置的选项,它们都在Bing Maps Silverlight Control Interactive SDK中。我就不必在这里把所有选项都罗列一遍了(他们已经做了很多工作了),但我要告诉你如何从地图中获取你的数据。重点只讲两件事:在地图中添加图钉和自定义的形状。
在地图中添加图钉
在C#中,添加一个图钉就是创建一个Pushpin对象,设置它的位置,然后将它添加到地图中。在XAML中也可以实现。很明显,XAML为你提供了更快捷的方式,但其实哪种方式都不复杂。
XAML
<map:Pushpin Location="40.1449, -82.9754" FontSize="30" Background="Orange" Content="1" />
C#
Pushpin pushpin = new Pushpin();
Location location = new Location();
location.Latitude = 40.1449;
location.Longitude = -82.9754;
pushpin.Location = location;
pushpin.Background = new SolidColorBrush(Colors.Orange);
pushpin.Content = "1";
pushpin.FontSize = 30;
MapControl.Children.Add(pushpin);
在上面的例子中,无论哪种方式,都会将一个图钉钉到我的办公室,在8800 Lyra Drive, Columbus可以找到。在我的程序中看起来是这样的:
如果你想知道如何将你的地址转换为经纬度,请参见我的这篇Silverlight 31日谈系列中的文章。它包含了地理地址编码以及你要在手机程序中所做的内容。
在地图中添加自定义XAML
在Map程序集中有一个小控件叫MapPolygon。向它提供一系列位置,它就会在你的地图中画出一个自定义的多边形,当用户缩放和移动地图时它仍会固定在那个位置。由于它是基于经纬度而绑定到地图的,所以程序中有需要,可以很容易地用它勾勒出国家,州,地区,甚至一个停车场的轮廓。下面是实现方法:
XAML
<map:MapPolygon Fill="Purple" Stroke="White" Opacity=".7" Locations="40.1449,-82.9754 40.1449,-12.9754 10.1449,-82.9754" />
C#
MapPolygon mapPolygon = new MapPolygon();
mapPolygon.Fill = new SolidColorBrush(Colors.Purple);
mapPolygon.Stroke = new SolidColorBrush(Colors.White);
mapPolygon.Opacity = .7;
LocationCollection locations = new LocationCollection();
Location location = new Location();
location.Latitude = 40.1449;
location.Longitude = -82.9754;
Location location1 = new Location();
location1.Latitude = 40.1449;
location1.Longitude = -12.9754;
Location location2 = new Location();
location1.Latitude = 10.1449;
location1.Longitude = -82.9754;
locations.Add(location);
locations.Add(location1);
locations.Add(location2);
mapPolygon.Locations = locations;
MapControl.Children.Add(mapPolygon);
就是这些。我们已经将一个图钉和一个自定义多边形覆盖到了地图中。关于这个控件的更多功能请参见Bing Maps Silverlight Control Interactive SDK(下面是MapPolygon的截图)
在这个例子中,你可以找到用XAML和C#将图钉和多边形添加到地图中的例子。可能你不需要全部内容,可以取其一,这完全由你决定。
Developing Web Applications with the Web ADF - Tutorials
Developing a scriptable Web ADF control
A key consideration when designing a custom web control is where the control's logic will execute. While all the logic could be put in either the client or the web tier, the best approach is usually to place some logic in each tier. User interface data and operations should usually be situated in the client tier, while data retrieval and heavy computational logic should (and often must) reside in the web tier. Distributing the control in this way maximizes the use of client resources and reduces server load. Additionally, in order to minimize network traffic, client tier functionality should initiate server tier logic only when necessary (e.g. to query a database housed in the web tier). Adhering to this architecture enables the development of controls with rich and responsive user interfaces.
Given the advantages of this architecture, the question becomes one of implementation. How can we efficiently create an implementation that is intuitive, maintainable, extensible, and re-distributable? One effective approach is to create a scriptable ASP.NET server control that inherits from the Web ADF WebControl. Scriptable server controls offer a framework for combining server and client logic into one easily re-distributable control. This framework allows for the partitioning of client and server logic as described above while keeping that logic housed in one control and adherent to object-oriented programming standards. Furthermore, inheriting from the Web ADF WebControl will automatically package the Microsoft ASP.NET AJAX and Web ADF JavaScript Libraries with the control, making these available to client tier operations.
This is the approach used by developers of the Web ADF in implementing web controls, and an example of its use is presented below. The walkthrough illustrates implementing a MapCoordinateDisplay control, which displays the current position of the mouse cursor over a buddied Map control.
1. Create a new 'ASP.Net Server Control' project in Visual Studio 2008 or a new 'Web Control Library' in Visual Studio 2005.
2. Add references to the 'ESRI.ArcGIS.ADF.Web.UI.WebControls.dll', 'System.Web.Extensions.dll' and 'AjaxControlToolkit' assemblies.
3. Rename the default name of the Server control (ServerControl1) to the more intuitive MapCoordinateDisplay and derive the class from the base ADF web control class. Make sure to change the name in the ToolboxData attribute as well as the class declaration.
[C#]
4. namespace MapCoordinateDisplay
5. {
6. [DefaultProperty("Text")]
7. [ToolboxData("<{0}:MapCoordinateDisplay runat=server></{0}:MapCoordinateDisplay>")]
8. public class MapCoordinateDisplay : ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl
{
9. Delete the default stub implementation of RenderContents inserted by Visual Studio.
10. Replace the getter and setter for the Text property to use StateManager to store state.
[C#]
11. public string Text
12. {
13. get
14. {
15. return StateManager.GetProperty("text") as string;
16. }
17. set
18. {
19. StateManager.SetProperty("text", value);
20. }
}
21. In order to buddy to the Map control, we add a property to store the ID of the map. We store the ID and not an actual reference to the control so that the property can be specified in markup and the control is not stored in session state. As explained in the best practices outlined later in this post, storing web controls in session state causes memory leaks and is therefore bad practice.
[C#]
22. /// <summary>The ID of the Map control to associate with this control.</summary>
23. public string Map
24. {
25. get
26. {
27.
28. return StateManager.GetProperty("map") as string;
29. }
30. set
31. {
32. StateManager.SetProperty("map", value);
33. }
}
34. Add a Label control as a member variable. This will be used to display the map coordinates.
[C#]
35. public class MapCoordinateDisplay : ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl
36. {
private Label m_displayLabel;
37. Override the CreateChildControls method to control the creation of sub controls.
[C#]
38. protected override void CreateChildControls()
39. {
40. Controls.Clear();
41. base.CreateChildControls();
42.
43. m_displayLabel = new Label();
44. m_displayLabel.ID = "DisplayLabel";
45. m_displayLabel.Text = Text;
46. m_displayLabel.Font.Bold = true;
47. Controls.Add(m_displayLabel);
}
48. For the client script component, add a new folder named "javascript" then add a new JScript file called "MapCoordinateDisplay.js". Set the file's build action to "Embedded Resource".
49. Mark the JavaScript file as an embedded resource in the AssemblyInfo.cs file (in the Properties folder). Pay special attention to the namespace and casing of the WebResource name attribute.
[C#]
[assembly: System.Web.UI.WebResource("MapCoordinateDisplay.javascript.MapCoordinateDisplay.js", "text/javascript")]
50. To include the JavaScript auto-magically with the server control, add the following custom attribute to the class definition. Again, pay special attention to the namespace and casing of the ClientScriptResource attribute.
[C#]
51. namespace MapCoordinateDisplay
52. {
53. [DefaultProperty("Text")]
54. [ToolboxData("<{0}:MapCoordinateDisplay runat=server></{0}:MapCoordinateDisplay>")]
55. [AjaxControlToolkit.ClientScriptResource("MapCoordinateDisplay.MapCoordinateDisplay",
56. "MapCoordinateDisplay.javascript.MapCoordinateDisplay.js")]
57. public class MapCoordinateDisplay : ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl
{
58. Add the following code in MapCoordinateDisplay.js to create a client-side representation of the control. Here we use the ASP.NET AJAX framework for convenient namespace and class registration. We also give the control AJAX client control functionality by specifying inheritance from the ASP.NET AJAX client control base class (Sys.UI.Control) in the call to registerClass. Recall that, since our server control inherits from the Web ADF WebControl base class, we can safely assume that ASP.NET AJAX functions are available to be called.
[JavaScript]
59. /// <reference name="MicrosoftAjax.js"/>
60. /// <reference assembly="ESRI.ArcGIS.ADF.Web.UI.WebControls" name="ESRI.ArcGIS.ADF.Web.UI.WebControls.Runtime.JavaScript.references.js"/>
61.
62. Type.registerNamespace('MapCoordinateDisplay');
63.
64. MapCoordinateDisplay.MapCoordinateDisplay = function(element) {
65. MapCoordinateDisplay.MapCoordinateDisplay.initializeBase(this, [element]);
66. }
67.
68. MapCoordinateDisplay.MapCoordinateDisplay.prototype = {
69. initialize : function() {
70. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'initialize');
71. // Add custom initialization here
72. },
73. dispose : function() {
74. //Add custom dispose actions here
75. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'dispose');
76. }
77. }
78.
79. MapCoordinateDisplay.MapCoordinateDisplay.registerClass('MapCoordinateDisplay.MapCoordinateDisplay', Sys.UI.Control);
80.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
81. Add a property to the component to encapsulate access to the buddied map control.
[JavaScript]
82. MapCoordinateDisplay.MapCoordinateDisplay = function(element) {
83. MapCoordinateDisplay.MapCoordinateDisplay.initializeBase(this, [element]);
84. this._map = null;
85. }
86.
87. MapCoordinateDisplay.MapCoordinateDisplay.prototype = {
88. initialize : function() {
89. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'initialize');
90. // Add custom initialization here
91. },
92. get_map : function() {
93. return this._map;
94. },
95. set_map : function(value) {
96. this._map = value;
97. },
98. dispose : function() {
99. //Add custom dispose actions here
100. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'dispose');
101. }
}
102. Add another property to store the reference to the DOM element rendered by the label (child) control we included in the server control implementation. This element can then be manipulated using DHTML and JavaScript.
[JavaScript]
103. MapCoordinateDisplay.MapCoordinateDisplay = function(element) {
104. MapCoordinateDisplay.MapCoordinateDisplay.initializeBase(this, [element]);
105. this._map = null;
106. this._displayLabel = null;
107. }
108.
109. MapCoordinateDisplay.MapCoordinateDisplay.prototype = {
110. initialize : function() {
111. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'initialize');
112. // Add custom initialization here
113. },
114. get_map : function() {
115. return this._map;
116. },
117. set_map : function(value) {
118. this._map = value;
119. },
120. get_displayLabel : function() {
121. return this._displayLabel;
122. },
123. set_displayLabel : function(value) {
124. this._displayLabel = value;
125. },
126. dispose : function() {
127. //Add custom dispose actions here
128. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'dispose');
129. }
}
130. Attach an event listener to the map's mouse move event. We wrap the listener in a call to createDelegate so that the "this" keyword in the listener will refer to the MapCoordinateDisplay control.
[JavaScript]
131. MapCoordinateDisplay.MapCoordinateDisplay.prototype = {
132. initialize : function() {
133. MapCoordinateDisplay.MapCoordinateDisplay.callBaseMethod(this, 'initialize');
134. // Add custom initialization here
135. if(this._map) {
136. this._map.add_mouseMove(Function.createDelegate(this, this._onMapMouseMove));
137. }
138. },
139. _onMapMouseMove : function(sender, args) {
140. // Display the co-ordinates
141. },
142. get_map : function() {
143. return this._map;
144. },
145. Define the handler for the mouseMove event to display the coordinates by manipulating the innerHTML property of the label using DHTML and JavaScript.
[JavaScript]
146.
147. _onMapMouseMove : function(sender, args) {
148. // Display the co-ordinates
149. if(this._displayLabel) {
150. this._displayLabel.innerHTML = args.coordinate.toString();
151. }
152. },
153. The only remaining part of our scriptable control implementation is including code that will create a corresponding MapCoordinateDisplay client control for any web tier instance. To do this, we construct JavaScript to instantiate the client control with the relevant server control properties and then register this script to so that it executes on application initialization. Note that, since our control derives from the base ADF WebControl, we can safely reference ASP.NET AJAX events and functions. This initialization code should be specified in the OnPreRender method as follows:
[C#]
154. protected override void OnPreRender(EventArgs e)
155. {
156. base.OnPreRender(e);
157.
158. if (!base.IsAsync)
159. {
160. StringBuilder script = new StringBuilder();
161. script.Append("Sys.Application.add_init(function() {");
162. script.Append("$create(MapCoordinateDisplay.MapCoordinateDisplay,");
163. script.AppendFormat("{{\"displayLabel\":$get('{0}')", m_displayLabel.ClientID);
164. script.AppendFormat("}}, null, {{\"map\":\"{0}\"}}, $get('{1}'));", Map, ClientID);
165. script.AppendLine("});");
166.
167. ScriptManager.RegisterStartupScript(this, typeof(MapCoordinateDisplay), this.ClientID+"_startup",script.ToString(), true);
168. }
}
169. To test the control, Build the control assembly and deploy it in an application with a test page that includes a map and a map resource manager. Buddy the new control to the map. At runtime, as the mouse cursor moves over the map, he map coordinates in the new script control change accordingly.
For additional information on best practices regarding creating custom Web ADF controls, see the topic Best Practices > Developing custom Web ADF controls .