title: Finding the great circle distance between three points using the Google Maps API v3
body: <p>Unfortunately I was not able to find any code / example how to draw <strong>great circle distance</strong> *<em>between three(3) points on google maps</em>*??</p>
  
  <p>Here is the javascript for two points. How can I make it work with three points?</p>
  
  <pre><code>var geocoder;
  var map;
  var addresses;
  var results;
  var dist;
  
  function initialize() {
      geocoder = new google.maps.Geocoder();
      address1 = document.getElementById("distancefrom").value;
      address2 = document.getElementById("distanceto").value;
      (distance(address1, address2));
  }
  
  function distance(add1, add2) {
      if (!geocoder)
          return 'Error, no geocoder';
  
      addresses = new Array(2);
      addresses[0] = add1;
      addresses[1] = add2;
      results = new Array(2);
      results[0] = new Array(2);
      results[1] = new Array(2);
  
      results[0][0] = 0; results[0][1] = 0; results[1][0] = 0; results[1][1] = 0.87;
      geocoded(1);
  }
  
  function geocoded(i) {
      geocoder.geocode( { 'address': addresses[i] }, function(res, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              results[i][0] = parseFloat(res[0].geometry.location.lat());
              results[i][1] = parseFloat(res[0].geometry.location.lng());
              i--;
  
              if (i &gt;= 0)
                  geocoded(i);
              else {
                  var latlng1 = new google.maps.LatLng(results[0][0], results[0][1]);
                  var latlng2 = new google.maps.LatLng(results[1][0], results[1][1]);
  
                  dist = google.maps.geometry.spherical.computeDistanceBetween(latlng1, latlng2)/1000;
                  var dmi = 0.539957*dist;
                  document.getElementById('totaldistancemiles').value = dmi.toFixed(2);
                  document.getElementById('totaldistancekm').value = dist.toFixed(2);
                  showMap(latlng1,latlng2);
              }
          }
      });
  }
  
  function showMap(location1,location2) {
      latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,
      (location1.lng()+location2.lng())/2);
      faddress1 = document.getElementById("distancefrom").value;
      faddress2 = document.getElementById("distanceto").value;
      var mapOptions = 
      {
          zoom: 1,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.HYBRID
      };
  
      map = new google.maps.Map(document.getElementById("map"), mapOptions);
      var flightPlanCoordinates = [
          location1,
          location2
      ];
      var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: "#FF0000",
          strokeOpacity: .5,
          geodesic: true,
          strokeWeight: 3
      });
  
      flightPath.setMap(map);
  
      var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: "#000000",
          strokeOpacity: .5,
          strokeWeight: 0
      });
      flightPath.setMap(map);
      var igreen= 'https://dl.dropboxusercontent.com/u/85343999/igreen.gif';
      var marker = new google.maps.Marker({
          map: map, 
          icon: igreen, 
          position: location1
      });
      var latlngbounds;
      latlngbounds = new google.maps.LatLngBounds();
      latlngbounds.extend( location1);
      latlngbounds.extend( location2);
      map.fitBounds(latlngbounds);
      var infowindow = new google.maps.InfoWindow();
      infowindow.setContent(faddress1);
      google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map, marker);
      });
  
      var marker2 = new google.maps.Marker({
          map: map, 
          icon: igreen, 
          position: location2
      });
  
      var infowindow2 = new google.maps.InfoWindow();
      infowindow2.setContent(faddress2);
      google.maps.event.addListener(marker2, 'click', function() {
          infowindow2.open(map, marker2);
      });
  }
  </code></pre>