Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem can be solved by taking the following steps.</p> <ul> <li><p>Calculate the distance from points A and B (use the <a href="http://www.movable-type.co.uk/scripts/latlong.html">Haversine formula</a>, which is good enough here, or the more complicated <a href="http://www.movable-type.co.uk/scripts/latlong-vincenty.html">Vincenty formula</a>). To use the formula correctly, Android's microdegrees, which is what getLatitude and getLongitude return, must be converted to radians, using a formula like this:</p> <pre><code> double radians = Math.toRadians((double)microdegrees/1000000); </code></pre></li> <li><p>Calculate the bearing (direction) from points A and B (use the formula on the same page). This will be different from the Pythagorean formula because the earth is round, not flat.</p></li> <li>Then you can choose a new distance and calculate point X given point A and the bearing found in the previous step (see either "Destination point given distance and bearing from start point" on the same page, or <a href="http://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html">Vincenty's direct formula</a>).</li> <li><p>Convert the radians from the generated point into microdegrees using this formula:</p> <pre><code> int microdegrees = (int)(Math.toDegrees(radians)*1000000); </code></pre></li> </ul> <p>Putting it all together, we have the following function, which I place in the public domain:</p> <pre><code> public static int[] getIntermediatePoint( int startLatMicroDeg, int startLonMicroDeg, int endLatMicroDeg, int endLonMicroDeg, double t // How much of the distance to use, from 0 through 1 ){ // Convert microdegrees to radians double alatRad=Math.toRadians((double)startLatMicroDeg/1000000); double alonRad=Math.toRadians((double)startLonMicroDeg/1000000); double blatRad=Math.toRadians((double)endLatMicroDeg/1000000); double blonRad=Math.toRadians((double)endLonMicroDeg/1000000); // Calculate distance in longitude double dlon=blonRad-alonRad; // Calculate common variables double alatRadSin=Math.sin(alatRad); double blatRadSin=Math.sin(blatRad); double alatRadCos=Math.cos(alatRad); double blatRadCos=Math.cos(blatRad); double dlonCos=Math.cos(dlon); // Find distance from A to B double distance=Math.acos(alatRadSin*blatRadSin + alatRadCos*blatRadCos * dlonCos); // Find bearing from A to B double bearing=Math.atan2( Math.sin(dlon) * blatRadCos, alatRadCos*blatRadSin - alatRadSin*blatRadCos*dlonCos); // Find new point double angularDistance=distance*t; double angDistSin=Math.sin(angularDistance); double angDistCos=Math.cos(angularDistance); double xlatRad = Math.asin( alatRadSin*angDistCos + alatRadCos*angDistSin*Math.cos(bearing) ); double xlonRad = alonRad + Math.atan2( Math.sin(bearing)*angDistSin*alatRadCos, angDistCos-alatRadSin*Math.sin(xlatRad)); // Convert radians to microdegrees int xlat=(int)Math.round(Math.toDegrees(xlatRad)*1000000); int xlon=(int)Math.round(Math.toDegrees(xlonRad)*1000000); if(xlat&gt;90000000)xlat=90000000; if(xlat&lt;-90000000)xlat=-90000000; while(xlon&gt;180000000)xlon-=360000000; while(xlon&lt;=-180000000)xlon+=360000000; return new int[]{xlat,xlon}; } </code></pre> <p>And here's how it's used:</p> <pre><code>int ax = oldPoint.getLatitude(); int ay = oldPoint.getLongitude(); int bx = currentPoint.getLatitude(); int by = currentPoint.getLongitude(); long at = oldPoint.getDataRilevamento(); //get time first point long bt = currentPoint.getDataRilevamento(); // get time second point long xt = x.getDate(); // time of point to find // Find relative time from point A to point B double t=(bt==at) ? 0 : ((double)(xt-at))/((double)(bt-at)); // Find new point given the start and end points and the relative time int[] xpos=getIntermediatePoint(ax,ay,bx,by,t); x.setLatitude(xpos[0]); //set the latitude of X x.setLongitude(xpos[1]); // set the longitude of X </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload