Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I am trying to use Google Direction API. Please help with my code

<!DOCTYPE html> <html> <body> <div id="map-canvas" style="width:400px;height:400px"></div> <div id="map-canvas"></div>

<script> function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB) { directionsService.route({ origin: pointA, destination: pointB, travelMode: google.map.TravelMode.Driving }, function (response, status) { if (status == google.maps.DirectionStatus.OK) { directionsDisplay.setDirections(response); } else { window.alert('Directions request failed due to'+status); } }); } function initMap() { var pointA = new google.maps.LatLng(44.960240, -93.124948), pointB = new google.maps.LatLng(44.969966, -93.159800), myOption = { zoom: 10, center: pointA, }, map = new google.maps.Map(document.getElementByID('map-canvas'),myOptions), directionService = new google.maps.DirectionsService, directionDisplay = new google.maps.DirectionsRenderer({map:map }), markerA = new google.maps.Marker({ position: pointA, title: "point A", label: "A", map:map }), markerB = new google.maps.Marker({ position: pointB, title: "point B", label: "B", map:map }), calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB); } </script>

</body> </html>

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

1) Your script provides two functions but there is nowhere either are called. The first is called within the second, but there is nothing that executes the second. 2) Your script references the the Google Maps API library but you aren't loading it in.