I am wondering if it is possible to display a Google Maps map generated from the Maps JavaScript API in an ASPX page.
Does anyone have experience with this or something similar?
I am wondering if it is possible to display a Google Maps map generated from the Maps JavaScript API in an ASPX page.
Does anyone have experience with this or something similar?
Best answer by Zoltan Febert
OK, let’s do it with JavaScript.
Add these three PXLiteral to your page. The first loads Google API, the second displays the map, and the third declares the div for the map.
I got the scripts from here: https://developers.google.com/maps/documentation/javascript/adding-a-google-map#javascript
I do not have an API key, so I used the Beta key from the documentation. It didn’t display the map properly, but I saw the scripts were running.
Once you replace the demo scripts with your working ones, you should do it in the customization editor, and PXLiteral control will encode them properly. Make sure there is no line comment in your script, because PXLiteral converts the multi line script into a one line script, and half of your script will be commented out.
<px:PXLiteral Enabled="True" runat="server" ID="s1" Text="<!-- prettier-ignore --> <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))}) ({key: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "beta"});</script>" ></px:PXLiteral>
<px:PXLiteral Enabled="True" runat="server" ID="s2" Text="<script > let map; function retek() {}; async function initMap() { const position = { lat: -25.344, lng: 131.031 }; const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); map = new Map(document.getElementById("map"), { zoom: 4, center: position, mapId: "DEMO_MAP_ID", }); const marker = new AdvancedMarkerElement({ map: map, position: position, title: "Uluru", }); } initMap(); </script>" ></px:PXLiteral>
<px:PXLiteral Text="<div id="map" style="height:400px; width:400px"></div>" runat="server" ID="s3" ></px:PXLiteral>
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.