使用silverlight制作热点地图

使用silverlight制作热点地图,第1张

概述参考文档:http://www.codeproject.com/Articles/49381/Silverlight-Creating-an-Image-Map-with-Hotspots  目前网络上关于热点地图的实现大多数都是通过flash实现的,在这里我记录下通过silverlight实现的方法。 需求: MainPage加载时展示世界地图,鼠标移动大洲区域变色突出显示。 显示鼠标停留大洲所

参考文档:http://www.codeproject.com/Articles/49381/Silverlight-Creating-an-Image-Map-with-Hotspots

 目前网络上关于热点地图的实现大多数都是通过flash实现的,在这里我记录下通过silverlight实现的方法。

需求:

MainPage加载时展示世界地图,鼠标移动大洲区域变色突出显示。 显示鼠标停留大洲所拥有的旅行线路条数,点击国家button进入相应搜索结果页。 点击大洲进入相应大洲的界面,该页面与MainPage类似,鼠标移入时显示结果略有不同,为该国家所拥有的线路名称与天数,即价格等。点击链接进入相应产品页。

 

截图如下:

 (MainPage)

 

 (大洲页,显示产品相关信息)

 

 开发工具:

visual studio 2010、Microsoft Expression design4 (主要拿来切图用,即用钢笔工具描出国家或者大洲的边界,工具生成的代码可以直接用在xaml文件中,具体用法见此处http://www.codeproject.com/Articles/49381/Silverlight-Creating-an-Image-Map-with-Hotspots写的很详细)

 

关键部分代码:

实现鼠标移动时目标区域变色功能

?
voID addMapEvents()       {           foreach (Canvas c in ( this .Findname( "map__matsuri" ) as Canvas).Children)           {               if (! string .IsNullOrEmpty(c.name))                                 c.MouseMove+= new MouseEventHandler(on_mouseMove);                                     c.MouseleftbuttonUp += new MousebuttonEventHandler(c_MouseleftbuttonUp);               }           }       }

  绑定鼠标移动的事件(如需在页面加载后就有此功能需将addMapEvents方法添加到页面的构造函数当中)

?
voID on_mouseMove( object sender,MouseEventArgs e)         {             Canvas c = sender as Canvas;             resetLastSelected();             if (! string .IsNullOrEmpty(c.name))             {                 if (c.name != lastSelected)                 {                     HIDeMenu();                 }                 lastSelected = c.name;                 Setuplinks();                 SetCanvascolor(c,colors.Blue,2,colors.Black);             }                           if (! string .IsNullOrEmpty(c.name))             {                 PopulateContextMenu(c.name);                 positionContextMenu(e.Getposition(contextMenu.Parent as UIElement), true );             }             @H_301_346@         }

  鼠标移动,区域变色的具体实现。SetCanvascolor(c,colors.Black); 其中在世界地图这个层面这里的canvas即为每个大洲。看到这里应该就比较清楚了,这种实现方式就是把一个世界地图用Expression design描出每一个大洲的边界(每一个大洲即为一个canvas)然后在鼠标移动的时候判断是在哪一个canvas上,并着色。

 

显示线路信息的解决办法

在这里分为两方面:

silverlight读取数据库信息。 d出/关闭菜单的动画效果。

关于silverlight读取数据库信息,开始我习惯性的找system.data,突然发现没有了(一番搜索之后才意识到silverlight是客户端程序不能直接访问数据库,一般都是采用webservice或是ria等访问的在本程序中我采用的是webservice这里没有太多可说的,需要注意的就是当修改了webservice服务中的方法后,silverlight端需要重新引用才能生效,否则会出现找不到新添加方法的情况。)

d出/关闭菜单的动画效果代码如下,

 

?
<Storyboard x:name= "HIDePopup" >       <DoubleAnimationUsingKeyFrames BeginTime= "0" Duration= "0" Storyboard.Targetname= "GrIDMenu" Storyboard.TargetProperty= "(UIElement.Opacity)" >           <discreteDoubleKeyFrame KeyTime= "0" Value= "0" />       </DoubleAnimationUsingKeyFrames>   </Storyboard>   <Storyboard x:name= "ShowPopup" >       <DoubleAnimationUsingKeyFrames BeginTime= "0" Storyboard.Targetname= "GrIDMenu" Storyboard.TargetProperty= "(UIElement.Opacity)" >           <SplineDoubleKeyFrame KeyTime= "0" Value= "0" />           <SplineDoubleKeyFrame KeyTime= "00:00:00.25" Value= "1" />       </DoubleAnimationUsingKeyFrames>   </Storyboard>

  同样的需要在后台绑定鼠标左键点击事件:LayoutRoot.MouseleftbuttonUp += new MousebuttonEventHandler(LayoutRoot_MouseleftbuttonUp);

然后显示的数据从数据库中读取,对button或者Hyperlinkbutton进行赋值即可。


原文地址:http://www.cnblogs.com/falcon-fei/archive/2012/08/06/2453531.HTML


Silverlight - Creating an Image Map with Hotspots

Introduction

Recently,I was working on a Silverlight application,and one of the pages had a graphic. One of the requirements was that the graphic should contain clickable regions (hotspots) and display a dynamic navigation menu when the end users click on it. An image that contains one or more hotspots or clickable areas is called an image map. I have put together a simple tutorial on how to get this done.

Getting Started

An image map can be created by using Microsoft Expression Design or Expression Blend.

If you do not have Microsoft Expression Design,you can download a trial version from here. If you do not have Microsoft Expression Blend,you can download a trial version from here. Microsoft Expression Designer

Prepare your favorite image. In this tutorial,I use a map graphic to demonstrate that we can create hotspots in different shapes with Microsoft Expression Designer/ Blend. I have downloaded a random map from here. Follow the steps from below.

Open Microsoft Expression Design 3. Go to file,New,fill up all the information,and click on the OK button. Go to VIEw,check Snap to Points and Snap to Pixels. file,import Image,and browse for your image and click the Open button. Double click on Layer1 and rename it to MainImage.

At this point,you should see something like below:

figure 1

Add a new layer at the top of the MainImage layer,highlight it,and select the Pen tool and draw a hotspot around Russia.

figure 2

Double click Layer 2 and name it Russia or something meaningful so we can utilize it in the Silverlight application. Highlight the points and click on the PropertIEs tab. See below:

figure 3

Pick a color you like and set its opacity to 40%.

figure 4

After you have finished drawing the hotspots,click on file,Export,and copy the setting from below.

figure 5

Putting everything together

Open Visual Studio 2008,go to file,Project,and choose the Silverlight Application template. If you don't have the Silverlight Controls Toolkit,you can download it from here. Make sure you have added the reference forSystem.windows.Controls.Toolkit. Open MainPage.xaml and drag a VIEwBox control on to the page. This control will stretch the map image insIDe it proportionally when we resize the browser. Then,copy the content inMapsHotSpot.xaml into the VIEwBox content in MainPage.xaml. See below.

Listing 1

 Collapse  |  Copy Code
<UserControl x:Class="MapsHotspotDemo.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:controlsToolkit=      "clr-namespace:System.windows.Controls;       assembly=System.windows.Controls.Toolkit"     xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:Ignorable="d" d:DesignWIDth="640"     d:DesignHeight="480">   <GrID x:name="LayoutRoot">        <controlsToolkit:VIEwBox  x:name="MapsVIEwBox"  ><!-- Maps content here -->           <Canvas               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"               x:name="MapsHotSpot"               WIDth="800" Height="600"               Clip="F1 M 0,0L 800,600L 0,0"               UseLayoutRounding="False">           ...           </Canvas><!-- End Maps content here -->        </controlsToolkit:VIEwBox>  </GrID></UserControl>
Expression Blend

If you choose to do it by using Expression Blend,here are the steps to follow:

Open Microsoft Expression Blend. Click on file,select New Project,and follow the setup from below. figure 6

Add a VIEwBox into the LayoutRoot and name it MapsVIEwBox,then add a Canvas insIDe the VIEwBox and name itMapsHotspot. After that,add a Canvas to MapsHotspot and an Image control to the Canvas. From the Imagecontrol propertIEs,specify the image source and setup the appropriate wIDth and height of the image. Then,create another Canvas and name it Russia or something meaningful or unique. Make sure that the wIDth and height of the newly created Canvas match the image in order to draw points on it. Click on the Pen tool and draw points or vertices around Russia. Pick a brush color and set its opacity to certain percentages. Repeat the same procedure for the rest of the countrIEs.

figure 7

tooltips

Remember that we named each layer at the beginning. The layers and points are translated into Canvas and Pathobjects,respectively,when we export them into XAML. We can provIDe each layer with a unique name,or include a tagattribute to it and use it to pull the country information from a database or resource dictionarIEs. For simplification sake,we will have a method to loop through each child element in the MapsHotSpot,grab the name,and set it to the tooltip content. Attach a MouseMove event to each layer to highlight the country on mouse hover. Also,attach aMouseleftbuttonUp event to pop up a menu when the left mouse button is released. See Listing 2.

Listing 2

 Collapse  |  Copy Code
foreach (Canvas c in (this.Findname("MapsHotSpot") as Canvas).Children){    if (!string.IsNullOrEmpty(c.name))    {        c.Cursor = Cursors.Hand;        tooltip tooltip = new tooltip { Content = c.name };        c.SetValue(tooltipService.tooltipProperty,tooltip);        c.MouseMove += new MouseEventHandler(c_MouseMove);        c.MouseleftbuttonUp += new MousebuttonEventHandler(c_MouseleftbuttonUp);    }}

displayed below is the implementation of the c_MouseMove method. This method will highlights the Canvas/ country and clears the prevIoUs selection,if there is any,during the mouse hover event.

Listing 3

 Collapse  |  Copy Code
voID c_MouseMove(object sender,MouseEventArgs e){    Canvas c = sender as Canvas;    resetLastSelected();    if (!string.IsNullOrEmpty(c.name))    {        lastSelected = c.name;        SetCanvascolor(c,color.FromArgb(255,92,112,171),2,colors.Green);    }}

VIEw in browser,you should see something like below. If you resize the browser,you will still see the full map. Place the mouse over the map image to see the tooltip and the highlighted region.

figure 8

We will build a simple menu with several links in it. First,add a simple class to hold the link propertIEs. See below for an example.

Listing 4

 Collapse  |  Copy Code
public class links{    public string Title { get; set; }    public string URL { get; set; }    public links(string t,string u)    {        Title = t;        URL = u;    }}

After that,create a global List (T) class to hold the link objects,and populate the link class with the below data on page load.

Listing 5

 Collapse  |  Copy Code
private List<links> _links = new List<links>();voID Setuplinks(){    _links.Add(new links("About the people","http://www.countryreports.org/{0}.aspx"));    _links.Add(new links("Economy","http://www.economicexpert.com/a/{0}.htm"));    _links.Add(new links("Global Statistics","http://www.geohive.com/cntry/{0}.aspx"));    _links.Add(new links("Population","http://www.geohive.com/charts/population1.aspx"));    _links.Add(new links("Wiki","http://en.wikipedia.org/wiki/{0}"));}

Below is the implementation of the c_MouseleftbuttonUp method. This method will bring up the popup menu by calling the PopulateContextMenu method in response to the mouse left button click event. ThePopulateContextMenu takes a country name as an argument,loops through the List of link objects,and concatenates the country name to it. The purpose of the positionContextMenu method is to set the popup menu position. I also added animation to fade in and fade out the popup menu,please refer to MainPage.xaml.

Listing 6

 Collapse  |  Copy Code
voID c_MouseleftbuttonUp(object sender,MousebuttonEventArgs e){    Canvas c = sender as Canvas;    if (!string.IsNullOrEmpty(c.name))    {        PopulateContextMenu(c.name);        positionContextMenu(e.Getposition(                 contextMenu.Parent as UIElement),true);        e.Handled = true;    }}voID positionContextMenu(Point p,bool useTransition){    if (useTransition)        contextMenu.IsOpen = false;    contextMenu.HorizontalOffset = p.X;    contextMenu.VerticalOffset = p.Y;    contextMenu.IsOpen = true;}voID PopulateContextMenu(string country){    contextListBox.Items.Clear();    foreach (links l in _links)    {        Hyperlinkbutton hlb = new Hyperlinkbutton();        hlb.Content = l.Title;        hlb.NavigateUri = new Uri(string.Format(l.URL,country));        hlb.Targetname = "_blank";        contextListBox.Items.Add(hlb);    }   }

At this point,you should see something like below:

figure 9

The final pIEce is to hIDe the popup menu and clear the highlighted country once we click on the non- clickable area of the map image. In order to achIEve that,we can attach a MouseleftbuttonUp event to the layout grID on page load. See below.

Listing 7

 Collapse  |  Copy Code
LayoutRoot.MouseleftbuttonUp +=    new MousebuttonEventHandler(LayoutRoot_MouseleftbuttonUp);voID LayoutRoot_MouseleftbuttonUp(object sender,MousebuttonEventArgs e){          resetLastSelected();    HIDeMenu();}voID HIDeMenu(){    HIDePopup.Begin();    contextMenu.HorizontalOffset = -50.0;}
Points of Interest

Initially,I was planning to use the right mouse button click event class that I found from here to trigger the popup menu,but I decIDed to leave it out after reading an article from here about its disadvantages.

I found this article useful although it was written in VB.NET and I can't get it to compile,but I'm able to utilize the logic embedded in it.

Conclusion

If you find any BUGs or disagree with the contents,please drop me a line and I'll work with you to correct it. I would suggest downloading the demo and exploring it in order to grasp the full concept of it because I might have left out some useful information. I hope someone will find this article useful on how to create image hotspots or map on images.

Resources Adding right click to Silverlight Limitations of Windowless mode for Silverlight Silverlight - World Map license

This article,along with any associated source code and files,is licensed under The Code Project Open License (CPOL)

总结

以上是内存溢出为你收集整理的使用silverlight制作热点地图全部内容,希望文章能够帮你解决使用silverlight制作热点地图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1018283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存