bing map 画圆,其他百度地图、谷歌地图、搜搜地图、搜狗地图等稍微修改即可

  最近需要在bing 地图上画圆,因为bing 地图没有直接提供画圆的API,画起来比较麻烦,经过在网上的查找终于找到了画圆的方法。下面的代码是我整理过的代码,其中画圆函数来至网络。因为画圆比较困难,所以分享给大家,让大家也少走弯路。

  如果你使用的是其他地图,例如百度地图、谷歌地图、搜搜地图、搜狗地图等,适当修改提供的api接口即可实现。如果需要进行地图的定制开发,请发送站内信给我。

  上代码,呵呵。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>bing map画圆</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript"
            src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=zh-cn"></script>
        <script type="text/javascript">
         var map     = null;
         function GetMap()
         {
            map = new VEMap('myMap');
            map.LoadMap();
            map.SetZoomLevel(14);
            var latLon=new VELatLong(34.79248399030799,113.69712352752688);
            //设置地图中心点
            map.SetCenter(latLon);
            map.SetMouseWheelZoomToCenter(false);
            //画圆,以地图中心latLon为圆心,半径为1km 画圆
            drawCircle(latLon,1);
         }
         //画圆函数 origin 圆心,radius 半径 单位km
        function drawCircle(origin, radius) {
            var earthRadius = 6371;
            //圆心纬度
            var lat = (origin.Latitude * Math.PI) / 180;
            //圆心经度
            var lon = (origin.Longitude * Math.PI) / 180;
            var d = parseFloat(radius) / earthRadius;
            var points = new Array();
            for (i = 0; i <= 360; i++) {
                var point = new VELatLong(0, 0);
                var bearing = i * Math.PI / 180; 
                point.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
                point.Longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(point.Latitude))) * 180) / Math.PI;
                point.Latitude = (point.Latitude * 180) / Math.PI;
                points.push(point);
            }
            var circle = new VEShape(VEShapeType.Polygon, points);
            //设置无边缘
            circle.SetLineColor(new VEColor(255, 0, 0, 0));
            circle.HideIcon();
            map.AddShape(circle);
        }
      </script>
    </head>
    <body onload="GetMap();">
        <div id='myMap'
            style="position: relative; width: 400px; height: 400px;"></div>
    </body>
</html>

 

posted @ 2013-03-19 20:53  沙琪玛  阅读(3028)  评论(2编辑  收藏  举报