9

An easy sample of the Google Maps API does not work with accelerated mobile page...

 3 years ago
source link: https://www.codesd.com/item/an-easy-sample-of-the-google-maps-api-does-not-work-with-accelerated-mobile-pages-amps.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

An easy sample of the Google Maps API does not work with accelerated mobile pages (AMPs)

advertisements

I'm trying to implement this easy 'Hello World' sample with the Accelerated Mobile Pages (AMP) technology.

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      #map {height: 100%;}
      html, body {height: 100%;margin: 0; padding: 0;}
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {map = new google.maps.Map(document.getElementById('map'), {center: {lat: -34.397, lng: 150.644},zoom: 8});}
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=foo&callback=initMap" async defer></script>
  </body>
</html>

It works ok, but if I insert the AMP libraries as the second child of the tag

<script async src="https://cdn.ampproject.org/v0.js"></script>

it stops working.

What am I doing wrong?


Well, it seems that only the amp-iframe is the only possible way to embed a map in an AMP supported page.

Here is the example on how to use amp-iframe in embedding a map.

<amp-iframe width=300 height=300
    sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"
    layout="responsive"
    frameborder="0"
    src="https://www.google.com/maps/embed/v1/place?key=AIzaSyDG9YXIhKBhqclZizcSzJ0ROiE0qgVfwzI&q=Alameda,%20CA">
</amp-iframe>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK