I adapted a formula for calculating bearing from JavaScript into AS3. The
following code calculates bearing from the route begin point to the
endpoint, although it could obviously be adapted to calculate bearing
between any two points on a polyline. I then plug the calculated bearing
into the first parameter of the Attitude object of a flyto method. This
seems to be working to position the camera behind the car. The commented
lines are the original JavaScript formula I began with.
*
private* *function* calculateBearingFromScratch(dir:Directions):Number {
*// Calculate the bearing between two latlng points
*
*var* beginPoint:LatLng = LatLng(dir.getRoute(0).getStep(0).latLng);
*var* endPoint:LatLng = LatLng(dir.getRoute(0).endLatLng);
*var* beginPointLatRad:Number = convertDegToRad(beginPoint.lat());
*var* endPointLatRad:Number = convertDegToRad(endPoint.lat());
*var* tempLon:Number = endPoint.lng() - beginPoint.lng();
*var* dLon:Number = convertDegToRad(tempLon);
*var* y:Number = Math.sin(dLon) * Math.cos(endPointLatRad);
*var* x:Number = Math.cos(beginPointLatRad)* Math.sin(endPointLatRad) -
Math.sin(beginPointLatRad)* Math.cos(endPointLatRad)* Math.cos(dLon);
*var* brngRads:Number = Math.atan2(y, x);
*var* brng:Number = convertRadToDeg(brngRads);
*var* finalBrng:Number = (brng + 360) % 360;
*trace*(*"dlon: "* + dLon);
*trace*(*"bearing: "* + finalBrng);
*return* finalBrng;
*// var dLon = (lon2-lon1).toRad();
// var y = Math.sin(dLon) * Math.cos(lat2);
// var x = Math.cos(lat1)*Math.sin(lat2)
-Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
// var brng = Math.atan2(y, x).toDeg();
*
}
*private* *function* convertDegToRad(degrees:Number):Number {
*return* (degrees * Math.PI/180);
}
*private* *function* convertRadToDeg(radians:Number):Number {
*return* (radians * 180/Math.PI);
}
--
You received this message because you are subscribed to the Google Groups
"Google Maps API For Flash" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-api-for-flash?hl=en.