多边形的孔

时间:2012-04-05 10:42:47

标签: google-maps-api-3 polygons

首先请那些想对我的低接受率评论发表意见的人......我是新的,给我一个休息时间。

我从这个网站添加了这个多边形代码: http://www.geocodezip.com/v3_polygon_example_donut.html

但是存在一个问题,半径不准确。因此,如果我测量两个城市之间的距离,那么画出这个圆圈就会离开,而圆圈越大就会变得更糟。

有什么想法吗?

<script type="text/javascript"> 
function drawCircle(point, radius, dir) { 
var d2r = Math.PI / 180;   // degrees to radians 
var r2d = 180 / Math.PI;   // radians to degrees 
var earthsradius = 3959; // 3959 is the radius of the earth in SM

   var points = 1000; 

   // find the raidus in lat/lon 
   var rlat = (radius / earthsradius) * r2d; 
   var rlng = rlat / Math.cos(point.lat() * d2r); 


   var extp = new Array(); 
   if (dir==1)  {var start=0;var end=points+1} // one extra here makes sure we connect the
   else     {var start=points+1;var end=0}
   for (var i=start; (dir==1 ? i < end : i > end); i=i+dir)  
   { 
  var theta = Math.PI * (i / (points/2)); 
  ey = point.lng() + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
  ex = point.lat() + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
  extp.push(new google.maps.LatLng(ex, ey)); 
  bounds.extend(extp[extp.length-1]);
   } 
   // alert(extp.length);
   return extp;
   }

    var map = null;
    var bounds = null;

    function initialize() {
  var myOptions = {
    zoom: 10,
    center: new google.maps.LatLng(29.10860062, -95.46209717),
    mapTypeControl: true,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    navigationControl: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  }
  map = new google.maps.Map(document.getElementById("map_canvas"),
                            myOptions);

  bounds = new google.maps.LatLngBounds();

  var donut = new google.maps.Polygon({
             paths: [triangleCoords = [
                        new google.maps.LatLng(-87, 120), 
                        new google.maps.LatLng(-87, -87), 
                        new google.maps.LatLng(-87, 0)],
                     drawCircle(new google.maps.LatLng(29.10860062, -95.46209717), 2000, -1)],";

             strokeColor: "#000000",
             strokeOpacity: 0.6,
             strokeWeight: 2,
             fillColor: "#999999",
             fillOpacity: 0.6
 });
 donut.setMap(map);

 map.fitBounds(bounds);


</script> 

1 个答案:

答案 0 :(得分:3)

使用http://www.movable-type.co.uk/scripts/latlong.html#destPoint

处的“轴承点”计算可以找到更好的圆绘程例程

θ是轴承(以弧度表示,从北向顺时针方向); d / R是角距离(以弧度表示),其中d是行进距离,R是地球半径

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
              Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                     Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

将所有内容转换为弧度并确保d和R都以相同的单位表示,我们得到一个像这样的圆绘图程序

function drawCircle(point, radius, dir, addtoBounds) { 
var d2r = Math.PI / 180;   // degrees to radians 
var r2d = 180 / Math.PI;   // radians to degrees 
var earthsradius = 6371000; // 3959 is the radius of the earth in SM

   var points = 1000; 

   // find the raidus in lat/lon 
   var rlat = (radius / earthsradius) * r2d; 
   var rlng = rlat / Math.cos(point.lat() * d2r); 


   var extp = new Array(); 
   if (dir==1)  {var start=0;var end=points+1} // one extra here makes sure we connect the
   else     {var start=points+1;var end=0}
   for (var i=start; (dir==1 ? i < end : i > end); i=i+dir)  
   { 
      var theta = Math.PI * (i / (points/2)); 
    var lat1=point.lat()*d2r;
    var lon1=point.lng()*d2r;
    var d=radius;
    var R=earthsradius;

    var ex = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
              Math.cos(lat1)*Math.sin(d/R)*Math.cos(theta) );
    var ey = lon1 + Math.atan2(Math.sin(theta)*Math.sin(d/R)*Math.cos(lat1), 
                     Math.cos(d/R)-Math.sin(lat1)*Math.sin(ex));
      extp.push(new google.maps.LatLng(ex*r2d, ey*r2d)); 
      if (addtoBounds) bounds.extend(extp[extp.length-1]);
   } 
   // alert(extp.length);
   return extp;
   }

我在http://www.acleach.me.uk/gmaps/v3/mapsearch.htm有一个示例,其中心位于CYQX,EINN处有另一个标记,半径为1715NM(表示为3176180m)。其他圆圈的半径为500,1000,1500,2000和2500NM。

我还在两点之间添加了一条测地线。这与预期的直角交叉圆圈,这是对圆计算精度的检查。