Google Maps JavaScript API v3

Navigation functions (heading)

View this example full screen.

JavaScript
var poly;
var geodesicPoly;
var marker1;
var marker2;

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(34, -40.605)
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  map.controls[google.maps.ControlPosition.TOP_CENTER].push(
      document.getElementById('info'));


  marker1 = new google.maps.Marker({
    map: map,
    draggable: true,
    position: new google.maps.LatLng(40.71435280, -74.0059731)
  });

  marker2 = new google.maps.Marker({
    map: map,
    draggable: true,
    position: new google.maps.LatLng(48.8566140, 2.35222190)
  });

  var bounds = new google.maps.LatLngBounds(marker1.getPosition(),
      marker2.getPosition());
  map.fitBounds(bounds);

  google.maps.event.addListener(marker1, 'position_changed', update);
  google.maps.event.addListener(marker2, 'position_changed', update);

  var polyOptions = {
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    map: map,
  };
  poly = new google.maps.Polyline(polyOptions);

  var geodesicOptions = {
    strokeColor: '#CC0099',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    geodesic: true,
    map: map
  };
  geodesicPoly = new google.maps.Polyline(geodesicOptions);

  update();
}

function update() {
  var path = [marker1.getPosition(), marker2.getPosition()];
  poly.setPath(path);
  geodesicPoly.setPath(path);
  var heading = google.maps.geometry.spherical.computeHeading(path[0],
      path[1]);
  document.getElementById('heading').value = heading;
  document.getElementById('origin').value = path[0].toString();
  document.getElementById('destination').value = path[1].toString();
}

google.maps.event.addDomListener(window, 'load', initialize);
JavaScript + HTML
<!DOCTYPE html>
<html>
  <head>
    <title>Navigation functions (heading)</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry"></script>
    <script>
var poly;
var geodesicPoly;
var marker1;
var marker2;

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(34, -40.605)
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  map.controls[google.maps.ControlPosition.TOP_CENTER].push(
      document.getElementById('info'));


  marker1 = new google.maps.Marker({
    map: map,
    draggable: true,
    position: new google.maps.LatLng(40.71435280, -74.0059731)
  });

  marker2 = new google.maps.Marker({
    map: map,
    draggable: true,
    position: new google.maps.LatLng(48.8566140, 2.35222190)
  });

  var bounds = new google.maps.LatLngBounds(marker1.getPosition(),
      marker2.getPosition());
  map.fitBounds(bounds);

  google.maps.event.addListener(marker1, 'position_changed', update);
  google.maps.event.addListener(marker2, 'position_changed', update);

  var polyOptions = {
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    map: map,
  };
  poly = new google.maps.Polyline(polyOptions);

  var geodesicOptions = {
    strokeColor: '#CC0099',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    geodesic: true,
    map: map
  };
  geodesicPoly = new google.maps.Polyline(geodesicOptions);

  update();
}

function update() {
  var path = [marker1.getPosition(), marker2.getPosition()];
  poly.setPath(path);
  geodesicPoly.setPath(path);
  var heading = google.maps.geometry.spherical.computeHeading(path[0],
      path[1]);
  document.getElementById('heading').value = heading;
  document.getElementById('origin').value = path[0].toString();
  document.getElementById('destination').value = path[1].toString();
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
    <div id="panel" style="margin-left:-320px">
      Origin: <input type="text" readonly id="origin">
      Destination: <input type="text" readonly id="destination">
      Heading: <input type="text" readonly id="heading"> degrees
    </div>
  </body>
</html>

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.