Hi all,
Looking to get your input on a function I created. If there is something I could do better with it, or if it falls short of best practices.
It takes 2 parameters, a choice of 1 of 2 places to start from and the $_POST array. It then creates a URL that google can use to create the map and returns it.
Thanks,
Liquidgenius
Usage:
$googleMapsUrl = gDirections('A', $_POST);
/*
GOOGLE DIRECTIONS URL EXAMPLES
From Akron to old home:
http://maps.google.com/maps?f=d&hl=en&saddr=135+S+Fir+Hill+St.,+Akron,+OH+44304&daddr=2209+Avalon+Dr.,+Parma,+OH+44134&ie=UTF8&om=1
From Green to old home:
http://maps.google.com/maps?f=d&hl=en&saddr=1800+Raber+Rd.,+Uniontown,+OH+44685-8841,+US&daddr=2209+Avalon+Dr.,+Parma,+OH+44134&ie=UTF8&om=1
*/
// $campusChoice will be either 'A' for Akron, or 'G' for Green
function gDirections($campusChoice, $posted){
//Create an array for Akron, Green and Destination Addresses, all with same keys.
$akronCampus = array('address' => '135', 'street' => 'S. Fir Hill St.,', 'city' => 'Akron,', 'state' => 'OH', 'zip' => '44304');
$greenCampus = array('address' => '1800', 'street' => 'Raber Rd.,', 'city' => 'Uniontown,', 'state' => 'OH', 'zip' => '44685-8841');
$destination = array('address' => $posted['address'] . ',', 'street' => $posted['street'] . ',', 'city' => $posted['city'] . ',', 'state' => $posted['state'], 'zip' => $posted['zip']);
//Create a multidimensional array of the Akron, Green and Destination arrays called campuses.
$campuses = array('A' => $akronCampus, 'G' => $greenCampus, 'D' => $destination);
//Convert spaces to '+' symbols for all values of the multidimensional array
foreach ($campuses as $outer_key => $locations) {
foreach ($locations as $inner_key => $value) {
$campuses[$outer_key][$inner_key] = preg_replace("/ /","+",$campuses[$outer_key][$inner_key]);
}
}
//Set up the different parts of the url
$baseUrl = "http://maps.google.com/maps?f=d&hl=en";
$startUrl = "&saddr=";
$destinationUrl = "&daddr=";
$endUrl = "&ie=UTF8&om=1";
//Create the start string this is where the first parameter $campusChoice is used
foreach ($akronCampus as $key => $value){ //Doesnt matter if I use akron or green array since both have the same number/named keys
$startUrl .= $campuses[$campusChoice][$key];
}
//Create the destination string
foreach ($destination as $key => $value){
$destinationUrl .= $campuses['D'][$key];
}
//Put them all together
$googleMapsUrl = $baseUrl . $startUrl . $destinationUrl . $endUrl;
//Return the url
return $googleMapsUrl;
}