{"id":896,"date":"2022-08-30T15:08:59","date_gmt":"2022-08-30T15:08:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/google-maps-set-zoom-from-hidden-input-value-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:59","modified_gmt":"2022-08-30T15:08:59","slug":"google-maps-set-zoom-from-hidden-input-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/google-maps-set-zoom-from-hidden-input-value-collection-of-common-programming-errors\/","title":{"rendered":"Google maps set zoom from hidden input value-Collection of common programming errors"},"content":{"rendered":"<p>I have a form with a hidden input that saves user&#8217;s current zoom level<\/p>\n<pre><code> \n<\/code><\/pre>\n<p>However, when I try to use the zoom value from the input after I submit my form it doesn&#8217;t use the input&#8217;s value<\/p>\n<pre><code>var zoom = $(\"#zoom\").val();\nvar point;\nvar gmap = new GMap2(document.getElementById(\"gmap\"));\n\nif (zoom == \"\") {\n    point = new google.maps.LatLng(latitude,longitude);\n    gmap.setCenter(point, 13);\n} else {\n    console.log(zoom); \/\/shows 15\n    point = new google.maps.LatLng(latitude,longitude);\n    gmap.setCenter(point, zoom); \/\/here the zoom is undefined\n}\n<\/code><\/pre>\n<ol>\n<li>\n<p>The value of &#8216;zoom&#8217; from the input field is a <strong>string<\/strong>, and the function requires a <strong>number<\/strong>. Use <code>parseInt()<\/code> to cast the field value as a numeric value.<\/p>\n<pre><code>var zoom = parseInt($(\"#zoom\").val());\nvar point;\nvar gmap = new GMap2(document.getElementById(\"gmap\"));\n\/\/ point assignment is abstracted from your zoom level logic\npoint = new google.maps.LatLng(latitude,longitude);\n\ngmap.setCenter(point, (typeof zoom == 'number') ? zoom : 13);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:55:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a form with a hidden input that saves user&#8217;s current zoom level However, when I try to use the zoom value from the input after I submit my form it doesn&#8217;t use the input&#8217;s value var zoom = $(&#8220;#zoom&#8221;).val(); var point; var gmap = new GMap2(document.getElementById(&#8220;gmap&#8221;)); if (zoom == &#8220;&#8221;) { point = [&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-896","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/896","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=896"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/896\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}