Hi there,
So, the good news is, I’ve got it working.
This is what I’ve done:[LIST]
[]I have stripped everything out of your initial page apart from the input box where you can enter your postcode, the radio buttons where you can select the hotel and the “Journey Planner” button. The reason for this, is so that we can make a bare-bones working example, then slowly integrate it back into your framework, checking to see if it breaks at regular intervals.
[]I have moved the entire JavaScript pertaining to Google Maps out of the PHP file and into the first file.
[*]I discovered an “onComplete” call back function for FancyBox. I am now using this to call the initialize() function once the form has loaded in FancyBox
[/LIST]The page now works like this:
[LIST]
[]Enter your postcode into the input field, select your hotel, then press “Plan Route”.
[]This will shoot off an AJAX call to “test.php”, passing the postcode entered as the variable “s” and the postcode of the hotel as the variable “e”.
[]test.php will then display a form, with an empty holder for the map <div id="map" class="map"></div>
.
[]Back in the original file, once FB has finished loading, the initialize() function will fire and will insert its code into the aforementioned div.
[*]When you click on “Calculate route” the route will be calculated and displayed.
[/LIST]Here’s the code:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Manage Bookings » Client Control Panel — To Book</title>
<style rel="stylesheet" type="text/css">@import url("http://www.yourdomain.com/library/styles/styles.css");</style>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://www.yourdomain.com/library/scripts/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" href="http://www.yourdomain.com/library/scripts/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style>#fancybox-overlay{ position:fixed}</style>
</head>
<body>
<div id="application-surround">
<div id="application-container">
<div id="add">
<form action="http://www.yourdomain.com/bookings/add/5" method="post" accept-charset="utf-8" name="form-bookings" id="form-bookings" class="form-bookings">
<div id="column-central">
<fieldset>
<label for="post_code">Post Code †</label><br />
<input type="text" name="post_code" value="" id="post_code" class="post_code" />
<div id="map_routes_button">
<div id="button-link"><a class="fancybox button-link" id="button" href="test.php" title="Plan your journey">Journey Planner</a></div>
</div>
</fieldset>
</div> <!-- column-central -->
<div id="column-supplement">
<fieldset>
<div id="previewer">
<div class="column-left">
<div class="venue_item" id="venue_item_1_0">
<input type="radio" name="venue_id" class="button_1_0" id="radio_1_0" value="165" data-postcode="DE23 6AD"/>
<div><h5>INTERNATIONAL HOTEL, DERBY</h5></div>
</div> <!-- .venue_item -->
<div class="venue_item" id="venue_item_1_1">
<input type="radio" name="venue_id" class="button_1_1" id="radio_1_1" value="155" data-postcode="Postcode"/>
<div><h5>OTHER, TOWN/CITY</h5></div>
</div> <!-- .venue_item -->
<div class="venue_item" id="venue_item_1_2">
<input type="radio" name="venue_id" class="button_1_2" id="radio_1_2" value="164" data-postcode="CO7 7QY"/>
<div><h5>RAMADA, COLCHESTER</h5></div>
</div> <!-- .venue_item -->
</div> <!-- .column-left -->
</div> <!-- previewer --><!-- previewer -->
</fieldset>
</div> <!-- column-supplement -->
</form>
<div class="cleaner"></div>
</div> <!-- add -->
</div> <!-- booking -->
</div> <!-- application-container -->
<script type="text/javascript">
var la = '52.43564';
var lo = '-1.64363';
var venue = "To Book Limited";
var trans = google.maps.DirectionsTravelMode.DRIVING;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function initialize() {
var latlng = new google.maps.LatLng(la,lo);
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DEFAULT
}
};
var map = new google.maps.Map(document.getElementById("map"),myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directions"));
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: venue
});
}
function calcRoute() {
var start = document.getElementById("route-start").value;
var end = document.getElementById("route-end").value;
var request = {
origin: start,
destination: end,
travelMode: trans
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
if (status == 'ZERO_RESULTS') {
alert('No route could be found between the origin and destination.');
} else if (status == 'UNKNOWN_ERROR') {
alert('A directions request could not be processed due to a server error. The request may succeed if you try again.');
} else if (status == 'REQUEST_DENIED') {
alert('This webpage is not allowed to use the directions service.');
} else if (status == 'OVER_QUERY_LIMIT') {
alert('The webpage has gone over the requests limit in too short a period of time.');
} else if (status == 'NOT_FOUND') {
alert('At least one of the origin, destination, or waypoints could not be geocoded.');
} else if (status == 'INVALID_REQUEST') {
alert('The DirectionsRequest provided was invalid.');
} else {
alert("There was an unknown error in your request. Requeststatus: \
\
"+status);
}
}
});
}
$("#button").click(function(ev) {
console.log("here");
s = $("#post_code").val();
e = $('input[name=venue_id]:checked').attr('data-postcode')
ev.preventDefault();
$.fancybox(this, {
ajax: {
type : "POST",
data : 's=' + s + '&e=' + e
},
onComplete: function(){
initialize();
}
});
});
</script>
</body>
</html>
test.php
<?php
$s = $_POST['s'];
$e = $_POST['e'];
?>
<div class="box-modal">
<h2>Route Planning</h2>
<div id="map-container">
<div id="map" class="map"></div>
</div>
<form action="/route" onSubmit="calcRoute(); return false;" id="form-route">
<fieldset>
<legend>Start</legend>
<div class="column-left">
<ol>
<li>Choose your mode of transport</li>
<li>Enter your address or post code</li>
</ol>
<ul id="transport">
<li><img id="trans-car" src="http://www.yourdomain.com/library/images/ui/transport/car.png" alt="Car" width="30" height="30" /></li>
<li><img id="trans-bus" src="http://www.yourdomain.com/library/images/ui/transport/bus.png" alt="Bus or Train" width="30" height="30" /></li>
</ul>
<dl>
<dt><label for="route-start">Start</label></dt>
<dd><input type="text" id="route-start" value="<?php echo $s; ?>"></dd>
<dt><label for="route-end">Destination</label></dt>
<dd><input type="text" id="route-end" value="<?php echo $e; ?>"></dd>
</dl>
<div id="button">
<input type="submit" name="submit" value="Plan Route" class="button">
</div> <!-- button -->
</div> <!-- .column-left -->
<div id="directions"></div>
<div class="cleaner"></div>
</fieldset>
</form>
<script type="text/javascript">
imgpath = 'http://www.yourdomain.com/library/images/ui/transport/';
document.getElementById('trans-car').onclick = function() {
trans = google.maps.DirectionsTravelMode.DRIVING;
this.src = imgpath+'car2.png';
if (document.getElementById('trans-bus').src == imgpath+'bus2.png') {
document.getElementById('trans-bus').src = imgpath+'bus.png';
}
}
document.getElementById('trans-bus').onclick = function() {
trans = google.maps.DirectionsTravelMode.TRANSIT;
this.src = imgpath+'bus2.png';
if (document.getElementById('trans-car').src == imgpath+'car2.png') {
document.getElementById('trans-car').src = imgpath+'car.png';
}
}
</script>
</div> <!-- box-modal -->
I have also uploaded a demo to my servers, just so you can see it in action.
I hope this helps you somewhat.
I don’t know how much of this you can implement using a framework, but hopefully this should give you a good understanding of the fundamentals and point you in the right direction.