title: taxi fare calculation using php with google distance matrix api v2
body: <p>I want to have a taxi fare calculation in my website.</p>
  
  <p>I have the following requirements:</p>
  
  <p>If Google Distance Matrix calculation show following distance kilometers, the rate should go like this:</p>
  
  <ul>
  <li>From 0 -10km = $65 (fixed price) + 11%</li>
  <li>10km = $70 + 11%</li>
  <li>15km = $80 + 11%</li>
  <li>20km = $90 + 11%</li>
  <li>25km = $100 + 11%</li>
  <li>30km = $120 + 11%</li>
  <li>30km above = $4 / km</li>
  </ul>
  
  <p>I put rate as $2 per km from 10 km above till 30km + $50  extra which result :</p>
  
  <p>if taxi if drived for </p>
  
  <ul>
  <li>20km = 20km × $2 + $50 = $90 + 11% </li>
  <li>21km = 21km × $2 + $50 = $92 + 11%</li>
  <li>22km = 22km × $2 + $50 = $94 + 11% and so on</li>
  </ul>
  
  <p>but i am dont know to calculate the fixed price of $65 which is the fix rate for below 10 km distance and $120 rate for 30 km and 30km above ie $4/km .</p>
  
  <p>the script i used for Google Distance Matrix is to show $2/km + $50 fix : (I have not added 11% in this script)</p>
  
  <pre><code>&lt;script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=_MY_API_KEY" 
  type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;?php
  $rate=2;
  $extra=50;
  ?&gt;
  &lt;script type="text/javascript"&gt;
  var geocoder, location1, location2, gDir;
      var map;
  var gdir;
  var geocoder = null;
  var addressMarker;
  var directionsPanel;
  var directions;
      function initialize() {
              geocoder = new GClientGeocoder();
              gDir = new GDirections();
              GEvent.addListener(gDir, "load", function() {
                      var drivingDistanceMiles = gDir.getDistance().meters / 1000;
                      var drivingDistanceround = 
  Math.round(drivingDistanceMiles*100)/100;
                      var cost = ((drivingDistanceMiles * &lt;?php echo $rate; ?&gt;) + (&lt;?php 
  echo $extra; ?&gt;))
                      &lt;?php echo $rate; ?&gt;) + (&lt;?php echo 
  $extra; ?&gt;))
                      };
                      */
                      var fare=cost;
                      var fare = Math.round(fare*100)/100;
                      document.getElementById('results').innerHTML = '&lt;table 
  width="100%" style="border-collapse:collapse; padding-top:3px;"&gt;&lt;tr&gt;&lt;td rowspan="2" 
  width="35"&gt;&lt;img src="images/rates.png"&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Distance: &lt;/strong&gt;' + 
  drivingDistanceround + ' Kilemeters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Estimated Cost: 
  &lt;/strong&gt;$ ' + fare + '&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;';
              });
              if (GBrowserIsCompatible()) {      
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(-33.722626, 150.810585), 10);
      gdir = new GDirections(map, document.getElementById("directions"));
      GEvent.addListener(gdir, "load", onGDimrectionsLoad);
      GEvent.addListener(gdir, "error", handleErrors);
  
    }
  }
  
  function setDirections(fromAddress, toAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + toAddress);
  
  
      }
  
      function showLocation() {
              geocoder.getLocations(document.forms[0].from.value, function (response) {
                      if (!response || response.Status.code != 200)
                      {
                              alert("Sorry, we were unable to geocode the first 
  address");
                      }
                      else
                      {
                              location1 = {lat: 
  response.Placemark[0].Point.coordinates[1], lon: 
  response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                              geocoder.getLocations(document.forms[0].to.value, function 
  (response) {
                                      if (!response || response.Status.code != 200)
                                      {
                                              alert("Sorry, we were unable to geocode 
  the second address");
                                      }
                                      else
                                      {
                                              location2 = {lat: 
  response.Placemark[0].Point.coordinates[1], lon: 
  response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                                              gDir.load('from: ' + location1.address + ' 
  to: ' + location2.address);
                                      }
                              });
                      }
              });
  
      ///////////////////////////////////////////////////////////
  
  var directionsPanel;
  var directions;
  
    directionsPanel = document.getElementById("route");
    directions = new GDirections(map, directionsPanel);
    directions.load("from: "+document.forms[0].from.value+" to: 
  "+document.forms[0].to.value);
  
      }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="initialize()" onunload="GUnload()"&gt;
  &lt;table width="915" border="2" cellpadding="0" cellspacing="0" bordercolor="#FF9F0F" 
  style="border-collapse:collapse"&gt;
    &lt;tr&gt;
  &lt;td bgcolor="#FFFF99" style="padding:10px;"&gt;&lt;table width="905" border="0" 
  cellspacing="0" cellpadding="0"&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;div id="map_canvas" style="width: 914px; height: 480px; border: solid 1px 
  #336699"&gt;&lt;/div&gt;&lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;tr&gt;
      &lt;td&gt;&lt;div id="form" style="width: 904px; text-align:center; border: solid 1px 
  #336699; background:#d1e1e4;"&gt;
        &lt;form action="#" onsubmit="document.getElementById('route').innerHTML=''; 
  showLocation(); setDirections(this.from.value, this.to.value); return false;"&gt;
          &lt;p style="font-family:Tahoma; font-size:8pt;"&gt;From:
            &lt;input id="fromAddress" type="text" onblur="if(this.value=='') 
  this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue) 
  this.value='';" value="Street Name City, State" maxlength="50" size="26" name="from" 
  style="font-family:Tahoma; font-size:8pt;" /&gt;
            To:
            &lt;input id="toAddress" type="text" onblur="if(this.value=='') 
  this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue) 
  this.value='';" value="Street Name City, State" maxlength="50" size="26" name="to" 
  style="font-family:Tahoma; font-size:8pt;"/&gt;
            &lt;input class="submit" name="submit" type="submit" value="Calculate" 
  style="font-family:Tahoma; font-size:8pt;" /&gt;
          &lt;/p&gt;
          &lt;div id="results" style="font-family:Tahoma; font-size:8pt; text-align:left; 
  padding-left:5px; padding-bottom:5px;"&gt;&lt;/div&gt;
          &lt;div id="route" style="font-family:Tahoma; font-size:8pt; text-align:left; 
  padding-left:5px; padding-bottom:5px;"&gt;&lt;/div&gt;
      &lt;/form&gt;
      &lt;/div&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  </code></pre>