Jimmy
Jimmy

Reputation: 16428

Cannot call method 'apply' of undefined on setting directions for jquery google maps plugin

I've followed the tutorial for creating a jquery mobile google map canvas here, and have tried to set that up however I'm getting this error in my JavaScript console:

Uncaught TypeError: Cannot call method 'apply' of undefined

I think that I may have a missing callback somewhere, but I can't spot it, can anyone please give me a pointer?

This is how I initialise and display the map as the page shows, which works fine I can pan/zoom around the map :

$('#map_canvas').gmap({ 'center': latLon,'scrollwheel':false});
    $('#map_canvas').gmap('option', 'zoom', 12);
    $('#map_canvas').gmap().bind('pageshow', function(ev, map){
        $('#map_canvas').gmap('addMarker', {'position':latLon, 'bounds':false}).click(function(){
            $('#map_canvas').gmap('openInfoWindow', {'content':myAddress}, this);
            });
    });

And I have these fields on my HTML page :

<p>
                <label for="from">From</label>
                <input id="from" class="ui-bar-c" type="text" value="Los Angeles" />
            </p>
            <p>
                <label for="to">To</label>
                <input id="to" class="ui-bar-c" type="text" value="New York" />
            </p>
            <a id="submit" href="#" data-role="button" data-icon="search">Get directions</a>


            <div id="map_canvas" style="width:100%;height:300px"></div>

            <div id="results" class="ui-listview ui-listview-inset ui-corner-all ui-shadow" style="display:none;">
                    <div class="ui-li ui-li-divider ui-btn ui-bar-b ui-corner-top ui-btn-up-undefined">Results</div>
                    <div id="directions"></div>
                    <div class="ui-li ui-li-divider ui-btn ui-bar-b ui-corner-bottom ui-btn-up-undefined"></div>
                </div>

The onclick binds to this JS function :

function getDirections()
{
    $('#map_canvas').gmap('displayDirections', { 'origin': 'Los Angeles, USA', 'destination': 'New York, USA', 'travelMode': google.maps.DirectionsTravelMode.DRIVING }, { 'panel': document.getElementById('panel') }, function(result, status) {
        if ( status === 'OK' ) {
                alert('Results found!');
        }
    }); 
}

Upvotes: 1

Views: 1014

Answers (1)

hereNT
hereNT

Reputation: 86

Turns out this is because a library is not included. In the files I downloaded, there was a jquery.ui.map.services.js file that had to be included before it worked. That's where the directions function lives.

Upvotes: 4

Related Questions