The error setDirection of undefined does not disappear.

Asked 3 months ago, Updated 3 months ago, 8 views

This site
When I was creating an application that allows me to search for routes between two points, I received the following error

cannot read property setDirections of undefined

setDirection is undefined, but I don't know the cause yet.The following comments were posted on Google's developer site:

The DirectionsResult contains the result of the directions query, which you may either handle yourself, or pass to a DirectionRender object, which can automatically handle displaying the result on a map.

To display a DirectionResult using a DirectionRender, you simply need to do the following:

1. Create a DirectionRender object.
2.Call setMap() on the render to bind it to the passed map.
3. Call setDirection() on the render, passing it the DirectionsResult as noted above.Because the render is an MVCObject, it will automatically detect any changes to its properties and update the map when associated directions have changed.

If you know the cause of the error, please let me know.

<!DOCTYPE html>
<html lang="ja">
    <title>two point route search</title>
    <script type="text/javascript"src=" = AIzaSyBouRngjBWxP4S7H1wqfShSsPkserg0xBw">/script>
    <script src=""></script>
        height —550px;

        <h2>Find a route between two points</h2>
        <form action="method="post">
            <p><span class="bold">Search by address/place name</span>/p>
            Brgin: <input type="text" name="sp" id="inputBegin">
            End: <input type="text" name="ep" id="inputEnd">
            <input type="submit" name="register" value="search" id="searchButton">


varmap, begin, end;
var directionsDisplay;
var directionsService;

begin = 'Tokyo Station';
end='Tokyo Skytree';

        e.preventDefault(); // href is disabled and screen transitions are not performed

        end=$('#inputEnd') .val();

        // Clear route description
        $('#directionPanel').text(' ');

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

function initialize (begin, end) {
    // instance [geocoder] creation
    vargeocoder=new google.maps.Geocoder();

        // Origin Keywords
        'address': begin

    }, function(result, status){
            // Specify a center point
            var latlng = result[0].geometry.location;

            // option
                center —latlng,
                scrollwheel: false, // wheel zoom
                mapTypeId: google.maps.MapTypeId.ROADMAP,

            // # Get map_canvas and create a map instance ([map]) of the contents of [mapOptions]
            map = new google.maps.Map( document.getElementById('map_canvas', myOptions);

            // Get Path
            directionsDisplay = new google.maps.DirectionRender();
            directionsDisplay.setPanel( document.getElementById('directionPanel')); // Route Details

            // place
            // $('#begin').text(begin);
            // $('#end').text(end);

        } else{
            alert('Unable to retrieve…'');

// route acquisition
function calcRoute(begin,end){

    var request = {
        origin —begin, // Start point
        destination:end, // end point
        travelMode:route in google.maps.TravelMode.DRIVING, // [automobile]
        voidHighways:true, // expressway usage flag

    // instance creation

    directionsService.route(request, function(response, status){
        } else{
            alert('Route not found...');

// kick
google.maps.event.addDomListener(window, 'load', function(){
    initialize (begin, end);
    calcRoute (begin, end);

javascript css html5 google-maps

2022-09-30 14:24

1 Answers

cannot read property setDirections of undefined

This error does not mean that setDirections is undefined, but directionsDisplay is undefined.

directionsDisplay is still in the undefined state when you call calcRoute() because directionsDisplay initializes asynchronously (after waiting for geocode communication results).

Therefore, it might work if you change the calling location of calcRoute() to call geocoder.geocode() after initializing directionsDisplay.

2022-09-30 14:24

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.