Routing Narrative

HTML

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.css"/>
<body>
    <div id="map"></div>
    <div id="route-narrative" style="margin-top:20px"></div>
</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.js"></script>
<script src="https://www.mapquestapi.com/sdk/leaflet/v2.s/mq-map.js?key=Kmjtd%7Cluua2qu7n9%2C7a%3Do5-lzbgq"></script>
<script src="https://www.mapquestapi.com/sdk/leaflet/v2.s/mq-routing.js?key=Kmjtd%7Cluua2qu7n9%2C7a%3Do5-lzbgq"></script>

CSS

#map {
    height: 500px;
    width: 500px;
}

JavaScript

var map,

  dir;

var searchResult,

tempResult;

var tempResult;

var search =/(?!Turn|Merge|Keep|Start|Take|Crossing|Passing)[A-Z][a-z]+(?:[\s-][A-Z][a-z]+)*/g;

var searchs =/(?:Greenville|Spartanburg|Charlotte|Statesville|Roannoke|Hazelton|Allentown|Morristown|New York City|Danbury|Meriden|Hartford|Springfield|Boston)/g;

var str;map = L.map('map', {

  layers: MQ.mapLayer(),

  center: [ 33, -83 ],

  zoom: 5

});dir = MQ.routing.directions()

.on('success', function(data) {

  var legs = data.route.legs,

      html = '',

      maneuvers,

      i;

      var j;

      var s;

     

 

  if (legs && legs.length) {

      maneuvers = legs[0].maneuvers;

      searchResult = maneuvers.slice();

      for (i=0; i<maneuvers.length; i++) {

         

         searchResult[i]= searchResult[i].narrative.match(searchs);

         

         if(searchResult[i]!=null)

          html += searchResult[i] + '<br />';

        }

         str = searchResult.join();

         

 

 

      L.DomUtil.get('route-narrative').innerHTML = html;

  }

});var textFile = null,

 makeTextFile = function (text) {

   var data = new Blob([text], {type: 'text/plain'});    // If we are replacing a previously generated file we need to

   // manually revoke the object URL to avoid memory leaks.

   if (textFile !== null) {

     window.URL.revokeObjectURL(textFile);

   }    textFile = window.URL.createObjectURL(data);    // returns a URL you can use as a href

   return textFile;

 };navigator.geolocation.getCurrentPosition(function(position) {

     

    var lat= position.coords.latitude;

    var lng= position.coords.longitude;

   

 

dir.route({

  locations: [

        {latLng:{

          "lat":lat,

          "lng":lng

       }},

     

      'Boston, MA'

     

  ],

  });

});    var create = document.getElementById('create');

     create.addEventListener('click', function () {

   var link = document.getElementById('downloadlink');

   link.href =...