2016-01-24 61 views
0

我已經編寫了一個Java代碼來驗證點是否位於圓定義的半徑的扇區內。點和圓心均以緯度和經度表示。如何驗證點是否位於由地理座標形成的扇區內

的Java代碼如下 -

public boolean isPointWithin(float centreLat,float centreLong,float pointLat,float pointLong,int maxDistance) 
    {        
     float adjFactor=0.0f; 
     double angleRange,bisectorIVector,bisectorJVector,IVector,JVector,VectorLength,dotProduct,EnodeBAngle, 
     latScale=0.0,longScale=0.0,distance,lower,upper,count,adjustmentFactor=0.0,jVector,dist1,dist2; 
     dist1=Math.pow(centreLat-pointLat, 2)+Math.pow(centreLong-pointLong, 2); 
     dist2=Math.pow(centreLat-pointLat, 2)+Math.pow(360-(Math.abs(centreLong)+Math.abs(pointLong)), 2); 
     if(dist1>dist2) 
     { 
      adjFactor=360-(Math.abs(pointLong)+Math.abs(centreLong)); 
      if(centreLong>0) 
       pointLong=(centreLong+adjFactor); 
      else 
       pointLong=(centreLong-adjFactor); 
     } 
     latScale=(111132.954-(559.822*Math.cos(2*Math.toRadians(pointLat)))+1.175*Math.cos(4*Math.toRadians(pointLat)))/1000; 

     angleRange=Math.toRadians(sectorAngle); 

     bisectorIVector=maxDistance*Precision.round(Math.sin(Math.toRadians(sectorDirection)),8); 
     bisectorJVector=maxDistance*Precision.round(Math.cos(Math.toRadians(sectorDirection)),8); 

     IVector=pointLong-centreLong; 
     JVector=pointLat-centreLat; 

     if(pointLong<centreLong) 
     { 
      lower=pointLong; 
      upper=centreLong; 
     } 
     else 
     { 
      upper=pointLong; 
      lower=centreLong; 
     } 
     count=lower; 
     while(Math.ceil(count)<Math.floor(upper)) 
     { 
      jVector=((JVector/IVector)*(count-centreLong))+centreLat; 
      longScale+=Math.cos(Math.toRadians(jVector)); 
      count++; 
     } 
     longScale*=latScale; 
     if((upper-lower)>Math.floor((upper-lower))) 
     { 
      jVector=((JVector/IVector)*(upper-centreLong))+centreLat; 
      adjustmentFactor=Math.cos(Math.toRadians(jVector))*latScale*((upper-lower)-Math.floor((upper-lower))); 
     } 
     longScale+=adjustmentFactor; 


     distance=Math.sqrt(Math.pow(longScale,2) + Math.pow(JVector*latScale,2)); 
     VectorLength=Math.sqrt(Math.pow(IVector,2) + Math.pow(JVector,2)); 
     if(VectorLength==0.0) 
      return true; 

     dotProduct=bisectorIVector*IVector+bisectorJVector*JVector; 
     EnodeBAngle=Precision.round(Math.acos(dotProduct/(maxDistance*VectorLength)),8); 

     if(EnodeBAngle<=Precision.round(angleRange/2,8) && distance<=maxDistance) 
      return true; 
     else 
      return false; 
    } 

現在如果這個點我想,以紀念上使用谷歌地圖API繪製界點的部門。

但問題是,即使我的Java代碼返回true,即點位於扇區內,谷歌地圖上繪製的相應扇區顯示該點位於扇區之外。

請問我是否出錯了?

回答

0

地球表面的距離並不是緯度的斜坡。和長。距離。你必須找到大圓弧的長度。看第一部分here

的JavaScript摘錄:

var R = 6371000; // metres 
var φ1 = lat1.toRadians(); 
var φ2 = lat2.toRadians(); 
var Δφ = (lat2-lat1).toRadians(); 
var Δλ = (lon2-lon1).toRadians(); 

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + 
     Math.cos(φ1) * Math.cos(φ2) * 
     Math.sin(Δλ/2) * Math.sin(Δλ/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 

var d = R * c; 

你告訴部門 - 但如果是扇區參數(啓動或圓心角,後掠角)?
也許你會計算方向角(方位角),如上面的方位部分提到的鏈接所示。

相關問題