2012-04-05 177 views
0

首先給那些想評論我的低接受率的人...即時新的,給我一個休息。多邊形的洞

我說從本網站這個多邊形代碼: 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> 
+0

你是什麼意思的「路」?你能提供一個演示鏈接嗎?或者至少有一個截圖? – 2012-04-05 12:41:18

+0

CYQX-EINN之間的距離是1715nm。當我輸入CYQX @ 1715NM時,它的長度大約爲200英里。我確實考慮了轉換。當你檢查像CYQX-CYJT(159nm)那樣的較短距離時,距離它約3英里。問題是半徑的公式存在缺陷。 – 2012-04-05 13:00:50

+0

因此,你在規約里程中使用地球半徑並不相關? 1715NM是1973SM; 159NM是182SM。如果你正在繪製1715年的規約里程,它將會在距離1715NM約200英里的地方。 – 2012-04-05 13:06:17

回答

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半徑的圓(表示爲3176180米)。其他圈已增加在500,1000,1500,2000和2500NM的半徑。

我也在兩點之間添加了測地線。如預期的那樣,這以正確的角度交叉圓圈,這是對圓圈計算精度的檢查。

+0

該孔需要將圓圈以與包含邊界相反的方向繪製。將方向'-1'改爲'1'。一旦你到達世界的另一邊,奇怪的事情就會發生。實際上(用'1'),你在你的圓圈上畫着陰影*從圓形的一側穿過它到另一側的盒子邊界。如果距離超過地球四分之一的距離,您可能需要改變線的使用順序[但我沒有證實]。 – 2012-04-06 15:57:32

+0

這是完全有道理的。我試圖看到結果。我灰色的問題取決於緯度和半徑大小。我可以從PAOM得到11000nm,而MMSD只能得到6500nm;在它給我問題之前。我滿足於你給我的東西,非常感謝你。 – 2012-04-07 11:49:00