# Basic Core Spatial Math Example

The following example shows how to use a number of the core Spatial Math functions. It uses north west and south east coordinates of the map to calculate a geodesic line between these points (line that follows the curvature of the earth), the midpoint of this line, a point that is 300 miles from the north west coordinate along the line, the length of the line, the heading between the coordinates, and the also converts the north west coordinate into Degree Minute Second format.

``````<!DOCTYPE html>
<html>
<title></title>
<meta charset="utf-8" />
<script type='text/javascript'>
function GetMap() {
var map = new Microsoft.Maps.Map('#myMap', {
zoom: 4
});

var bounds = map.getBounds();

var northWest = bounds.getNorthwest();
var southEast = bounds.getSoutheast();

//Calculate a geodesic path between the two points (line that follows the curvature of the earth).
var path = Microsoft.Maps.SpatialMath.getGeodesicPath([northWest, southEast]);
var poly = new Microsoft.Maps.Polyline(path);
map.entities.push(poly);

//Calculate the midpoint of the line.
var midPoint = Microsoft.Maps.SpatialMath.interpolate(northWest, southEast);
var pin = new Microsoft.Maps.Pushpin(midPoint, {
title: 'Midpoint'
});
map.entities.push(pin);

//Calculate a location that is along the path 300 miles from the north west coordinate.
var point300MilesFromNW = Microsoft.Maps.SpatialMath.getLocationAlongPath(poly, 300, Microsoft.Maps.SpatialMath.DistanceUnits.Miles);
var pin2 = new Microsoft.Maps.Pushpin(point300MilesFromNW, {
title: '300 miles from NW'
});
map.entities.push(pin2);

//Calculate the distance from the northWest coordinate to the southEast coordinate.
var distance = Microsoft.Maps.SpatialMath.getDistanceTo(northWest, southEast, Microsoft.Maps.SpatialMath.DistanceUnits.Miles);

//Calculate the heading from the northWest coordinate to the southEast coordinate.

//Convert the northWest coordinate into Degree Minute Second format.
var northWestDMS = Microsoft.Maps.SpatialMath.toDegMinSec(northWest);

document.getElementById('outputPanel').innerHTML = 'Distance: ' + distance + ' miles<br/>Heading: ' + heading + ' degrees<br/>North West coordinate in DMS format: ' + northWestDMS;
});
}
</script>
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>