{"id":2791,"date":"2021-04-15T13:02:57","date_gmt":"2021-04-15T13:02:57","guid":{"rendered":"https:\/\/ntspl.co.in\/blog\/?p=2791"},"modified":"2023-01-31T08:09:37","modified_gmt":"2023-01-31T08:09:37","slug":"loading-google-maps-platform-javascript-in-modern-web-applications","status":"publish","type":"post","link":"https:\/\/www.ntspl.co.in\/blog\/loading-google-maps-platform-javascript-in-modern-web-applications\/","title":{"rendered":"Loading Google Maps Platform JavaScript in Modern Web Applications"},"content":{"rendered":"<div class=\"block-paragraph\">\n<div class=\"rich-text\">\n<p>We are pleased to announce two open-source libraries that make it easier for you to use the Maps JavaScript API in modern web apps. Up until now, the Maps JavaScript API has required the use of a script tag inserted into the HTML document; however, this pattern requires careful selection of script tag attributes and a deep understanding of how the browser parses and executes JavaScript. To simplify things, we released the <a href=\"https:\/\/www.npmjs.com\/package\/@googlemaps\/js-api-loader\" target=\"_blank\" rel=\"noopener noreferrer\">@googlemaps\/js-api-loader<\/a> and <a href=\"https:\/\/www.npmjs.com\/package\/@googlemaps\/react-wrapper\" target=\"_blank\" rel=\"noopener noreferrer\">@googlemaps\/react-wrapper<\/a> modules on NPM.<\/p>\n<p>&nbsp;<\/p>\n<p>The first library, <a href=\"https:\/\/www.npmjs.com\/package\/@googlemaps\/js-api-loader\" target=\"_blank\" rel=\"noopener noreferrer\">@googlemaps\/js-api-loader<\/a>, dynamically loads the Maps JavaScript API and wraps the process in a Promise. This means you can now import the js-api-loader and load the Maps JS API via JavaScript rather than HTML. The loader also supports all of the options for loading the API, such as version, libraries, and apiKey, passed in as an options object. Other benefits include retries on network errors and a singleton pattern to avoid duplicate loads of the Maps JavaScript API. The following shows the usage of this library:<\/p>\n<\/div>\n<\/div>\n<div class=\"block-image_full_width\">\n<div class=\"article-module h-c-page\">\n<div class=\"h-c-grid\">\n<figure class=\"article-image--large h-c-grid__col h-c-grid__col--6 h-c-grid__col--offset-3 \"><img decoding=\"async\" src=\"https:\/\/storage.googleapis.com\/gweb-cloudblog-publish\/images\/import_loader.max-1000x1000.png\" alt=\"Dynamic loading example\" \/><figcaption class=\"article-image__caption \">\n<div class=\"rich-text\">Dynamic loading example with @googlemaps\/js-api-loader<\/div>\n<\/figcaption><\/figure>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"block-paragraph\">\n<div class=\"rich-text\">\n<p>The second library, <a href=\"https:\/\/www.npmjs.com\/package\/@googlemaps\/react-wrapper\" target=\"_blank\" rel=\"noopener noreferrer\">@googlemaps\/react-wrapper<\/a>, provides a React component you can use to wrap child components that require the MapsJavaScript API. The component will load the Maps JS API first to ensure the API is available, followed by your child components. This component accepts props matching the same options as <a href=\"https:\/\/www.npmjs.com\/package\/@googlemaps\/js-api-loader\" target=\"_blank\" rel=\"noopener noreferrer\">@googlemaps\/js-api-loader<\/a> and it can safely be used many times in a web application. An example usage is below:<\/p>\n<\/div>\n<\/div>\n<div class=\"block-image_full_width\">\n<div class=\"article-module h-c-page\">\n<div class=\"h-c-grid\">\n<figure class=\"article-image--large h-c-grid__col h-c-grid__col--6 h-c-grid__col--offset-3 \"><img decoding=\"async\" src=\"https:\/\/storage.googleapis.com\/gweb-cloudblog-publish\/images\/New_React_2.max-1000x1000.png\" alt=\"Using the React wrapper component\" \/><figcaption class=\"article-image__caption \">\n<div class=\"rich-text\">Using the React wrapper component<\/div>\n<\/figcaption><\/figure>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"block-paragraph\">\n<div class=\"rich-text\">\n<p>These libraries manage the insertion of the script tag into the HTML document and abstract away the asynchronous complexity into a Promise and React hook. The internal implementation matches the traditional pattern below:<\/p>\n<\/div>\n<\/div>\n<div class=\"block-image_full_width\">\n<div class=\"article-module h-c-page\">\n<div class=\"h-c-grid\">\n<figure class=\"article-image--large h-c-grid__col h-c-grid__col--6 h-c-grid__col--offset-3 \"><img decoding=\"async\" src=\"https:\/\/storage.googleapis.com\/gweb-cloudblog-publish\/images\/Doctype.max-1000x1000.png\" alt=\"Script tag\" \/><figcaption class=\"article-image__caption \">\n<div class=\"rich-text\">Script tag inserted into HTML document<\/div>\n<\/figcaption><\/figure>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"block-paragraph\">\n<div class=\"rich-text\">\n<p>For more information on script loading patterns, see the documentation in the <a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/overview\" target=\"_blank\" rel=\"noopener noreferrer\">Maps JavaScript API overview<\/a>.<\/p>\n<p><i>For more information on Google Maps Platform, <a href=\"https:\/\/cloud.google.com\/maps-platform\/\">visit our website<\/a>.<\/i><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We are pleased to announce two open-source libraries that make it easier for you to use the Maps JavaScript API in modern web apps. Up until now, the Maps JavaScript API has required the use of a script tag inserted into the HTML document; however, this pattern requires careful selection of script tag attributes and [&hellip;]<\/p>\n","protected":false},"author":53,"featured_media":2949,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11,1],"tags":[477],"class_list":["post-2791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-uncategorized","tag-javascript-for-google-maps"],"acf":{"custom_meta_title":"Loading Google Maps Platform in Modern Web Applications","meta_description":"Google Cloud announced two open-source libraries that make it easier for you to use the Maps JavaScript API in modern web apps.","meta_keyword":"javascript for google maps, google maps javascript in web applications","other_meta_tag":"<meta property=og:locale content=\"en-IN\" \/>\r\n<meta property=og:type content=\"article\" \/>\r\n<meta property=og:title content=\"Loading Google Maps Platform JavaScript in Modern Web Applications\"\/>\r\n<meta property=og:description content=\"Google Cloud announced two open source libraries that make it easier for you to use the Maps JavaScript API in modern web apps.\"\/>\r\n<meta property=\"og:image\" content=\"https:\/\/ntspl.co.in\/blog\/wp-content\/uploads\/2021\/04\/loading-google-maps-platform-javascript-in-modern-web-applications.jpg\"\/>\r\n<meta property=og:url content=\"https:\/\/www.ntspl.co.in\/blog\/loading-google-maps-platform-javascript-in-modern-web-applications\"\/>\r\n<meta property=og:site_name content=NTSPL \/>\r\n\r\n<meta name=\"twitter:site\" content=\"@NTSPL\">\r\n<meta name=twitter:card content=\"summary\" \/>\r\n<meta name=twitter:description content=\"Google Cloud announced two open source libraries that make it easier for you to use the Maps JavaScript API in modern web apps.\"\/>\r\n<meta name=twitter:title content=\"Loading Google Maps Platform JavaScript in Modern Web Applications\"\/>"},"_links":{"self":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/2791"}],"collection":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/users\/53"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/comments?post=2791"}],"version-history":[{"count":4,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/2791\/revisions"}],"predecessor-version":[{"id":6473,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/posts\/2791\/revisions\/6473"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media\/2949"}],"wp:attachment":[{"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/media?parent=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/categories?post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ntspl.co.in\/blog\/wp-json\/wp\/v2\/tags?post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}