HWD
HWD

Reputation: 1629

Google Maps API Save Waypoints

I'm really struggling with waypoints and I was hoping to get some help.

My ultimate goal is to send the waypoints data to a hidden input field that I can save to my database using PHP when the user is done plotting their route.

I've got the following code (Javascript API V3), but the last waypoints section keeps breaking the script.

jQuery(document).ready(function(){

// INITIALIZE MAP

var map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 5,
    center: new google.maps.LatLng(37.09024, -95.71289),
    mapTypeId: google.maps.MapTypeId.ROADMAP
}),
directions = new google.maps.DirectionsService(),
displayer = new google.maps.DirectionsRenderer({
    draggable: true
});

// LOAD DEFAULTS

/* start coordinates */

var start_lat = jQuery('input[name="origin_lat"]').val();
var start_lng = jQuery('input[name="origin_lng"]').val();

/* end coordinates */

var end_lat = jQuery('input[name="destination_lat"]').val();
var end_lng = jQuery('input[name="destination_lng"]').val();

displayer.setMap(map);
directions.route({
    origin: new google.maps.LatLng(start_lat,start_lng),
    destination: new google.maps.LatLng(end_lat,end_lng),
    travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (result) {
    displayer.setDirections(result);
});

// INFO WINDOW AND MARKERS

var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
  map: map
});

// WAYPOINTS

google.maps.event.addListener(displayer, 'directions_changed', function(){
    var waypoints = displayer.directions.route[0].legs[0].via_waypoint;
});

});

I'd greatly appreciate any help you can give.

Upvotes: 2

Views: 2126

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

This:

var waypoints = displayer.directions.route[0].legs[0].via_waypoint;

should be:

var waypoints = displayer.getDirections().routes[0].legs[0].via_waypoints;

Please note: start and end of a route are not included inside the waypoints-array.

Upvotes: 1

Related Questions