javascripthtmlgoogle-maps-api-3maps

Google Maps API v3: Driving directions over a map which first loads 2 positions


I have a map which displays two locations with markers (and automatically centers between these 2 locations)

I'm trying to integrate it with the Directions service, exactly like this demo: https://developers.google.com/maps/documentation/javascript/examples/directions-simple

The map should first load the 2 locations markers and then if a user select a start point it should show directions to one of the 2 locations.

Here's what I have so far (of course doesn't work, the map isn't loaded)... :P http://jsfiddle.net/multiformeingegno/fhAJA/5/

JAVASCRIPT

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

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
var locations = [
    ['Place 1', 41.897467, 12.470364, 2],
    ['Place 2', 41.896561, 12.467792, 1]
];

    var infowindow = new google.maps.InfoWindow();

    var bounds = new google.maps.LatLngBounds();

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });

        bounds.extend(marker.position);

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

    map.fitBounds(bounds);

    var listener = google.maps.event.addListener(map, "idle", function () {
        map.setZoom(17);
        google.maps.event.removeListener(listener);
    });

    directionsDisplay.setMap(map);
}

function calcRoute() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

HTML:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<b>Start: </b>

<select id="start" onchange="calcRoute();">
    <option value="chicago, il">Chicago</option>
    <option value="st louis, mo">St Louis</option>
    <option value="joplin, mo">Joplin, MO</option>
    <option value="oklahoma city, ok">Oklahoma City</option>
    <option value="amarillo, tx">Amarillo</option>
    <option value="gallup, nm">Gallup, NM</option>
    <option value="flagstaff, az">Flagstaff, AZ</option>
    <option value="winona, az">Winona</option>
    <option value="kingman, az">Kingman</option>
    <option value="barstow, ca">Barstow</option>
    <option value="san bernardino, ca">San Bernardino</option>
    <option value="los angeles, ca">Los Angeles</option>
</select> <b>End: </b>

<select id="end" onchange="calcRoute();">
    <option value="41.897467, 12.470364">Place 1</option>
    <option value="41.896561, 12.467792">Place 2</option>
</select>
<div id="map" style="position:relative;width:500px;height:300px"></div>

What am I doing wrong? :)


Solution

  • You are missing a google.maps.Map object. The fiddle doesn't have the API script included correctly:

    var myOptions = {
        zoom: 10,
        center: new google.maps.LatLng(-33.9, 151.2),
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map"),
                                    myOptions);
    

    working fiddle (updated so directions work, at least from locations in Italy)