Keyword Bolded Directions

Virtual Earth gives us the ability to get turn by turn directions which is great. However the directions may take longer to read through when you need to know which way to turn at the last minute. One way to aid this is to bold the key words in the directions so that they stand out. The following code is an simple example of how to display the plain text, un-bolded directions.

function OnGotRoute( route )
{
var legs = route.RouteLegs;
var leg = null;
var directions = "";

for (var i=0; i < legs.length; i++)
{
leg = legs[i];
legItinerary = leg.Itinerary;

for ( var j=0; j< legItinerary.Items.length; j++ )
{
item = legItinerary.Items[j];
directions += item.Text+"<br/>";
}
}

document.getElementById("directions").innerHTML = directions;
}

We can determine which words to bold by using regular expressions. Searching and bolding every keyword would require a list of all the possible key words which would result in a really long regular expression. However, there are a limited number of words that are commonly used by Virtual Earth in the directions which we don’t want to bold. Knowing this we can bold the key words by bolding the complete directions instruction and un-bolding all the common words. The following code example is a modified version of the previous code that bolds the complete direction instruction and un-bolds all common words, thus bolding all keywords.

function OnGotRoute( route )
{
var legs = route.RouteLegs;
var leg = null;
var directions = "";

for (var i=0; i < legs.length; i++)
{
leg = legs[i];
legItinerary = leg.Itinerary;

for ( var j=0; j< legItinerary.Items.length; j++ )
{
item = legItinerary.Items[j];
directions += "<b>"+item.Text.replace(/\b(Take| Turn|^Turn|Bear|Depart|onto|for|and|the|then|Keep
|follow|signs|toward|At|exit|ramp|stay|on|to|Make a|Road name changes|Arrive)\b/gi,"</b>$1<b>")+"</b><br/>";
}
}

document.getElementById("directions").innerHTML = directions;
}

The following is an example of set of bolded directions from zip code 12345 to zip code 04556:

Depart River Rd
Turn left onto Edison Ave
Turn right onto SR-5 / State St
Turn right onto Broadway
Take ramp left and follow signs for I-890 / Schenectady County Memorial Veteran Hwy East
Take ramp for I-90 East / Gov Thomas E Dewey Thruway East / New York State Thruway East toward Albany
Keep right onto I-87 South / Gov Thomas E Dewey Thruway South / New York State Thruway South
At exit 21A, take ramp right for Gov Thomas E Dewey Thruway East / New York State Thruway East toward Mass Tpke / Boston
Keep straight onto I-90 East / Gov Thomas E Dewey Thruway East / New York State Thruway East
At exit 11A, take ramp right for I-495 North toward Marlborough / N.H.-Maine
Keep straight onto I-95 North
At exit 44, take ramp right for I-295 North toward South Portland / Downtown Portland
At exit 28, take ramp right toward Coastal Route / Brunswick / Bath
Keep straight onto US-1 North / Blue Star Memorial Hwy
Keep left to stay on US-1 / Blue Star Memorial Hwy / Mill St
Keep left to stay on US-1
Make a U-turn at Cochran Rd
Turn left onto SR-27
Turn left onto Old County Rd
Road name changes to E Old County Rd
Turn left onto road
Arrive on the left

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s