I'm passing this along in case it's useful to someone else.

One of my customers wanted a map of their locations, but they also wanted to give the user a chance to get driving directions.

Using GMap, it was relatively easy to create the map, and plot a list of markers. The reason I used GMap is because I didn't want to take on the javascript that the Google Map API revolves around. However, I couldn't find any information about providing driving directions with GMap.

Doing some reading in the FAQ, I saw that Google prefers users get driving directions via a link:

http://maps.google.com/maps?saddr={start_address}&daddr={destination_address}

If you've used markers with GMap, you already know that the markers are just an array of arrays with info like, latitude, longitude, etc. One of the pieces of the individual marker array is 'text', which is what shows up in the information balloon when you click a marker. So......

...since I already had the user enter their starting address to plot the center of the map, and I had already coded the address of the marker location into the 'text' property so the address would be displayed when the marker was clicked, I just modified Google's link to accept variables for the start_address and destination_address, and presto, the user can click on the "Directions to this location" link, and Google does the rest!

Because of non-disclosure agreements, I can't post the exact code, but the below should get you started if you want to try this.





Code to pull location lat, long, name, address, city, state, zip, and phone number goes here (to save time when displaying the map, I've geocoded all of the location addresses - now I just read the lat/long out of the database instead of having to look them up each time).

// add the above info to the 'text' property of the $markerlist
$markerlist[$i]['text'] = ('<b>' . $data->name . ' <br></b>' .  $data->addr1 .'<br> ' .  $data->city .', ' . $data->state .', '
                                          . $data->zip .' <br>' . $data->phone . '<br>');


// add the Google link with variable for start_address (I collected that info earlier in another function)
$get_directions = '<a href="http://maps.google.com/maps?saddr={' . $address . '}&daddr={'
                                                                       . $data->addr1 .', ' . $data->city .', '
                                                                       . $data->state .', ' . $data->zip
                                                                       . '}" target=_blank">Directions to this location</a>';

// add driving directions link to info balloon
$markerlist[$i]['text'] .= $get_directions;

If you need a nudge in the direction of putting multiple markers on a map, take a lookie at tingan's code at this link:
http://drupal.org/node/465010

Many thanks to the creators of the GMap and Location modules, as well as to tingan for the info on how to display multiple markers on a map.

Comments

ludo1960’s picture

...Thanks a bunch!