0
votes

I am trying to provide walking directions on campus between buildings. When I use the following code, the start and end coordinates get placed on the nearest named road rather than the specific path that was specified.

    var request = {
    origin: "34.23974770397957,-118.53099968624116",
        destination: "34.240064785369974,-118.52827992630006",
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };
    directionsService = new google.maps.DirectionsService();
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });

You can see the problem here http://www.csun.edu/~kjm39451/maps/mapsError/main.html by selecting the drop down for anything in both fields.

But I want the result to look like this https://maps.google.com/maps?saddr=34.23974770397957,-118.53099968624116&daddr=34.240064785369974,-118.52827992630006&hl=en&ll=34.239825,-118.530614&spn=0.003721,0.004823&sll=34.23962,-118.52964&sspn=0.003721,0.004823&geocode=FQR1CgIdSFzv-A%3BFUF2CgId6Gbv-A&dirflg=w&mra=ls&t=m&z=18

How can I use google maps api to give directions just like google maps does?

1

1 Answers

1
votes

The destination and origin properties of DirectionsRequest can either take a string or a LatLng class. If you supply a LatLng it seems to be working.

var request = {
  origin: new google.maps.LatLng(34.23974770397957, -118.53099968624116),
  destination: new google.maps.LatLng(34.240064785369974, -118.52827992630006),
  travelMode: google.maps.DirectionsTravelMode.WALKING
};

You can refer to the maps API documentation: https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest

https://developers.google.com/maps/documentation/javascript/reference#LatLng