0

I have the following code to show direction form one point to another. I have followed the code provided by this post http://stackoverflow.com/questions/5959788/google-maps-api-v3-how-show-the-direction-from-a-point-a-to-point-b-blue-lineby the user @user2314737.

I changed the code little bit according to my knowledge. But the directions are not shown on my map; What am I doing wrong?

Here is the code;

        <script>
        function initialize(){
            var latlng = new google.maps.LatLng(6.242635,80.051840); //latitude and longitude respectively, pointA
            var pointB = new google.maps.LatLng(6.237038,80.054709);

            var contentString = '<h1> Volunteer House </h1>'+
                '<p>Many people also refer to this home as <b>VH</b>. This is a must see place in the city.'+ 
                    '<br><br>Direction - Pass the <i>balangoda Children Park</i> and head along the <i>K Road</i>. '+
                    'Turn to left at the second turn and walk just 25m.</p>'+
                    '<br>Address - K Road, balangoda, Sri Lanka'+
                    '<br>Website: Volunteer House, <a href="http://r.comlu.com" target="_blank">'+
                    'http://r.comlu.com</a>';

            var infowindow = new google.maps.InfoWindow({
                content: contentString
            });             

            var mapProp = {
                center:latlng, //or you can pointB
                zoom:15, //4 - displays in world map zoom
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);


            //------------------------------ Multiple markers using loops
            var places = [
                ['balangoda Children Park', 6.241713, 80.052307, 2], //2 is the zIndex - order in which these markers should display on top of each other
                ['My Home', 6.242693, 80.051855, 1]
            ];

            var images = ['children.ico', 'office2.ico'];


            var myMarker = new Array(); //store marker in array

            for( var i=0; i<places.length; i++ ){
                    var place = places[i];
                    myMarker[i] = new google.maps.Marker({
                            position: {lat: place[1], lng:place[2]},
                            map: map,
                            title: place[0],
                            icon: images[i],                                
                            zIndex: place[3]
                    });

                    if( i == 1 ){
                            myMarker[1].setAnimation(google.maps.Animation.BOUNCE);

                            myMarker[1].addListener('click', function(){ 
                                infowindow.open(map, myMarker[1]); 
                            })
                    }
            };

            //---------------------------------------------------------------------

            /*------------------------------ show direction from point A to B using Google Maps API Direction Service
               DirectionService - object to send and receive direction requests
               DirectionRenderer - object to render the returned route on the map */

            directionsService = new google.maps.DirectionsService;
            directionsDisplay = new google.maps.DirectionsRenderer({
                    map: map
            });

            //get route from A to B
            calculateAndDisplayRoute( directiionsService, directionsDisplay, latlng, pointB );

            //---------------------------------------------------------------------
        }

        function calculateAndDisplayRoute( dS, dD, pA, pB){
                dS.route(
                        { //1
                            origin: pA,
                            destination: pB,
                            travelMode: google.maps.TravelMode.DRIVING
                        },//1
                        function(response, status){ //2
                            if(status==google.maps.DirectionsStatus.OK){
                                dD.setDirections(response);
                            }
                            else{
                                window.alert('Directions request failed due to ' + status);
                            }
                        } //2
                );
        }


        google.maps.event.addDomListener(window, 'load', initialize)

    </script>
Sofiene Djebali
  • 4,398
  • 1
  • 21
  • 27
Amali Perera
  • 115
  • 1
  • 1
  • 8
  • 3
    first check if th browser console there are errors when you load the page and run the code – ScaisEdge May 20 '16 at 16:43
  • 2
    Did the original, un-edited code work for you? If so, what changes did you make? Also, what does this have to do with PHP? All the code you provided is Javascript. – Patrick Q May 20 '16 at 16:45
  • 1
    don't comment on the previous question .. comment here otherwise is impossible follow you – ScaisEdge May 20 '16 at 16:49
  • @PatrickQ, unedited code works fine. I just made only minor changes like variable names, longitude, etc. – Amali Perera May 20 '16 at 16:59
  • @scaisEdge I press F12 and check. I can see no errors. Only one warning Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys – Amali Perera May 20 '16 at 17:01
  • @scaisEdge, in my code, only the 'direction service' related coding parts only do not work!! – Amali Perera May 20 '16 at 17:02
  • 1
    If it worked before your changes, but doesn't work after, then you should note your changes in your question. It doesn't matter how minor you think they are, something about them is causing a problem. – Patrick Q May 20 '16 at 17:24
  • 1
    I have posted an answer hope is useful ... – ScaisEdge May 20 '16 at 17:34
  • 1
    I get a javascript error with the posted code `Uncaught ReferenceError: directiionsService is not defined` (as sciasEdge indicates in his answer), [when I fix that, I see a result (fiddle)](http://jsfiddle.net/geocodezip/f8upmb8b/). – geocodezip May 20 '16 at 18:59
  • @geocodezip, sir you are correct. Now I fixed problem. Thanks. But how did you see that javascript error? I could not see any error. Unlike PHP, does not Javascript show syntax errors, undefined variables by default? Is there anyway to check it? I check in firebug pressing F12. But no errors shown. – Amali Perera May 21 '16 at 03:19
  • 1
    It is in the javascript console, see [Troubleshooting](https://developers.google.com/maps/documentation/javascript/tutorial#Troubleshooting) – geocodezip May 21 '16 at 05:37

1 Answers1

1

You have a mistake is directionsService and not directiionsService

calculateAndDisplayRoute( directionsService, directionsDisplay, latlng, pointB );
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I did not see any error both on webpage and firebug (F12) when I ran my code with that mistake. (just rest of the program showed the maps except direction). Unlike PHP, does not Javascript show syntax errors, undefined variables by default? Is there anyway to check it? – Amali Perera May 21 '16 at 03:22
  • 1
    Which browser do you use .. ? – ScaisEdge May 21 '16 at 06:33
  • I use Google chrome. – Amali Perera May 21 '16 at 07:01
  • I don't know where is palced the error console in chrome . I use Firefox with firebugs ,, firefox developer edition sometime and Opera with developer tool ... and these browser in console show aklway the javascrit result (error included .. ) – ScaisEdge May 21 '16 at 10:15