0

I am displaying in google maps (v3) markers with the position where the photograph was taken and position of the subject and tracing a line to show direction in which photo was taken. I have also calculated the distance and the angle from camera position.

What I am trying now is to show the view from camera with a triangle that opens, lets say 30 degrees toward the subject. Seem to me is a third grade math, but can't figure out after 25 years, I know how to draw the polygon/triangle but not really how to calculate the points at subject's distance about 30 degrees in both directions, of course taking in mind the heading angle.

Almost there...

Used the formula Red answered below to create a function (found I needed to convert: lat1, lon1 and bearing to radians before the math.
I call this function each time a marker changes to calculate again pos1 and pos2 of the triangle and change the paths to redraw it.

Thing now is that triangle shows up but facing all kind of directions but the proper one.

Question now is:

Google return the heading in degrees negative to West and positive to East (-180/180) and seems that the formula need the bearing (heading) in 360 degress? Right now function CameraView(75) display correctly but I am passing the angle as 75 degrees instead of 15 degrees (to have 15 degrees view left, and 15 to right).

function getVertex(brng) {

var R = 6366.707;
var d = parseFloat( getObj('GPSDestDistance').value ) * 1.5  //distance on form field

//position of Camera
var lat1 = deg2rad( markerCam.getPosition().lat() )
var lon1 = deg2rad( markerCam.getPosition().lng() )

var lat2 = Math.asin( Math.sin(lat1) * Math.cos(d/R) + Math.cos(lat1) * Math.sin(d/R) * Math.cos(brng) );
var lon2 = lon1 + Math.atan2( Math.sin(brng) * Math.sin(d/R) * Math.cos(lat1), Math.cos(d/R) - Math.sin(lat1) * Math.sin(lat2) );

lat2 = rad2deg( lat2 )
lon2 = rad2deg( lon2 )

var pos = new google.maps.LatLng(lat2, lon2)
return pos;
}
function CameraView(angle) {
var brng = deg2rad( parseFloat( getObj('GPSDestBearing').value ) );  //get heading from form
if(brng<0){
    //brng = Math.round(-brng / 360 + 180)
    }
var pos1 = markerCam.getPosition();
var pos2 = getVertex(brng - angle);
var pos3 = getVertex(brng + angle);
var paths = [ pos1, pos2, pos3 ];
poly.setPath(paths);
}

function deg2rad(x)     { return x * (Math.PI / 180); }
function rad2deg(x)     { return x * (180 / Math.PI); }
4

2 回答 2

0

更简单的方法:使用谷歌地图几何库

  calculateAngle: function(subjectMarker, cameraMarker, angle) {
    var distance, heading, left, right;

    // Get the heading between two markers
    heading = google.maps.geometry.spherical.computeHeading(subjectMarker.getPosition(), cameraMarker.getPosition());

    // convert heading range from [-180,180] to [0,360]
    heading = ((heading - -180) * 360) / 360;

    // Get the distance between two markers
    distance = google.maps.geometry.spherical.computeDistanceBetween(cameraMarker.getPosition(), subjectMarker.getPosition());

    // Calculate angle
    left = new google.maps.geometry.spherical.computeOffset(cameraMarker.getPosition(), distance / Math.cos(Math.PI * (angle / 2) / 180), heading - (angle / 2));
    right = new google.maps.geometry.spherical.computeOffset(cameraMarker.getPosition(), distance / Math.cos(Math.PI * (angle / 2) / 180), heading + (angle / 2));

    // Return an array of `google.maps.LatLng` objects for use when drawing a polygon.
    return [cameraMarker.getPosition(), left, right];
  }
于 2015-03-19T16:56:25.877 回答
0

从起始位置 (lat1,lon1)(也是弧度)、范围 (d)、地球半径 (R) 和方位角 (brng) 获取端点纬度和经度 (lat 2,lon2)(弧度)的公式)

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

您的起点将是三角形多边形的点之一。其他两个可以使用此代码片段找到,输入您的方位 +/- 15 度(对于 30 度三角形示例)和一个完全取决于您想要三角形的大小的范围。

于 2013-03-26T19:40:57.413 回答