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,即點位於扇區內,谷歌地圖上繪製的相應扇區顯示該點位於扇區之外。
請問我是否出錯了?