您的位置:首页 > 编程语言 > C#

用C#创建一个封装Google Map API的GoogleMap Web服务器控件(一)

2008-04-23 12:58 411 查看
最近想搞两个基于Google Map的网站玩玩,觉得直接操作Google Map API实在麻烦,于是就想用C#写一个封装Google
Map API的GoogleMap Web服务器控件,这样,用ASP.NET创建基于Google Map的网站就非常简单啦!

下面就让我们从头开始吧!

一. 首先打开Visual Studio 2008(其他IDE也可以),创建一个C#类库项目,命名为WindEagle.GoogleMap,勾选"创建解决方案目录"选项.

二. 删除默认的Class1.cs,修改项目属性,设置默认命名空间为"WindEagle.Web.UI.Controls".

打开AssemblyInfo.cs,作如下修改:

using System.Reflection;

[assembly: AssemblyTitle("WindEagle.GoogleMap")]

[assembly: AssemblyDescription("封装Google Map API的GoogleMap ASP.NET Web服务器控件")]

[assembly: AssemblyCompany("WindEagle")]

[assembly: AssemblyProduct("WindEagle.GoogleMap")]

[assembly: AssemblyCopyright("Copyright © WindEagle 2008")]

[assembly: AssemblyVersion("2.0.0.0")]

[assembly: AssemblyFileVersion("2.0.0.0")]

三. 在项目中添加一个ASP.NET Server Control,命名为GoogleMap.cs.

导入如下命名空间:

using System;

using System.ComponentModel;

using System.Security.Permissions;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

using System.Drawing.Design;

我们初步设计的GoogleMap控件应该有以下这些属性:

1. Height和Width, 因为Google Map的大小不能为零,且高宽比例一定,所以我决定重写继承于WebControl的Height和Width属性.

private static readonly Unit DefaultWidth = new Unit("1200px");

private static readonly Unit DefaultHeight = new Unit("960px");

public override Unit Height

public override Unit Width

[

Bindable(true),

Category("Google"),

DefaultValue(0),

Description("The zoom.")

]

public virtual int Zoom

[

Bindable(true),

Category("Google"),

DefaultValue(0),

Description("The latitude.")

]

public virtual double Latitude

[

Bindable(true),

Category("Google"),

DefaultValue(0),

Description("The longitude.")

]

public virtual double Longitude

[

Bindable(true),

Category("Google"),

DefaultValue(""),

Description("The address."),

Localizable(true)

]

public virtual string Address

[

Bindable(true),

Category("Google"),

DefaultValue(""),

Description("The user key obtained from google map api.")

]

public virtual string Key

protected override HtmlTextWriterTag TagKey

using System.Web.UI.Design;

namespace WindEagle.Web.UI.Controls

[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]

[ToolboxData("<{0}:GoogleMap runat=\"server\"></{0}:GoogleMap>")]

[Designer(typeof(GoogleMapDesigner))]

public class GoogleMap : WebControl

好了,下一篇文章中我将给GoogleMap类添加更多实用的属性.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐