首页 > 解决方案 > 简码未在 wordpress 中显示地图?

问题描述

我编写了一个似乎在浏览器上运行良好的 javascript 代码,但是当我尝试在 wordpress 中查看它时,没有地图。我已经使用 WPCoder 将代码添加到 wordpress,然后将该简码添加到我的页面中以显示地图。

<!DOCTYPE html>
<html>
  <head>
    <title>Place Searches</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>

    <script>
        var map;
    var service;
    var infowindow;

    function initMap() {
      var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);

      map = new google.maps.Map(document.getElementById('map'), {
          center: pyrmont,
          zoom: 15
        });

      var request = {
        location: pyrmont,
        radius: '5000',
        query: 'Mutli cuisine restaurant',
        center: pyrmont,
      };

      service = new google.maps.places.PlacesService(map);
      service.textSearch(request, callback);
    }

    function callback(results, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
          var place = results[i];
          createMarker(results[i]);
        }
      }
    }
    function createMarker(place) {
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });

        google.maps.event.addListener(marker, 'click', function() {
          infowindow.setContent(place.name);
          infowindow.open(map, this);
        });
      }
    </script>

  </head>
  <body>
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBqKuEuFOEKe7ErGlm1q3K-dXPXYOI0sc8&libraries=places&callback=initMap" async defer></script>
  </body>
</html>

这在浏览器中运行良好,但 wordpress 页面根本不显示地图。只是一个纯白的屏幕,没有别的。

标签: javascriptwordpressgoogle-maps

解决方案


推荐阅读