0

According to the Google documnetation, one can pass the Google Place ID of a location to the Direciton Service. However, regardless of what combination I try, I absolutely cannot get it to work; I am receiving a NOT_FOUND error. I have tried hard coding the id as a test to no avail.

The basic initialization code:

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




     function initialize() {

        var mapOptions = {
        center: { lat: 37.30138, lng: -89.57778},
        zoom: 15,
        }; 

        hotelMap = new google.maps.Map(document.getElementById("googlemaps"), mapOptions);

          var marker = new google.maps.Marker({
          position:{ lat: 37.30138, lng: -89.57778},
          map: hotelMap,
          });

          var info = new google.maps.InfoWindow({
          content: "3265 William Street, Cape Girardeau, MO 63701"
          });

         marker.setMap(hotelMap);
         info.open(hotelMap, marker);

        directionsDisplay = new google.maps.DirectionsRenderer();
        directionsDisplay.setMap(hotelMap);
        directionsDisplay.setPanel(document.getElementById("directionModalBody"));

        document.getElementById("searchButton").addEventListener("click", function() {

        var keyword =  document.getElementById("searchBox").value;
        var requestOptions = {
        location: { lat: 37.3011339, lng: -89.5770238},
        radius: '5000',
        keyword: keyword
        };

        placesService = new google.maps.places.PlacesService(hotelMap);
        placesService.nearbySearch(requestOptions, findCallback);

        });

        }; // end initiallize

The window.onload function:

window.onload = function() {
initialize();
document.getElementById("calcDirections").onclick = function() {
if ($("#city").val() != null && $("#city").val() != "") {
findRoute();
} else {
alert("Please Enter a City");
}

}; // end onclick

$(".areaList").on("click", "a", function(e) {
e.preventDefault();
var placeID = $(this).attr("href");
 locationRoute(placeID);
}) // end onclick

};  

The problem function:

function locationRoute(locationID) {
var start = "ChIJfbJ8AyaId4gR4XCrciru2Qc";
var end = new google.maps.Place(locationID);
alert(locationID);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
}; // end request object
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
document.getElementById("getDirectionButton").click();
} else {
alert(status);
}// end if
}); // end route
} // end findRoute

I have tried just passing the place IDs as a string with no success. I have tried prefixing them, again no success. It seems from the Google documentation, one needs to create a google.maps.Place object, but how? I consulted the documentation (https://developers.google.com/android/reference/com/google/android/gms/location/places/Place#getId()), but did not see a constructor. How can I resolve this issue? Thanks so much.

KellyM
  • 2,472
  • 6
  • 46
  • 90
  • possible duplicate of [Pass Place ID location to destination in Google Maps API](http://stackoverflow.com/questions/33990153/pass-place-id-location-to-destination-in-google-maps-api) – geocodezip Sep 26 '16 at 22:59
  • possible duplicate of [Pass Google Maps SearchBox result to Directions Service](http://stackoverflow.com/questions/34270540/pass-google-maps-searchbox-result-to-directions-service) – geocodezip Sep 26 '16 at 23:00
  • possible duplicate of [Google Maps API (JS) Create a route using PlaceId in waypoints](http://stackoverflow.com/questions/36763790/google-maps-api-js-create-a-route-using-placeid-in-waypoints) – geocodezip Sep 26 '16 at 23:01

2 Answers2

2

Try this

directionsService.route({
    origin: {placeId: start},
    destination: {placeId: locationID}
    ...
Anatolii Suhanov
  • 2,524
  • 1
  • 12
  • 14
1

There are two different options available

if you want to use place id

directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID})

if you want to use lat and long

directionsService.route({
origin: {location: {lat:33.699234,lng:-102.870486}},
destination: {location: {lat:33.123366,lng:-102.862864}},
travelMode: "DRIVING"

and also make sure you configure direction service in google console

here is the link for that

https://developers.google.com/maps/documentation/javascript/directions

Arunakiran Nulu
  • 2,029
  • 1
  • 10
  • 16