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

推荐订阅源

GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
I
InfoQ
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News

博客园 - zhangdong

软件架构和设计 在Visual Studio 2010中定义GDI+自定义控件——自定义控件介绍 Visual Studio 2010 Team System 动手实验室——架构师工具 Visual Studio 2010 Team System 动手实验室 Silverlight 框架 .NET Framework 3.5 Silverlight 的发展之路 Microsoft Exchange Server 2010 介绍 Silverlight 3 —— 介绍 搭建基于MOSS的团队解决方案01——Microsoft Office SharePoint 2007 Server快速入门(Continued) 搭建基于MOSS的团队解决方案01——Microsoft Office SharePoint 2007 Server快速入门 Windows Workflow Foundation实验01——Windows Workflow Foundation快速入门(练习四) Windows Workflow Foundation 实验01 —— Windows Workflow Foundation 快速入门(练习三) Windows Workflow Foundation实验01——Windows Workflow Foundation 快速入门(练习二) Windows Workflow Foundation实验01——Windows Workflow Foundation快速入门(练习一) Windows Workflow Foundation实验——安装与准备 使用.NET平台工具操作Live Framework 地图控件快速入门——控制地图 WCF中的管道——管道类型
地图控件快速入门 - zhangdong - 博客园
zhangdong · 2010-01-13 · via 博客园 - zhangdong

显示一个地图:

使用Virtual Earth地图控件的第一步是在你的网站上显示一个地图。

现实默认地图:

1. 新建html页面命名为:HTMLPageDefaultMap.htm。

   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:   
   3:  <html xmlns="http://www.w3.org/1999/xhtml">
   4:  <head>
   5:      <title></title>
   6:  </head>
   7:  <body>
   8:   
   9:  </body>
  10:  </html>

2. 在header配置节中,添加一个META元素以将charset属性设置为utf-8。

   1:  <head>
   2:      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   3:      <title></title>
   4:  </head>

注意:建议在你的网页中使用UTF-8。

3. 同样是在header配置节中,添加一个指向地图控件的引用。

   1:  <head>
   2:      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   3:      <script charset="UTF-8" type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us">
   4:      </script>
   5:      <title></title>
   6:  </head>

4. 在页面的body中,添加一个Div元素来容纳地图。地图的大小由Div的高度和宽度定义。地图的位置使用position,top,left属性进行配置。你可以直接设置他们的值,或者在样式表中定义。

   1:  .map {
   2:     position: absolute;
   3:     top: 20;
   4:     left: 10;
   5:     width: 400px;
   6:     height: 400px;
   7:     border:#555555 2px solid;
   8:  }
   1:  <head>
   2:      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   3:      <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
   4:      <script charset="UTF-8" type="text/javascript" 
                   src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us">
   5:      </script>
   6:      <title></title>
   7:  </head>
   8:  <body>
   9:      <!--<div id='myMap' style="position:absolute; width:400px; height:400px;"></div>-->
  10:      <div id="myMap" class="map"></div>
  11:  </body>

注意:如果不定义宽度,默认宽度为600px。默认高度为400px。为了跨浏览器的兼容性,你应该总是定义position属性。如果你在地图的Div上使用一个百分比的宽度和高度,它将采用父级容器的百分比来定义。

5. 创建一个VEMap类的实例,并调用VEMap.LoadMap方法。

   1:  <head>
   2:      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   3:      <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
   4:      <script charset="UTF-8" type="text/javascript" 
                   src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us">
   5:      </script>
   6:      <script type="text/javascript">
   7:          function GetMap() {
   8:              var map = new VEMap('myMap');
   9:              map.LoadMap();
  10:          }
  11:      </script>
  12:      <title></title>
  13:  </head>
  14:  <body onload="GetMap();">
  15:      <!--<div id='myMap' style="position:absolute; width:400px; height:400px;"></div>-->
  16:      <div id="myMap" class="map"></div>
  17:  </body>

注意:大多数情况下,在你调用VEMap方法或尝试访问VEMap属性前,你必须调用LoadMap方法。

image

当加载时自定义地图:

当你首次加载时,你还可以定义位置,缩放级别,和地图样式。可以使用VEMap.LoadMap方法来传递位置、缩放级别,地图样式,是否锁住地图,地图模式,是否显示地图模式选项以及使用多少缓冲区。

   1:  <script type="text/javascript">
   2:          function GetMap() {
   3:  //            var map = new VEMap('myMap');
   4:  //            map.LoadMap();
   5:              var map = new VEMap('myMap');
   6:              map.LoadMap(new VELatLong(47.6, -122.33, 0, VEAltitudeMode.RelativeToGround)
                             , 10
                             , VEMapStyle.Road
                             , false
                             , VEMapMode.Mode2D
                             , true
                             , 1);
   7:          }
   8:      </script>

image 

案例:

一个完整的包含显示一个地图所必须的元素的网页。

   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:  <html>
   3:     <head>
   4:        <title></title>
   5:        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   6:   
   7:        <script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
   8:   
   9:        <script type="text/javascript">
  10:           var map = null;
  11:   
  12:           var LA = new VELatLong(34.0540, -118.2370);
  13:   
  14:           var pinPoint = null;
  15:           var pinPixel = null;
  16:                    
  17:           function GetMap()
  18:           {
  19:              map = new VEMap('myMap');
  20:              map.LoadMap(LA, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
  21:   
  22:              AddPin();
  23:           }
  24:   
  25:           function getInfo()
  26:           {
  27:              var info;
  28:   
  29:              if (map.IsBirdseyeAvailable())
  30:              {
  31:                  var be = map.GetBirdseyeScene();
  32:   
  33:                  info  = "ID: "          + be.GetID() + "\n";
  34:                  info += "orientation: " + be.GetOrientation()+ "\n";
  35:                  info += "height: "      + be.GetHeight() + "\n";
  36:                  info += "width: "       + be.GetWidth() + "\n";
  37:   
  38:                  var pixel = be.LatLongToPixel(map.GetCenter(), map.GetZoomLevel());
  39:   
  40:                  info += "LatLongToPixel: " + pixel.x + ", " + pixel.y + "\n";
  41:   
  42:                  // Check to see if the current birdseye view contains the pushpin pixel point.
  43:                  info += "contains pixel " + pinPixel.x + ", " + pinPixel.y + ": " + 
  44:                          be.ContainsPixel(pinPixel.x, pinPixel.y, map.GetZoomLevel()) + "\n";
  45:                  
  46:                  // Check to see if the current view contains the pushpin LatLong.
  47:                  info += "contains latlong " + pinPoint + ": " + be.ContainsLatLong(pinPoint) + "\n";
  48:                  
  49:                  // This method may return null, depending on the selected view and map style.
  50:                  info += "latlong: " + map.PixelToLatLong(pixel);
  51:   
  52:                  alert(info);
  53:              }
  54:              else
  55:              {
  56:                  var center = map.GetCenter();
  57:   
  58:                  info  = "Zoom level:\t" + map.GetZoomLevel() + "\n";
  59:                  info += "Latitude:\t"   + center.Latitude    + "\n";
  60:                  info += "Longitude:\t"  + center.Longitude;
  61:   
  62:                  alert(info);
  63:              }
  64:           }
  65:           
  66:           function AddPin()
  67:           {
  68:              // Add a new pushpin to the center of the map.
  69:              pinPoint = map.GetCenter();
  70:              pinPixel = map.LatLongToPixel(pinPoint);
  71:              map.AddPushpin(pinPoint);
  72:           }
  73:        </script>
  74:     </head>
  75:     <body onload="GetMap();">
  76:        <div id='myMap' style="position:relative; width:400px; height:400px;"></div>
  77:         <input id="btnGetInfo" type="button" value="Get Scene Information" name="getinfo" onclick="getInfo();">
  78:         <br/>
  79:          (zoom out 5 clicks to get latitude/longitude and zoom level)
  80:     </body>
  81:  </html>

image

image