| Ricky's profileRicky's Bing Maps BlogBlogSkyDrive | Help |
|
|
10/25/2008 VE Imagery Service and Custom IconsCurrently the VE Imagery service has the ability to display 26 different icons on the map. Oddly enough the default pushpin icon from the JavaScript version of Virtual Earth is not one of these icons. Unfortunately there is currently no built in method to display custom icons on the map image. There is also currently a limitation of 10 pushpins being passed to the imagery service. This article shows how to add any number of custom icons to the map image in the correct location. The default pushpin icon from the JavaScript version of Virtual Earth will be used as the custom icon. The high level steps involved to accomplish this are as follows:
Calculate the Best Map ViewThe best map view is the zoom level and center point of a map image that contains all locations with the closest zoom level. The best map view will be stored using a structure. /// <summary> /// Structure to define a BestView object which defines a map view (centerpoint and zoom level) /// </summary> struct BestView Listing 1Structure to define a BestView object The following method calculates the best map view for an array of locations. This results in a map image similar to what is displayed if the VEMap.SetMapView function is used in the JavaScript version of Virtual Earth. /// <summary> /// Calculates best map view for an array of points. Similar to VEMap.SetMapView method /// </summary> /// <param name="points">Array of Location objects</param> /// <returns></returns> private BestView CalculateMapView(Location[] points) Listing 2 Method to calculate the best map view for an array of Locations The method that calculates the best map view uses a method called HaversineDistance. This method calculates the distance in kilometers between two coordinates using the Haversine formula. The code for this method is as follows: /// <summary> /// Calculate the distance in kilometers between two coordinates /// </summary> /// <param name="lat1"></param> /// <param name="lon1"></param> /// <param name="lat2"></param> /// <param name="lon2"></param> /// <returns></returns> private double HaversineDistance(double lat1, double lon1, double lat2, double lon2) Listing 3 Haversine distance method Request Map Image from the Imagery ServiceBy specifying a specific zoom level and center point for a map, this ensures that we have at least one point of reference between pixel coordinates and a latitude/longitude coordinate. The following method retrieves an image URL of a map for a specific map view. The image is then retrieves using a stream. /// <summary> /// Gets map from VE Imagery Web Service /// </summary> /// <param name="view">Map view</param> /// <returns></returns> private Image GetMapImage(BestView view) Listing 4 Method to retrieve map image from the VE Web Services for best map view Calculate Pixel Coordinate for a LocationVirtual Earth tiles are positioned using quad key tile positioning. In Virtual Earth a map tile is 256 pixels by 256 pixels. For zoom level one, there are 4 map tiles displayed, making the total map width and height to display the whole world 512 pixels by 512 pixels. If the zoom level is 2, there are 16 map tiles being displayed, making the total map width and height to display the whole world 1024 pixels by 1024 pixels. Additional information the Virtual Earth tiling system can be found here: http://msdn.microsoft.com/en-us/library/bb259689.aspx With the Virtual Earth tiling system in mind, all pixel coordinates for a set of latitude/longitude coordinates can be calculated. Using the center point of the map image the top left hand corner of the map image that is returned by the Virtual Earth imagery service can be calculated as a pixel location on a map that displays the whole world. Relative pixel coordinates can be then calculated for all location latitude/longitude coordinates. The following method takes in a Location object and a BestView object and returns a Point object which refers to the pixel location of a latitude/longitude coordinate on the map image. /// <summary> /// Converts a latlitude longitude coordinate to a pixel coordinate of a map based on the map view /// </summary> /// <param name="latlong"></param> /// <param name="view"></param> /// <returns></returns> private Point LatLongToPixel(Location latlong, BestView view) Listing 5 LatLong to Pixel coordinate method Putting it all together In order for these methods to work in a WinForm application the following references will have to be made: using System.Drawing.Design; Listing 6 Required References A reference to the Virtual Earth Imagery Service and the Virtual Earth token service will also be required. The following global variables will also be needed: string clientToken; Listing 7 Global Variables The following method will calculate the best map view for an array of locations, retrieve a map image from the imagery service, add custom icons to the map image and then display the map image in a picture box. /// <summary> /// Generate map image with custom icons /// </summary> /// <param name="points">Array of Location objects</param> private void GenerateImage(Location[] points) Listing 8 Method to Generate map image with custom icons The following method can be used to add the custom icons to a map image: /// <summary> /// Adds custom icon image to map image /// </summary> /// <param name="view"></param> /// <param name="map"></param> /// <param name="points"></param> private void AddCustomIcon(BestView view, Image map, Location[] points) Listing 9 Method to add custom icons to a map image This method can add a custom icon image and specify an image offset similar to the VECustomIconSpecification. Here is an example of five locations plotted onto a map using Virtual Earth: Figure 2 Points plotted using the JavaScript version of Virtual Earth Here is an example of the same points being plotted onto a map image that is generated from the Virtual Earth Imagery Service using the methods in this article. Figure 3 Custom icons on a VE Imagery Service image ConclusionCombine these methods with a valid client token and you will be able to add your custom icons to a map image. Complete sample code can be found here: This article was written by Richard Brundritt. Richard works for Infusion Development. 10/19/2008 Determine the Points of Intersections of Two Overlapping PolygonsSimilar to the method described in the post "Determine if Two Polygons Overlap" (http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!475.entry), we will have to iterate through all the line segments that make up one polygon and see if they overlap with any of the line segments that make up the second polygon. The difference will be that we will create an array of all the points of intersections. The following function takes in two arrays of coordinates that make up two polygons. An array of VELatLong objects will be returned. //poly1 and poly2 are arrays of VELatlongs that represent polygons function PolygonIntersections(poly1, poly2) The code for the function "SimplePolylineIntersection" can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!474.entry Here is a screen shot of this function being used and the points being plotted on the map: Determine if Two Polygons overlapIn order to determine if two polygons overlap we need to iterate through the line segments that make up each polygon and determine if any of the line segments intersect. Once we find a single point where the lines intersect we can then stop the iterations. The following code can be used to determine if two polygons overlap: //poly1 and poly2 are arrays of VELatlongs that represent polygons function ArePolygonsOverlapped(poly1, poly2) The code for the function "SimplePolylineIntersection" can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!474.entry Approximate Points of Intersection of Two Line SegmentsThe theory for calculating the point of intersection of two line segments is similar to the theory described in the post "Approximate Point of Intersection of Two Planes" found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!473.entry The difference is that in order to be a point of intersection between two line segments, the calculated point must be within both of the bounding boxes in which the line segments are contained in. When can thus use the following two functions to determine the poin tof intersection of two polylines. function SimplePolylineIntersection(latlong1,latlong2,latlong3,latlong4)A more complex method for calculating the point of intersection of two line segments in 3D space can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!439.entry Approximate Point of Intersection of two planesThe point of intersection of two planes in 3D space can be calculated using spherical mathematics, as described here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!438.entry Taking this route requires a significant number of calculation. If we wanted to calculate the points of intersection of a large number of planes this can cause performance issues. The following is a method that can be used to calculate a 2D approximation for the point of intersection. Theory A 2D plane, commonly known as a line, can be defined using a formula of the following format: Ax + By = C A = y2-y1 If we were to have two planes defined by the following equations: A1x + B1y = C1 We can then calculate the determinate of these two equations using the following formula: determinate = A1*B2 - A2*B1 If the determinate is 0, this would indicate that the planes are parallel to each other. If the determinate is 1 then this would indicate that the planes are perpendicular to each other. Now that we have the determinate of these equations we can now solve for the x and y components of the point of intersection. x = (B2*C1 - B1*C2)/determinate Application The following code takes in four coordinates, where two coordinates represents the endpoints of a line segment. This code will return either a VELatLong representing the point of intersection or will return null which would indicate that the planes are parallel. function SimplePlaneIntersection(latlong1,latlong2,latlong3,latlong4) 10/14/2008 Draw polyline across international datelineBy default Virtual Earth does not draw polylines across the international dateline (-180/180 degrees longitude) in 2D mode. The following code is an example of how to over come this issue. This code draws a polyline from Sydney Australia to Los Angeles California, which is a common airplane route. What this code does is determine the point of intersection of the path with the international date line. It then creates two polylines to represent this path. The algorithms use din this code are documented in the VE Math section of this blog. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" Calculate midpoint of PolylineSometimes it might be important to calculate the midpoint along a polyline. this can be done by first calculating the total length of a polyline and then to iterate through the points of the polyline, calculating the distance between points until we find the points where the midpoint falls between. From here we can calculate the coordinate of the midpoint. The following functions can calculate the midpoint coordinate of a polyline. function PolylineCentroid(points) Information on the haversineDistance method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!317.entry Information on the calculateBearing method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!393.entry Information on the calculateCoord method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!400.entry Gravitational Acceleration at a given latitude and altitudeTheory The gravitational acceleration that an object experiences decreases the further it gets from the center of the Earth. The gravitational acceleration at sea level at any point on the Earth’s surface can be calculated using the International Gravity formula: When at sea level the gravitational acceleration only changes with respect to the latitude and not the longitude. The Earth is not a smooth surface and as such we need to be able to calculate this acceleration at altitudes with respect to sea level. We can modify the previous formula so that an altitude (h) in meters can be used to calculate the correct gravitational acceleration. Application This function will take in a latitude and an altitude in meters. It will return a number representing the gravitational accelerations whose units are in function gravitationalAcceleration(latitude, altitude) Listing 1 Gravitational Acceleration function TriangulationTheory Triangulation is a common method for finding a coordinate based on the direction the point in question is in relative to two other points and the distance between these two other points. Triangulation is commonly used in surveying, navigation, wireless communications, astronomy, and just about anywhere it is necessary to calculate the location of an object from a distance. Take for example two points A and B, each having a heading pointing towards some distant point C. Using triangulation we can calculate the coordinate of C. As you can see in the following figure this creates a triangle. Figure 1 Triangulation Using the law of sines we can make the following connection between the three points. Using the formulas that have already been derived in this article we can calculate the distance between point A and B, we can also calculate the bearing from A to B. Once we know this we can calculate the angles From here we can calculate the distances AC and BC using the following formulas: We only need to calculate one of the distances. Once we have that we can use the distance, and the point is based at either A of B, and the associated heading to calculate the coordinate of C. Application This function for calculating the coordinate of a point using triangulation will take in two VELatLong objects and two bearings that are between 0 and 180 degrees. This function will return a VELatLong object for the point in which the provided information triangulates to. When calculating the angle between two headings we are only interested in the inner angle because we are working with a triangle all the angles will be under 180˚. function triangulate(latlongA,latlongB,bearingAC,bearingBC) Listing 1 Triangulation Function Information on the calculateBearing method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!393.entry Angle between two connected polylinesTheory Often it will be useful to know the angle between to polylines. By using the connecting point as our origin and calculating the bearings of the two polylines we can calculate the difference in the bearings to find out the angle between the polylines. There are two angles that we can calculate, the inner angle and the outer angle. The inner angle is less than 180˚. Figure 1 Angle between polylines We can calculate the angle using the following formula: Application A line segment consists to two end points. Since we have two line segments that are connected we have three points. Since there are two possible angles that can be calculated we will make the function so that the type of angle to be returned can be specified. The two angle types that can be used in this function is “inner” and “outer”. This function will return a number between 0 and 360 which will be the angle between the two polylines. function angleBetweenPoints(latlongA,latlongB,latlongC,angleType) Listing 1 Calculate angle between three points We’ve broken the function to calculate the angle between three points up into two parts. By doing this we increase the usability of our code. This second function allows us to calculate the angle between two headings. This function will take in two headings between 0 and 360 degrees and will take in the type of angle that should be returned. A number representing the angle in degrees will be returned, function angleBetweenHeadings(headingBA,headingBC,angleType) Listing 2 Angle between two headings Information on the calculateBearing method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!393.entry Point of intersection of two PolylinesTheory The point of intersection is a location where two line segments overlap. We can use the function that calculates the intersection of two planes to find the two possible points of intersections. Once we have these two points of intersections we can then calculate the length of the two polylines and the length from each vertex of the polylines to a point of intersection. The sum of the lengths of the vertices should be the length of the polyline in which the vertices belong to if the point of intersection is on the polyline. If the first point of intersection is not on one of the polylines we can then do the same calculations for the second possible point of intersection. Application This function will take in the four VELatLongs that define the end points of the two polyline line segments. We will first calculate the two possible points of intersections. We then will calculate the lengths of the two polylines and the distances from the vertices to the first point of intersection. We will then take the length of each polyline and subtract it by the lengths of its vertices to the point of intersection. In theory this should be 0 if the point of intersection is on the line. Since there is a 1% accuracy we will then check to see if the absolute value of this calculation is within 1% of the length of the longest polyline. If the point is not on one of the polylines then the calculation is repeated for the second point of intersection. This function will either return a VELatLong object for -1 if the lines do not intersect. function pointOfIntersection(latlong1,latlong2,latlong3,latlong4) Listing 1 Point of Intersection between two Polylines Information on the intersectionOfPlanes method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!438.entry Points of Intersection of two PlanesTheory We can think of each polyline as a line segment that represents a plane defined by the end points of the line segment and the center of the Earth. The point of intersection would be the point where two planes intersects which would create a line that passes through the center of the Earth, thus representing a point on the surface of the Earth. Figure 1 Intersection of two planes defined by line segments We will look at the solution when Cartesian coordinates are used. A line segment can be defined by two points P1 and P2. Each point having an x, y and z component. We can define a plane that contains P1, P2 and the center of the Earth (P0) taking the dot product of P0 and the cross product of P1 and P2 which will equal zero. We can evaluate this as follows: We can represent a second line segment the same way which consists of points P3, and P4. We can then solve for x and Y in terms of Z as follows: The point of intersection with this line and the sphere of radius r has z such that the distance from the center of the Earth is r. Thus; From this we can solve for z. Knowing z we can find x and y, however the equations are starting to get fairly large. We can significantly reduce the number of calculations that need to performed to solve x, y and z by first solving sub components. For instance, let us define the following subcomponents. We can then write simplified versions of x, y, and z: We could then convert this to work directly with spherical coordinates; however the equations grow in size fairly quickly. It is better to convert the coordinates to Cartesian coordinates and then perform our calculations and then convert the points back into spherical coordinates. Since the Earth is Spherical there will be two points on the surface of the Earth where these planes will intersect. This second point is the inverse coordinate. Application To calculate the intersection of two planes we have to define the planes with line segments. A line segment will be defined by two VELatLong objects. Since we want to find to intersection of two planes we need to pass four VELatLong objects into our function to represent the two line segments. We then have to convert these VELatLong objects into Cartesian coordinates. From there we can calculate two vectors that represent the two planes. We can then calculate the unit vectors of each plane and check that the planes are not equal. If the planes are equal we will return -1. If the planes are not equal we can calculate the vector director. The unit vector of the vector director is the first point of intersection. We can take the first intersection point and calculate its inverse coordinate to calculate the second point of intersection. We then have to convert our x, y, and z parameters into a VELatLong objects. We can then return an array containing these two VELatLongs. Note that this algorithm will calculate the point of intersections to within a distance of 1% of the largest polyline length used to define a plane. function intersectionOfPlanes(latlong1,latlong2,latlong3,latlong4) Listing 1 Intersection Point of two Planes Information on the convertSphericalToCartesian and convertCartesianToSpherical methods can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!280.entry Information on the crossProduct method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!365.entry Information on the unitVector method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!374.entry Information on the inverseCartesian method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!342.entry Calculating the Midpoint of a Line SegmentTheory The midpoint of a line segment is the point on the line segment that is equidistance from each end point. By using functions which we have already created we can easily calculate the midpoint of a line segment. The first step is to find the length of the line segment. This can be found be sending the end points into the Haversine formula. Our mid point is half this distance away from either of our end points. We then have to calculate the bearing from one end point to the other. Which every end point we choose as the origin of our bearing is the point we will pass into the function that calculates a coordinate based on a point of origin, a bearing and a distance. Application To calculate the midpoint of a line segment we can use the Haversine function to find the distance between the end points. We can then calculate the bearing between the points. Once we have the distance to the midpoint and the bearing we can then calculate the midpoint coordinate. This function will take in two VELatLong objects and will return one VELatLong object. function midpoint(latlong1, latlong2) Listing 1 Midpoint between two coordinates Calculate destination coordinateTheory Sometimes it’s useful to be able to calculate the coordinates of a destination based off a point of origin, a bearing (β) and a distance. One popular activity that uses this is orienteering. Orienteering is a competition which is a timed race where participants use a map, and a compass to navigate between check points. At each check point the participant will receive the bearing and distance to the next check point based on the current location of the check point. The distance that is used is the arc length (L) between the two points. Knowing this we can derive the central angle (δ) between the two points based at the center of the Earth. Using the central angle and trigonometry we can calculate the latitude of the destination point using the following formula: Now that we know the latitude of the destination point we can calculate the longitude by using the following formula:
Application The function to calculate the destination coordinate will take in three parameters; a point of origin, a bearing, and a distance. With these parameters we can calculate the latitude and longitude coordinates of the destination point. This function returns a VELatLong object. var earthRadius = 6367; //radius in km Listing 1 Function to calculate a destination coordinate Calculating BearingTheory Bearing is the direction in which an object is pointing or traveling. This is also commonly known as a heading. In Virtual Earth polygons and polylines consist of an array of coordinates that represent endpoints of line segments. It’s useful to think of these line segments as vectors. Each vector has a length and a bearing. Taking this into consideration will allow us to come up with some very useful algorithms. Virtual Earth uses bearings in the 3D map mode in the form of the heading which is the direction in which the camera is pointing. This heading uses 0 degrees as true North, 90 degrees as East, 180 degrees as South, and 270 degrees as West. These same bearings can be used in 2D map mode when representing the line segments of a polygon or polyline as vectors. This calculation requires us to use complex variable calculations to calculate the arctangent of a point in space. This calculation is often done using the atan2 function. The reason for this is that it can handle situations when the denominator is equal to 0. This complex function has the following complex argument: For y = 0 Where φ is an angle in the range The sgn function, known as the sign function and signum function, is another complex function that extracts the sign of a real number. This function has the following characteristics: The following formula can be used to calculate the bearing between two coordinates. Note that this formula takes in latitude and longitudes as radian values and returns the bearing as a radian value in the following range: We can convert this bearing to degrees in the range [0,360] by using the following formula: The reason for adding 360 to the previous formula, then calculate the remainder when divided by 360, is to ensure that the bearing is a positive angle between 0 and 360. Application The function we are going to create will take in two a VELatLong objects and return the bearing in degrees. We will have to take the latitudes and longitudes of each VELatLong and convert them to their radian value. This will be done using the DegtoRad function we created earlier. We will then calculate the two points we need to put into the arctangent function. We will then calculate the bearing in degrees. function calculateBearing(latlong1,latlong2)
Listing 1 Bearing calculation function Unit Vectors and Vector LengthsTheory A unit vector is vector that has a length of one. Unit vectors are used to indicate directions. Any vector can be expressed as a product of a magnitude and a direction. The magnitude of a vector is its length. The following formula can be used to calculate the magnitude of a vector The unit vector of Application The following algorithm calculates the length of vector. This algorithm takes in a Cartesian coordinate and returns a float representing the magnitude of the Cartesian coordinate. function vectorLength(p1) Listing 1 Vector Length The following algorithm calculates the unit of vector a Cartesian coordinate. This algorithm takes in a Cartesian coordinate and returns a Cartesian coordinate representing the unit vector. function unitVector(p1) Listing 2 Unit Vector Cross and Dot productsTheory When working with spherical coordinates it is often useful to be able to perform cross and dot product calculations. The cross product of two Cartesian coordinates, Where The dot product of two Cartesian coordinates, Application The following algorithm calculates the cross product of two Cartesian coordinates. This algorithm takes in two Cartesian coordinates and returns a Cartesian coordinate representing the cross product vector. function crossProduct(p1,p2) Listing 1 Cross Product function The following algorithm calculates the dot product of two Cartesian coordinates. This algorithm takes in two Cartesian coordinates and returns a float representing the dot product. function dotProduct(p1,p2) Listing 2 Dot Production function Calculate Inverse CoordinateTheory The inverse of a spherical coordinate can be calculated by taking the inverse of the latitude (θ) and offsetting the longitude (Φ) by 180˚. The following formulas can be used to calculate these values. The sgn function, known as the sign function and signum function, is a complex function that extracts the sign of a real number. This function has the following characteristics: The inverse of a Cartesian coordinate can be calculated by taking the x, y, and z components of the coordinate and multiply them by -1. Application The function to calculate the inverse coordinate will take in a VELatLong object and will return a VELatLong object. There is not a sgn function in the JavaScript Math class but we can do this using simple logic. function inverseSpherical(latlong) Listing 1 Inverse Spherical Coordinate The following algorithm calculates the inverse coordinate of a Cartesian coordinate. This algorithm takes in a Cartesian coordinate and returns a Cartesian coordinate. function inverseCartesian(p1) Listing 2 Inverse Cartesian Coordinate Calculating the Radius of the EarthTheory A large majority of these calculations are based on the Earth being a perfect sphere with evenly geocentric latitudes. Making this assumption allows us to use these formulas in any part of the world with minimal error while at the same time keeping the mathematics simple enough to understand. In this article the radius of the Earth is used for several calculations. The radius of the Earth can be represented using any form of measurement, kilometers (km), miles (mi), etc. For these formulas we can define the radius in any units as long as we also define any distance variables in the same units. For most cases using the average radius of the Earth, 6367 km, will result in accurate enough results. In the real world we know that the Earth is an ellipsoidal in shape and that the circles of latitude are further apart near the equator and closer together near the poles. Ellipses have a minor axis and a major axis. For Earth, the minor axis passes through the poles and has an approximate polar radius of 6,356.7523142 km (b). The major axis is equivalent to the radius at the equator with an approximate radius of 6,378.137 km (a). When Earth is assumed to be a sphere the latitude is the angle between the equatorial plane and a line from the center of Earth (r). In the real world the angle of latitude is the angle between the radius of curvature in the minor axis and the equatorial plane (rN). Figure 1 Geodetic Latitude in Ellipsoidal Earth The formula to calculate the radius of curvature in the minor axis is as follows: The formula for calculating the radius of the Earth at a specific geodetic latitude with respect to the center of the Earth is as follows: Application All the formulas in this article will implement a global variable for the radius of the Earth as defined below. We will also make the radius of Earth at poles and equator global variables. Rather than using a and b as variable names we will us equatorRadius (a) and polarRadius (b). var earthRadius = 6367; //radius in km var equatorRadius = 6378.137; //equatorial radius in km var polarRadius = 6356.7523142; //polar radius in km Listing 1 Global Variable: Radius of the Earth For calculating the radius of the Earth at a specific geodetic latitude we will create two functions. The first function will take in a VELatLong and return the radius and the second function will take a latitude in degrees. function getEarthRadius(latlong) Listing 2 Radius of Earth at a specific geodetic Latitude The following post has additional information on the DegToRad method: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!257.entry
Calculate Distance between two CoordinatesThere are several different ways to calculate the distance between two coordinates, P1(r, θ, Φ) and P2(r, θ, Φ). The first method consists of converting the points to Cartesian coordinates, and then calculating the cord length (d) between the points (straight line distance through the Earth). From there we can calculate the central angle between the two points (δ). Once we have the central angle between the two points we can make a simple calculation to figure out the arc length (L) between the two points. Figure 1 Arc Length calculation To calculate the cord length between P1 and P2 we can use Pythagorean Theorem as follows: Now that we have the cord length we can calculate the central angle between the two points. Using simple trigonometry we can calculate We want The second method for calculating the distance between two points is the Haversine method. The basic principal is similar to the last method of calculating the distance between two points except that it does not solve for x, y, and z, but uses the formulas of the components. The cord length between the two points can be calculated by using Pythagorean Theorem against the displacement in the x, y, and z axis. By using the following trigonometric identities we can derive a simpler formula for the cord length between two points. We can then use the atan2 function to derive the central angle between the two points: We can then calculate the distance between the two points using the following formula: Both of these methods produce fairly accurate results. If more precision is required the Vincenty formula can be used which calculates the distance between two points on an ellipsoid. The basic principals are similar to the other two formulas except that the radius varies between the two points. Application For the function that calculates the arc length between two points it will take in to VELatLong objects and return a number. This function has to convert the VELatLongs into Cartesian coordinates. When we use the convertSphericalToCartesian function the returned value is a Cartesian coordinate object with X, Y, and Z values. var earthRadius = 6367; //radius in km Listing 1 Arc Length function The Haversine function will take in to VELatLong objects and return a number. In this function we will need to convert the latitude and longitude values from degrees to radians using the DegtoRad function. function haversineDistance(latlong1,latlong2) Listing 2 Haversine function The following post has additional information on the RadToDeg and DegToRad methods: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!257.entry The following post has additional information on the convertSphericalToCartesian method: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!280.entry |
|
|