必应地图中国API入门讲座之六:添加多边形
作为必应地图的三项最基本应用之一,添加多边形的作用也非常广泛。例如,在物流、导航、监控领域,设置一个监控区域,或者观察目标状态的变化。如果我们学习了上两讲的内容,那么这一讲的内容应该内度并不大。 首先我们要初始化一个多边形: var shape = new VEShape(VEShapeType.Polygon, [new VELatLong(lat,lon-0.15), new VELatLong(lat+0.1,lon-0.05), new VELatLong(lat+0.1,lon+0.05), new VELatLong(lat,lon+0.15), new VELatLong(lat-0.1,lon+0.05), new VELatLong(lat-0.1,lon-0.05)]); 此处lat和lon为当前地图中心的纬度和经度。 可以看到,多边形的初始化参数为VEShapeType.Polygon,至少三个点确定一个多边形。 我们还可以通过shape类的各种方法来指定多边形的属性:线条粗细、颜色,填充颜色,多边形描述信息等等。这一讲我们增加一个新的内容,即多边形的描述可以支持html语法。 var infobox = "<div style='width:309px;'>You can add html codes into the info box or change the style of the info box. You can design and show any info box you want!<br>" +"<a href='http://msdn2.microsoft.com/en-us/library/bb412553.aspx' target='_blank'>Click here to find out more.</a><br></div>" +"<embed src='http://images.soapbox.msn.com/flash/soapbox1_1.swf' quality='high' width='309px' height='272px' wmode='transparent' type='application/x-shockwave-flash' pluginspage='http://macromedia.com/go/getflashplayer' flashvars='c=v&v=a4b53303-d58c-450e-a01d-069c9bcb5fe9' ></embed><br /><a href='http://soapbox.msn.com/video.aspx?vid=a4b53303-d58c-450e-a01d-069c9bcb5fe9' target='_blank' title='Virtual Earth - Bird's eye view and 3D'>Video: Virtual Earth - Bird's eye view and 3D</a>"; 上面的代码看似很长,其实就是定义了一个包含html语法的字符串。然后通过调用shape.SetDescription(infobox)设置多边形的描述内容。
下面让我们看一下添加多边形函数的完整实现: function AddPolygon() { var ll = map.GetCenter(); var lat = ll.Latitude; var lon = ll.Longitude; var shape = new VEShape(VEShapeType.Polygon, [new VELatLong(lat,lon-0.15), new VELatLong(lat+0.1,lon-0.05), new VELatLong(lat+0.1,lon+0.05), new VELatLong(lat,lon+0.15), new VELatLong(lat-0.1,lon+0.05), new VELatLong(lat-0.1,lon-0.05)]); shape.SetTitle("<h2>Custom Pin</h2>"); shape.SetDescription(infobox); //Add the shape the the map map.AddShape(shape); } 并且在html body中增加一个控制链接: <div><a href='#' onclick='AddPolygon();'>增加多边形</a></div> 记住,别忘了在代码中定义infobox字符串。 |