0

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?

geocodezip
  • 158,664
  • 13
  • 220
  • 245
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34

1 Answers1

1

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

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Okay, you deserve some upvoting and this is answered. But as a parting comment, here is natural followup question. Is there an "easy" way to draw an arbitrary amount of waypoints? I am annoyed google api says we can only use 8 waypoints. What if I have 100 waypoints because I am traveling to Alaska or something like that? I guess I can make multiple routes, but googling on how to accomplish that ironically doesn't lend an easy answer. I guess they designed it broke on purpose? – GeekyOmega Jul 01 '13 at 21:54