i have written java code verifies whether point lies within sector of circle defined radius. both point , circle centre represented in terms of latitude , longitude.
the java code follows-
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; }
now if point within sector trying mark point on sector drawn on map using google api.
but problem if java code return true i.e. point lies within sector still, corresponding sector plotted on google map shows point outside sector.
please me whether did go wrong?
distance @ earth surface not hypotenuse of lat. , long. distances. have find length of great circle arc. @ first section here
javascript excerpt:
var r = 6371000; // metres var φ1 = lat1.toradians(); var φ2 = lat2.toradians(); var Δφ = (lat2-lat1).toradians(); var Δλ = (lon2-lon1).toradians(); var = 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;
and told sector - sector parameters (starting or central angle, sweep angle)?
calculate direction angle (bearing) shown @ mentioned link in bearing section.
Comments
Post a Comment