0

my calculated route works good so far, but I have some trouble with my destination address.

When the destination is within a pedestrian area (city center), this destination (lat, long) will set to the next valid street. This is probably how it supposed to be, but it's important that I get the route until the specific destination address. What I want: E.g. I drive into the pedestrian area or I drive as far as possible by car and then walk to the destination.

Anyone has an idea how I can solve this problem? I am using the following code, the code works so that is not the problem:

<script type="text/javascript">

var directionsDisplay = new google.maps.DirectionsRenderer({
 animation: google.maps.Animation.DROP,
 draggable: true
});
var directionsService = new google.maps.DirectionsService();
var map;
var geocoder;
var resultsInput;

var question;
var routeStart, routeEnd;
var travMode;
var frage_gruppen_nr;

var zsf1,zsf2;

var zentrieren;

function initialize() {
 geocoder = new google.maps.Geocoder();

 routeStart = $('.multiple-short-txt:eq(0) .text:eq(0)');
     routeEnd = $('.multiple-short-txt:eq(0) .text:eq(1)');
 

 coords_field_map = $('.text-short:eq(1) .text:eq(0)');
 coords_field_map.hide();

 zsf1 = $('.multiple-short-txt:eq(1) .text:eq(0)');
 zsf2 = $('.multiple-short-txt:eq(1) .text:eq(1)'); 


  var aktuelle_frage = 'r1';
  var frage_gruppen_nr = parseInt(aktuelle_frage.substr(1));
  frage_gruppen_nr = frage_gruppen_nr - 1;

  if(frage_gruppen_nr>1){
   
   routeStart.val('xxxx'); 

   var coords;
   var ziel_vorfrage =  'abc'; 
   alert(ziel_vorfrage);
   geocoder.geocode( { 'address': ziel_vorfrage}, function(results1, status1) {
     if (status1 == google.maps.GeocoderStatus.OK) {
     coords = String(results1[0].geometry.location)

     } 
    });
   zentrieren = new google.maps.LatLng(coords);  
  }else{

  zentrieren = new google.maps.LatLng(49.759578, 6.644134);  
  }
 
 
 travMode = document.getElementsByTagName("SELECT")[0];
 
 question = '{SGQ}_c';

 var mapOptions = {
  zoom:12,
  center: zentrieren
 };
 
map = new google.maps.Map(document.getElementById('gmap_canvas_'+question), mapOptions);

 google.maps.event.addListener(directionsDisplay, 'directions_changed', function(event) {
  
  var directions = this.getDirections();
  var overview_path = directions.routes[0].overview_path;
  var startingPoint = overview_path[0];
  var destination = overview_path[overview_path.length - 1];

  if (typeof startLatlng === 'undefined' || !startingPoint.equals(startLatlng)) {
   startLatlng = startingPoint;
   getLocationName(startingPoint, function(name) {
   routeStart.val(name);
   });
  }
  if (typeof endLatlng === 'undefined' || !destination.equals(endLatlng)) {
   endLatlng = destination;
   getLocationName(destination, function(name) {
   routeEnd.val(name);
   });
  }
  //alert('startpoint: '+startingPoint);
  calculateDistances(startingPoint, destination);

 });

 directionsDisplay.setMap(map);  
 
 resultsInput = $('.text-long:eq(0) .textarea');

 $(resultsInput).attr('readonly', true);
 
}

function calcRoute() {
 
 var coords;
 var coords2;

 var start =  routeStart.val(); 
  geocoder.geocode( { 'address': start}, function(results1, status1) {
  if (status1 == google.maps.GeocoderStatus.OK) {
    coords = String(results1[0].geometry.location)

 } 
 else {
   alert('Die Startadresse konnte nicht gefunden werden.');
  }
  });
 
 
  var end = routeEnd.val();
   geocoder.geocode( { 'address': end}, function(results2, status1) {
   if (status1 == google.maps.GeocoderStatus.OK) {
     coords2 = String(results2[0].geometry.location)
     
  } 
  else {
   alert('Die Zieladresse konnte nicht gefunden werden.');
   }
  }); 
  
 var selectedMode;
 
 if(travMode.value == 1){
  selectedMode = 'DRIVING';
 }else if(travMode.value == 2){
  selectedMode = 'BICYCLING';  
 }else if(travMode.value == 3){
  selectedMode = 'TRANSIT';  
 }else if(travMode.value == 4){
  selectedMode = 'WALKING';  
 }

 //
 var request = {
   origin:start,
   destination:end,
   travelMode: google.maps.TravelMode[selectedMode]

 };
 directionsService.route(request, function(response, status) {
     if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
 }
  });
  
}

 
 function calculateDistances(origin, destination) {
  
   var selectedMode;

   //select travel mode
   if(travMode.value == 1){
    selectedMode = 'DRIVING';
   }else if(travMode.value == 2){
    selectedMode = 'BICYCLING';  
   }else if(travMode.value == 3){
    selectedMode = 'TRANSIT';  
   }else if(travMode.value == 4){
    selectedMode = 'WALKING';  
   }

  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix({
   origins: [origin],
   destinations: [destination],
   travelMode: google.maps.TravelMode[selectedMode],
   unitSystem: google.maps.UnitSystem.METRIC,
   avoidHighways: false,
   avoidTolls: false
  }, callback);
 }

 function callback(response, status) {
  
  if (status != google.maps.DistanceMatrixStatus.OK) {
   
  } else {
   
   
   var origins = response.originAddresses;
   var destinations = response.destinationAddresses;

   for (var i = 0; i < origins.length; i++) {
    var results = response.rows[i].elements;
    for (var j = 0; j < results.length; j++) {
     $(resultsInput).val('Startaddresse: '+origins[i]+'\n\nReiseziel: '+destinations[j]+'\n\nDistanz: '+results[j].distance.text+'\n\nZeit: '+results[j].duration.text+'');
     
     //save result to fields
     zsf1.val(results[j].distance.text);
     zsf2.val(results[j].duration.text);
    }
   }
  }
 }

 function getLocationName(latlng, callback) {
  geocoder.geocode({
   location: latlng
  }, function(result, status) {
   if (status === google.maps.GeocoderStatus.OK) {
    var i = -1;
    console.log(result);
    // find the array index of the last object with the locality type
    for (var c = 0; c < result.length; c++) {
     for (var t = 0; t < result[c].types.length; t++) {
      if (result[c].types[t].search('locality') > -1) {
       i = c;
      }
     }
    }

    var locationName = result[0].formatted_address;     
    callback(locationName);
   }
  });
 }
    

google.maps.event.addDomListener(window, 'load', initialize);
</script>
  • Have you tried requesting walking directions from the end of the route to the final destination? Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – geocodezip Feb 16 '16 at 16:17
  • related question: [Google Maps API directionsService.route different from Google Maps Directions](http://stackoverflow.com/questions/32596050/google-maps-api-directionsservice-route-different-from-google-maps-directions) – geocodezip Feb 16 '16 at 17:18
  • This is what I was looking for, thanks a lot. I will try it out on the weekend. – user3515431 Feb 17 '16 at 06:40

0 Answers0