GeekyOmega
GeekyOmega

Reputation: 1305

Why is this modification of sample code for waypoints failing?

I am learning how to draw routes. This has been accomplished because of this excellent post as seen here. I thought it would be a simple process of adding waypoint(s) to this example as follows:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps API v3 Directions Example</title> 
   <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></head> 
<body style="font-family: Arial; font-size: 12px;"> 
   <div style="width: 600px;">
     <div id="map" style="width: 580px; height: 300px; float: left;"></div> 
   </div>

   <script type="text/javascript"> 

     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();

     var orig = new google.maps.LatLng(34.011689,-118.49633);
     var dest = new google.maps.LatLng(34.040818,-118.26815);


     //New coordinate defined below
     var wps1 = new google.maps.LatLng(34.069654,-118.44434);

    //Here is the waypoint 
    var waypts = [{ location: wps1, stopover: true }];

     var map = new google.maps.Map(document.getElementById('map'), {
       zoom:7,
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     directionsDisplay.setMap(map);


     var request = {
             origin: orig, 
             destination: dest, 
             waypoints: waypts, //Adding the waypoint here 
             travelMode: google.maps.DirectionsTravelMode.DRIVING
     }



      directionsService.route(request, function(response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       }
     });
   </script> 
</body> 
</html> 

When I test this, I just see a very direct route from the beach to downtown LA. I am trying to get it to show the detour to UCLA first.

I have checked out some Google documentation, but as far as I am concerned. What I did above should work? I know, computers don't lie, but whatever it is must be subtle.

1. I added the waypoint in the correct format. 2. I told the request to handle that waypoint.

What is going wrong?

Upvotes: 0

Views: 184

Answers (1)

geocodezip
geocodezip

Reputation: 161384

This waypoint is nowhere near LA:

 var wps1 = new google.mapsLatLng(38.854782,-94.741055);

and it has a typo (missing period between maps and LatLng):

 var wps1 = new google.maps.LatLng(38.854782,-94.741055);

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
   <title>Google Maps API v3 Directions Example</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>
   <div style="width: 600px;">
     <div id="map" style="width:600px; height:500px;"></div>
   </div>

   <script type="text/javascript">
     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();

     var orig = new google.maps.LatLng(34.011689,-118.49633);
     var dest = new google.maps.LatLng(34.040818,-118.26815);


     //New coordinate defined below
     var wps1 = new google.maps.LatLng(34.068921, -118.44518119999998);

    //Here is the waypoint
    var waypts = [{ location: wps1, stopover: true }];

     var map = new google.maps.Map(document.getElementById('map'), {
       zoom:7,
       center: wps1,
       mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     directionsDisplay.setMap(map);


     var request = {
             origin: orig,
             destination: dest,
             waypoints: waypts, //Adding the waypoint here
             travelMode: google.maps.DirectionsTravelMode.DRIVING
     }



      directionsService.route(request, function(response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       } else { alert("Directions Request failed: "+status); }
     });
   </script>
</body>
</html> 

working example

Upvotes: 1

Related Questions