{"id":1332,"date":"2022-08-30T15:15:39","date_gmt":"2022-08-30T15:15:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/goggle-maps-api-uncaught-referenceerror-directionsservice-is-not-defined-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:39","modified_gmt":"2022-08-30T15:15:39","slug":"goggle-maps-api-uncaught-referenceerror-directionsservice-is-not-defined-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/goggle-maps-api-uncaught-referenceerror-directionsservice-is-not-defined-closed-collection-of-common-programming-errors\/","title":{"rendered":"Goggle maps API &#8211; Uncaught ReferenceError: directionsService is not defined [closed]-Collection of common programming errors"},"content":{"rendered":"<p>Ia m trying to use direction on my map used on my site, but there is one error that keeps popping up and I have no idea how to get rid of it!<\/p>\n<p><em><strong>Uncaught ReferenceError: directionsService is not defined<\/strong><\/em><\/p>\n<p>This is my code used<\/p>\n<pre><code>$(window).load(function () {\n    var directionsDisplay;\n    var directionsService = new google.maps.DirectionsService();\n    var map;\n});\n$(window).load(function () {\n        initialize();\n    });\n\nfunction initialize() {\n  directionsDisplay = new google.maps.DirectionsRenderer();\n  var chicago = new google.maps.LatLng(41.850033, -87.6500523);\n  var mapOptions = {\n    zoom:7,\n    mapTypeId: google.maps.MapTypeId.ROADMAP,\n    center: chicago\n  }\n  map = new google.maps.Map(document.getElementById(\"map\"), mapOptions);\n  directionsDisplay.setMap(map);\n  directionsDisplay.setPanel(document.getElementById(\"directionsPanel\"));\n}\n\nfunction calcRoute() {\n  var start = new google.maps.LatLng(54.986136, -1.537945);\n  var end = new google.maps.LatLng(41.850033, -87.6500523);\n\n  var request = {\n    origin:start,\n    destination:end,\n    travelMode: google.maps.TravelMode.DRIVING\n  };\n  console.log(request);\n  if (request==true)\n  {\n    console.log(\"REQUEST\");\n  }\n  console.log(JSON.stringify(request)); \n  directionsService.route(request, function(result, status) {\n    if (status == google.maps.DirectionsStatus.OK) {\n        if (!response.routes[0].copyrights) { \n        response.routes[0].copyrights = \"Copyrights unknown.\"; \n        }\n        console.log(\"ALL GOOD!\");\n      directionsDisplay.setDirections(result);\n    }\n    else\n    {\n        console.log(\"NOT-GOOD!\");\n    }\n  });\n}\n<\/code><\/pre>\n<ol>\n<li>\n<p>Remove the var-keyword in this line:<\/p>\n<pre><code>var directionsService = new google.maps.DirectionsService();\n<\/code><\/pre>\n<p>When you use the var-keyword, the variable <code>directionsService<\/code> is only visible inside the function, but you need a global variable(visible from any scope)<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:17:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Ia m trying to use direction on my map used on my site, but there is one error that keeps popping up and I have no idea how to get rid of it! Uncaught ReferenceError: directionsService is not defined This is my code used $(window).load(function () { var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1332","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1332"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1332\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}