{"id":1386,"date":"2022-08-30T15:16:06","date_gmt":"2022-08-30T15:16:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/15\/get-directions-to-a-google-maps-marker-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:06","modified_gmt":"2022-08-30T15:16:06","slug":"get-directions-to-a-google-maps-marker-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/get-directions-to-a-google-maps-marker-collection-of-common-programming-errors\/","title":{"rendered":"Get Directions to a Google Maps Marker-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to get directions to a Google Maps Marker position from a postcode that a user can enter in a textbox. I&#8217;m using the Maps, Places and Directions Google Libraries.<\/p>\n<p>I&#8217;ve found this example from google and I&#8217;m trying to work from it http:\/\/code.google.com\/apis\/maps\/documentation\/directions\/<\/p>\n<p>I think I&#8217;m pretty much there. I want the user to be able to click on the marker from the map and then click a link that says &#8216;Directions from Marker One&#8217;. I can get this to appear correctly and I pass the correct location into a function but for some reason it doesn&#8217;t keep the entire latlng coordinates. It looks like it drops one of them and that&#8217;s why it throws an error. If i print out what I pass to the new function (placeLoc) it&#8217;s only showing one of the coordinates.<\/p>\n<p>the error it gives is: Uncaught Error: Invalid value for property : -0.5796900000000278<\/p>\n<p>heres the code I&#8217;ve got so far:<\/p>\n<pre><code>        function addPlaceResults(){\n\n    var request = {\n        location: midWayPoint,\n        radius: 7000,\n        types: [type]\n    };\n\n    infowindow = new google.maps.InfoWindow();\n    var service = new google.maps.places.PlacesService(map);\n    service.search(request, callback);\n    service.getDetails(request, callback);\n\n\n    function callback(results, status) {\n        if (status == google.maps.places.PlacesServiceStatus.OK) {\n            for (var i = 0; i &lt; results.length; i++) {\n            createMarker(results[i]);\n            }\n        }   \n    }\n\n    function createMarker(place) {\n        var placeLoc = place.geometry.location;\n        var marker = new google.maps.Marker({\n            map: map,\n            position: place.geometry.location\n        });\n\n        var request = { reference: place.reference };\n        service.getDetails(request, function(details, status) {\n\n            google.maps.event.addListener(marker, 'click', function() {\n                infowindow.setContent(\n                    details.name + \"<br \/>\" \n                    + details.formatted_address + \"<br \/>\" \n                    + \"Visit Website\" + \"<br \/>\" \n                    + details.formatted_phone_number + \"<br \/>\"\n                    + \"Directions from Marker One\" + \"<br \/>\"\n\n                );\n             infowindow.open(map, this);\n            });\n        });\n    }\n};\n\n        \/\/directions from point one\n        function dPoint1(x) {\n    console.log(x);\n    directionsService = new google.maps.DirectionsService();\n    directionsDisplay = new google.maps.DirectionsRenderer({\n        suppressMarkers: true,\n        suppressInfoWindows: true\n    });\n\n    directionsDisplay.setMap(map);\n    directionsDisplay.setPanel(document.getElementById('directions-panel'));\n\n    var request = {\n        origin: address1, \n        destination: x,\n        travelMode: google.maps.DirectionsTravelMode.DRIVING\n    };\n\n    directionsService.route(request, function(response, status){\n        if (status == google.maps.DirectionsStatus.OK) \n        {\n            directionsDisplay.setDirections(response);\n        }\n    });\n};\n<\/code><\/pre>\n<p>I&#8217;ve tried to include only the relevant code and details in this question. If there is anything I&#8217;ve missed or anything that isn&#8217;t clear please let me know and I&#8217;ll add it in \/ do my best to clarify myself.<\/p>\n<p>Once again, I&#8217;ll greatly appreciate any help.<\/p>\n<p>Cheers JA<\/p>\n<p><strong>Edit<\/strong><\/p>\n<p>Ok, that makes perfect sense. Thanks guys.<\/p>\n<p>I&#8217;ve tried using the toString() method but that doesn&#8217;t work. I can successfully pass a string with the two values, separated by a comma into dPoint1 function by using toUrlValue(). When I do this I can print them both out to the console using console.log(x,y) but I can&#8217;t actually run the code. I get the same error as before.<\/p>\n<p>I think the problem is because I need them to be one thing so i can add it to &#8216;destination: &#8216; for example it should ultimately be destination: x, but because I need to pass x &amp; y in I have to have destination: x ,y, which throws the error.<\/p>\n<p>I&#8217;m guessing I need to make use of the google.maps.LatLng() method.<\/p>\n<p>I added this in late last night:<\/p>\n<pre><code>newlatlng = new google.maps.LatLng((placeLoc.lat()+placeLoc.lat()));\n<\/code><\/pre>\n<p>that gave me some directions but not to the right place!<\/p>\n<p>Does anyone have any ideas on how I could use this?<\/p>\n<p>Thanks so much for all the help you&#8217;ve given me so far.<\/p>\n<p>JA<\/p>\n<ol>\n<li>\n<p>This won&#8217;t work<\/p>\n<pre><code>onclick='dPoint1(\" +placeLoc+ \")'\n<\/code><\/pre>\n<p>because you can&#8217;t pass objects to functions like that: concatenating it as a string will produce a string representation which will probably be in the form &#8220;x,y&#8221;. You need to define function dPoint1 to take x and y &#8212; which may well work &#8212; but it would be better to pass literal values explicitly which you derive from placeLoc.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-15 09:08:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to get directions to a Google Maps Marker position from a postcode that a user can enter in a textbox. I&#8217;m using the Maps, Places and Directions Google Libraries. I&#8217;ve found this example from google and I&#8217;m trying to work from it http:\/\/code.google.com\/apis\/maps\/documentation\/directions\/ I think I&#8217;m pretty much there. I want the user [&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-1386","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1386","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=1386"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1386\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}