{"id":560,"date":"2025-05-29T21:22:07","date_gmt":"2025-05-29T21:22:07","guid":{"rendered":"https:\/\/ouritsource.com\/blog\/?p=560"},"modified":"2025-05-29T21:36:56","modified_gmt":"2025-05-29T21:36:56","slug":"geolocations-using-google-api","status":"publish","type":"post","link":"https:\/\/ouritsource.com\/blog\/geolocations-using-google-api\/","title":{"rendered":"Geolocations using Google API"},"content":{"rendered":"<body>\n<p>To create this simple web page, one will just need two files: a web page  to utilize the Google API and a JavaScript file containing coordinates that will be passed to the Google API.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>One will first need to obtain an API key to utilize Google\u2019s APIs. To obtain a key, go to Google\u2019s article: <a href=\"https:\/\/support.google.com\/googleapi\/answer\/6158862?hl=en\">Setting up API Keys<\/a> \u2013 <a href=\"https:\/\/support.google.com\/googleapi\/answer\/6158862?hl=en\">https:\/\/support.google.com\/googleapi\/answer\/6158862?hl=en<\/a><\/li>\n\n\n\n<li>Once you have an API Key, create an HTML page called <strong>geolocation.html.<\/strong><\/li>\n\n\n\n<li>You will need to specify your API Key  as part of the Google script:<br><br>&lt;script src=\u201dhttps:\/\/maps.googleapis.com\/maps\/api\/js?key=&lt;your API Key&gt;\u201d&gt;&lt;\/script&gt;<br><br>Below is an example of an html page utilizing the above:<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-8f05af93a13a1deb53f1668c7df22d34\"><code>&lt;!DOCTYPE HTML&gt;\n&lt;html&gt;\n&lt;title&gt;Geolocations Example&lt;\/title&gt;\n&lt;body&gt;\n  &lt;h1 style=\"text-align:center\"&gt;Dennis V DeOcampo&lt;\/h1&gt;\n  &lt;h2 style=\"text-align:center\"&gt;Revision: 03\/08\/2020&lt;\/h2&gt;\n&lt;div id=\"canvas\" \n  style=\"width: 100%; height: 600px; \n        border-bottom:#000 1px solid;\n        border-top: #000 1px solid;\n        border-left: #000 1px solid;\n        border-right: #000 1px solid;\"&gt;&lt;\/div&gt;\n\n &lt;script src=\"geolocation.js\"&gt;&lt;\/script&gt;\n\n&lt;script src=\"https:\/\/maps.googleapis.com\/maps\/api\/js?key=daSDFAFQWFDQW1ESAXZCSAC_notAValidKey\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n\n<\/code><\/pre>\n\n\n\n<p>4. As can one can see in the above html page, a JavaScript file called<strong> <em>geolocation.js <\/em>i<\/strong>s used.  The JavaScript file allows us to ask Google for our current coordinates as well as specify locations of businesses of coordinates we want shown on the map.<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-9fa0f57173c4daa0da2d9a8fc1d06d2a\"><code>\n\/\/get visitor's current location\nfunction getGeolocation() {\n    navigator.geolocation.getCurrentPosition(showMap);\n   }\n \n   \/\/show visitor's current location on map\n  function showMap(geoPos) {\n        \/\/locate user's location\n        geolocate = new google.maps.LatLng(geoPos.coords.latitude, geoPos.coords.longitude);\n        let mapProp = {\n        center: geolocate,\n        zoom:13,\n        };\n        \/\/map it to div id canvas and display user's location\n        let map=new google.maps.Map(document.getElementById('canvas'),mapProp);\n        let infowindow = new google.maps.InfoWindow({\n         map: map,\n        position: geolocate,\n        content:\n        `Your Location:\n           &lt;br&gt;Latitude: ${geoPos.coords.latitude}\n           &lt;br&gt;Longitude: ${geoPos.coords.longitude}`\n        });\n   \n        \/\/offices to mark on map\n        var offices = [\n        ['Office Location Number 1 &lt;br&gt; 5335 Village Market &lt;br&gt; Wesley Chapel, Fl 33545',28.2415238,-82.347757,'https:\/\/goo.gl\/maps\/HhjPnZC82EdjF35E9', 0],\n        ['Office Location Number 2 &lt;br&gt; 6333 Wesley Grove Blvd &lt;br&gt; Wesley Chapel, Fl 33545',28.2459471,-82.3477248,'https:\/\/goo.gl\/maps\/L8rbSU9D4zG3ndYo8',1],\n        ['Office Location Number 3 &lt;br&gt; 30056 FL-54, &lt;br&gt; Wesley Chapel, Fl 33545', 28.2454982,-82.3160237,'https:\/\/goo.gl\/maps\/DLd52YpJpWNaL7BN8', 2],\n        ['Office Location Number 4 &lt;br&gt; 28500 FL-54, &lt;br&gt; Wesley Chapel, Fl 33545', 28.2348261,-82.3463467,'https:\/\/goo.gl\/maps\/1h4KUGobfdfc7Z5f9', 3],\n        ['Office Location Number 5 &lt;br&gt; 31108 FL-54, &lt;br&gt; Wesley Chapel, Fl 33545', 28.234515,-82.298482,'https:\/\/goo.gl\/maps\/1h4KUGobfdfc7Z5f9', 4],\n        ['Office Location Number 6 &lt;br&gt; 2245 Lelan Ln &lt;BR&gt;Casselberry, FL 32707',28.648061,-81.333389 ,'https:\/\/goo.gl\/maps\/mzdkpxWjmMpEaB27A', 5],\n        ['Office Location Number 7 &lt;br&gt; 3425 Winter Lake Road &lt;BR&gt;Lakeland, FL 33803',27.9971698,-81.8888027,14.87 ,'https:\/\/goo.gl\/maps\/6qGfbt4NVsT1foKR7', 5],\n    \n      ]\n \n   \/\/place marker on businesses on the locations array\n   var marker, i,infowindow2\n\n   for (i = 0; i &lt; offices.length; i++) {\n   \n         marker = new google.maps.Marker({\n         position: {lat: offices[i][1], lng: offices[i][2]},\n         map: map,\n         title: offices[i][0],\n       });\n\n      var info = new google.maps.InfoWindow({})\n         \n      \/\/make marker clickable to display info and add a &lt;a href&gt; link\n      google.maps.event.addListener(\n        marker,\n        'click',\n        (function(marker, i) {\n          return function() {\n            info.setContent(offices[i][0] + \"&lt;br&gt;&lt;a href='\" + offices[i][3] + \"'&gt; Get Directions &lt;\/a&gt;\")\n            info.open(map, marker)\n          }\n        })(marker, i)\n      )\n\n   \n     \n  };\n\n  \n \n  }\n \n \n   getGeolocation();\n \n<\/code><\/pre>\n\n\n\n<p>5. As you can see in the screenshot below, the visitor\u2019s coordinates (longitude and latitude) is provided from the Google API.  The coordinates in red is based on the list of offices from a JSON formatted list in the JavaScript.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"179\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation.png?resize=640%2C179&#038;ssl=1\" alt=\"\" class=\"wp-image-561\" style=\"width:1085px;height:auto\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=1024%2C286&amp;ssl=1 1024w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=300%2C84&amp;ssl=1 300w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=768%2C214&amp;ssl=1 768w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=1536%2C429&amp;ssl=1 1536w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=2048%2C571&amp;ssl=1 2048w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=900%2C251&amp;ssl=1 900w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?resize=1200%2C335&amp;ssl=1 1200w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?w=1280&amp;ssl=1 1280w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/screenshot-geolocation-scaled.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>To create this simple web page, one will just need two files: a web page to utilize the Google API and a JavaScript file containing coordinates that will be passed to the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":562,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-script","has-thumbnail"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/GeolocationImage.png?fit=1536%2C1024&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":5,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":567,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions\/567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media\/562"}],"wp:attachment":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}