How to compute hypotenuse and bearing

后端 未结 1 901
梦谈多话
梦谈多话 2021-01-27 17:11

I got the below code from @DanS at this link how-to-display-a-map-still-image-file-with-a-moving-current-location

onCurrentPosition(Location current){
    double         


        
相关标签:
1条回答
  • 2021-01-27 17:34

    This is the actual code I'm using, not pseudo code posted previously:

    Location upperLeft = new Location("");
    upperLeft.setLatitude(41.866514127810355);
    upperLeft.setLongitude(-87.6720142364502);
    Location lowerRight = new Location("");
    lowerRight.setLatitude(41.83397145565242);
    lowerRight.setLongitude(-87.62824058532715);
    Location current = new Location("");
    current.setLatitude(41.850033);
    current.setLongitude(-87.65005229999997);
    double hypotenuse = upperLeft.distanceTo(current);
    double bearing = upperLeft.bearingTo(current);
    double currentDistanceX = Math.cos(bearing * Math.PI / 180.0) * hypotenuse;
    //                     "percentage to mark the position"
    double totalHypotenuse = upperLeft.distanceTo(lowerRight);
    double totalDistanceX = totalHypotenuse * Math.cos(upperLeft.bearingTo(lowerRight) * Math.PI / 180.0);
    double currentPixelX = currentDistanceX / totalDistanceX * 512;
    
    System.out.println(currentPixelX); // 259.3345493341548
    

    Your calculated answer looks a bit off. To calculate Y change copy all the X marked calculations and variables to use Math.sin() instead of Math.cos().

    0 讨论(0)
提交回复
热议问题