<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saraan Asim on Medium]]></title>
        <description><![CDATA[Stories by Saraan Asim on Medium]]></description>
        <link>https://medium.com/@saraanofficial?source=rss-65b20414547d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_qdNrCG2kC31OXjE</url>
            <title>Stories by Saraan Asim on Medium</title>
            <link>https://medium.com/@saraanofficial?source=rss-65b20414547d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:08:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saraanofficial/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Google Maps Integration in Next 14,13 and React (Load, Display) |Step By Step Guide]]></title>
            <link>https://medium.com/@saraanofficial/google-maps-integration-in-next-14-13-and-react-load-display-step-by-step-guide-ab2f6ed7b3c0?source=rss-65b20414547d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab2f6ed7b3c0</guid>
            <category><![CDATA[google-maps-api]]></category>
            <category><![CDATA[next13]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[next-14]]></category>
            <category><![CDATA[google-maps]]></category>
            <dc:creator><![CDATA[Saraan Asim]]></dc:creator>
            <pubDate>Sun, 11 Feb 2024 13:13:10 GMT</pubDate>
            <atom:updated>2024-02-11T13:13:10.486Z</atom:updated>
            <content:encoded><![CDATA[<h3>Google Maps Integration in Next 14, 13 and React |Step By Step Guide</h3><p>This article will give you a complete understanding about <strong>loading google maps and displaying a map </strong>in Next 14 (Latest at the time of writing this article)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AhuaJsmX24QHmcUm826Ypg.png" /></figure><h3>NOTE: This approach will work for React js and Next 13 as well.</h3><h3>Prerequisites</h3><ul><li>A Next js application</li><li>Google Map API Key (If you don&#39;t have one, then below is the guide to get your own)</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F2_HZObVbe-g%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2_HZObVbe-g&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2_HZObVbe-g%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/de58c1ab0c32d38764b9a63fd06f376d/href">https://medium.com/media/de58c1ab0c32d38764b9a63fd06f376d/href</a></iframe><h3><strong>Lets Get Started!</strong></h3><h3>Step 1: Install <strong>@react-google-maps/api </strong>package</h3><pre>npm i @react-google-maps/api</pre><p>Or if you are using Yarn package manager,</p><pre>yarn add @react-google-maps/api</pre><h3>Step 2: Place the google maps API key in your env</h3><ol><li>Create a file named as “<strong><em>.env</em></strong>” in the root directory to store all your sensitive environment variables.</li><li>Place your API key inside it, like</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8qFdMRbaS4o7wHx9GDVlbg.png" /><figcaption>Google map API key placed inside the .env file created in root directory</figcaption></figure><h3>Step 3: Create a provider for loading google maps</h3><p>Although <strong>you don’t need to create a provider</strong>, you can just pick the code for loading google maps and place it in the component itself (Where you want to place the code for displaying a map), but I would <strong>prefer this approach</strong>, since it provides r<strong>e-usability</strong> and you don’t need to copy paste the same code for loading google maps in multiple pages or components where you want to deal with any map related functionality.</p><ol><li><strong>Create a folder called providers </strong>(You can have other providers here too since its a standard approach ; such as redux provider, stripe provider etc)</li><li><strong>Create a file</strong> named “<strong><em>map-provider.tsx” </em></strong>and place below code inside it</li></ol><pre>//Since the map will be laoded and displayed on client side<br>&#39;use client&#39;;<br><br>// Import necessary modules and functions from external libraries and our own project<br>import { Libraries, useJsApiLoader } from &#39;@react-google-maps/api&#39;;<br>import { ReactNode } from &#39;react&#39;;<br><br>// Define a list of libraries to load from the Google Maps API<br>const libraries = [&#39;places&#39;, &#39;drawing&#39;, &#39;geometry&#39;];<br><br>// Define a function component called MapProvider that takes a children prop<br>export function MapProvider({ children }: { children: ReactNode }) {<br><br>  // Load the Google Maps JavaScript API asynchronously<br>  const { isLoaded: scriptLoaded, loadError } = useJsApiLoader({<br>    googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_MAP_API as string,<br>    libraries: libraries as Libraries,<br>  });<br><br>  if(loadError) return &lt;p&gt;Encountered error while loading google maps&lt;/p&gt;<br><br>  if(!scriptLoaded) return &lt;p&gt;Map Script is loading ...&lt;/p&gt;<br><br>  // Return the children prop wrapped by this MapProvider component<br>  return children;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oIKgwajLSne61bElkQ23GA.png" /><figcaption>Map provider’s code</figcaption></figure><h3>Step 4: Wrap the component inside the provider</h3><p><strong>NOTE : </strong><em>You can also wrap the JSX inside the root layout to load google maps for the whole application. Or you can make a route group and wrap that route group’s layout’s JSX inside the provider to load google maps for specific routes.</em></p><p>Lets <strong>wrap the content</strong> inside the “<strong><em>page.tsx</em></strong>” within the wrapper, since in this tutorial we are going to show the map inside the main “<strong><em>page.tsx</em></strong>” (default route)</p><pre>import { MapProvider } from &quot;@/providers/map-provider&quot;;<br><br>export default function Home() {<br><br>  return (<br>    &lt;MapProvider&gt;<br>      &lt;main&gt;<br>        Map will come here<br>      &lt;/main&gt;<br>    &lt;/MapProvider&gt;<br>  );<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8g4uaZb9jHId_Xhl1ENpig.png" /><figcaption>Wrapping of the provider</figcaption></figure><h3>Step 5: Make a separate component for map functionality</h3><p>Since we are working in a react based framework and the biggest advantage of react is code re-usability and separation of concerns using components, lets <strong>make a separate component for map related functionality</strong> and use it inside the provider in “<strong><em>page.tsx</em></strong>”</p><ol><li><strong>Create a folder named components</strong>, along side providers</li><li><strong>Create</strong> a “<strong><em>map.tsx</em></strong>” <strong>inside it</strong> having the following code</li></ol><pre>/* <br>Since the map was loaded on client side, <br>we need to make this component client rendered as well else error occurs<br>*/<br>&#39;use client&#39;<br><br>//Map component Component from library<br>import { GoogleMap } from &quot;@react-google-maps/api&quot;;<br><br>//Map&#39;s styling<br>export const defaultMapContainerStyle = {<br>    width: &#39;100%&#39;,<br>    height: &#39;80vh&#39;,<br>    borderRadius: &#39;15px 0px 0px 15px&#39;,<br>};<br><br>const MapComponent = () =&gt; {<br>    return (<br>        &lt;div className=&quot;w-full&quot;&gt;<br>            &lt;GoogleMap mapContainerStyle={defaultMapContainerStyle}&gt;<br>            &lt;/GoogleMap&gt;<br>        &lt;/div&gt;<br>    )<br>};<br><br>export { MapComponent };</pre><p>3. <strong>Import and use</strong> the newly created component inside “<strong><em>page.tsx</em></strong>”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7rsSdg9hR4vyK_9kfAm2-Q.png" /><figcaption>Usage of MapComponent</figcaption></figure><h3>We should be good to go, right? Not Quite</h3><p><strong>You must be seeing a blank screen right now.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x_4e5uR3L5sx3lsEBQPy8A.png" /><figcaption>Blank google map due to the absence of center, zoom and map options</figcaption></figure><p><strong>NOTE : </strong><em>If instead you are facing an error, please make sure that you have used “use client” at the top of component and the provider to make the code run on client side. Next js by default runs on server side.</em></p><h3>What did we miss? Oh, the what and how for displaying the map!</h3><p>Google maps component needs 4 necessary props to display a map</p><ol><li><strong>Some width and height</strong> in container styles</li><li><strong>Map center</strong></li><li><strong>Map zoom</strong> level</li><li><strong>Map options</strong></li></ol><p>We already fulfilled the first requirement, lets deal with the rest of them</p><h3>Step 6.1: Prepare default map center coordinates</h3><p>Create a constant “<strong>defaultMapCenter</strong>” having below coordinates for <a href="https://en.wikipedia.org/wiki/K2"><strong>K2, the world’s second highest peak</strong></a>, situated in <strong>Pakistan’s northern areas</strong>. (You will surely be delighted with the outcome at the end of this article)</p><pre>const defaultMapCenter = {<br>    lat: 35.8799866,<br>    lng: 76.5048004<br>}</pre><h3>Step 6.2: Prepare default map zoom</h3><p>Create a constant “<strong>defaultMapZoom</strong>” having default value of 18</p><pre>const defaultMapZoom = 18</pre><h3>Step 6.3: Prepare default map options</h3><p>Create a constant “<strong>defaultMapOptions</strong>” having the configuration of map involving the type of the map to display (road map or satellite view), tilt ratio etc.</p><pre>const defaultMapOptions = {<br>    zoomControl: true,<br>    tilt: 0,<br>    gestureHandling: &#39;auto&#39;,<br>    mapTypeId: &#39;satellite&#39;,<br>};</pre><h3>Last Step: Wire it all together!</h3><p>Lets assign these default values to the component</p><pre>&lt;GoogleMap<br>    mapContainerStyle={defaultMapContainerStyle}<br>    center={defaultMapCenter}<br>    zoom={defaultMapZoom}<br>    options={defaultMapOptions}&gt;<br>&lt;/GoogleMap&gt;</pre><h3>Press “Save” and Voila! Look at that view</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6gdr_KxifzF4g2f7psJOvA.png" /><figcaption>Image of K2, the 2nd highest peak in the world , situated in Pakistan’s northern areas.</figcaption></figure><h3>Final Code</h3><blockquote><strong><em>components/map.tsx</em></strong></blockquote><pre>/*Since the map was loaded on client side, <br>we need to make this component client rendered as well*/<br>&#39;use client&#39;<br><br>//Map component Component from library<br>import { GoogleMap } from &quot;@react-google-maps/api&quot;;<br><br>//Map&#39;s styling<br>const defaultMapContainerStyle = {<br>    width: &#39;100%&#39;,<br>    height: &#39;100vh&#39;,<br>    borderRadius: &#39;15px 0px 0px 15px&#39;,<br>};<br><br>//K2&#39;s coordinates<br>const defaultMapCenter = {<br>    lat: 35.8799866,<br>    lng: 76.5048004<br>}<br><br>//Default zoom level, can be adjusted<br>const defaultMapZoom = 18<br><br>//Map options<br>const defaultMapOptions = {<br>    zoomControl: true,<br>    tilt: 0,<br>    gestureHandling: &#39;auto&#39;,<br>    mapTypeId: &#39;satellite&#39;,<br>};<br><br>const MapComponent = () =&gt; {<br>    return (<br>        &lt;div className=&quot;w-full&quot;&gt;<br>            &lt;GoogleMap<br>                mapContainerStyle={defaultMapContainerStyle}<br>                center={defaultMapCenter}<br>                zoom={defaultMapZoom}<br>                options={defaultMapOptions}<br>            &gt;<br>            &lt;/GoogleMap&gt;<br>        &lt;/div&gt;<br>    )<br>};<br><br>export { MapComponent };</pre><blockquote><strong>providers/map-provider.tsx</strong></blockquote><pre>//Since the map will be laoded and displayed on client side<br>&#39;use client&#39;;<br><br>// Import necessary modules and functions from external libraries and our own project<br>import { Libraries, useJsApiLoader } from &#39;@react-google-maps/api&#39;;<br>import { ReactNode } from &#39;react&#39;;<br><br>// Define a list of libraries to load from the Google Maps API<br>const libraries = [&#39;places&#39;, &#39;drawing&#39;, &#39;geometry&#39;];<br><br>// Define a function component called MapProvider that takes a children prop<br>export function MapProvider({ children }: { children: ReactNode }) {<br><br>  // Load the Google Maps JavaScript API asynchronously<br>  const { isLoaded: scriptLoaded, loadError } = useJsApiLoader({<br>    googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_MAP_API as string,<br>    libraries: libraries as Libraries,<br>  });<br><br>  if(loadError) return &lt;p&gt;Encountered error while loading google maps&lt;/p&gt;<br><br>  if(!scriptLoaded) return &lt;p&gt;Map Script is loading ...&lt;/p&gt;<br><br>  // Return the children prop wrapped by this MapProvider component<br>  return children;<br>}</pre><blockquote><strong>page.tsx</strong></blockquote><pre>import { MapProvider } from &quot;@/providers/map-provider&quot;;<br>import { MapComponent } from &quot;./components/map&quot;;<br><br>export default function Home() {<br><br>  return (<br>    &lt;MapProvider&gt;<br>      &lt;MapComponent/&gt;<br>    &lt;/MapProvider&gt;<br>  );<br>}</pre><p><strong>I will be continuing from here</strong> and showing you how to<strong> place Markers, Polygons and Polylines</strong> in the next articles. Do give feedback on what problems you face and I will try my best to answer your queries.</p><p><strong>Till then, Stay tuned!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab2f6ed7b3c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>