Map显示TypeError:$不是wordpress中的函数

时间:2017-12-22 23:48:46

标签: javascript jquery wordpress google-maps

我想在网站上显示谷歌地图。我不想使用embed方法,因此使用async defer方法。

奇怪的是我的代码在html文件中运行正常但是,当我在wordpress中使用它时我得到TypeError:$不是函数错误。

enter image description here

当我在wordpress中使用时,html文件和typeError怎么没有类型错误?

 <script>
  var rating;
  var address;
  var name;
  var infoWindow = null;
  function initMap() {
    var map = new google.maps.Map(document.getElementById('mapDiv'), {
      center: {lat: 33.531495, lng: -88.42207},
      zoom: 19
    });
    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.getDetails({
      placeId: 'ChIJJdJXNmjphogRo-W21kkhbJg'
    }, function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        rating = place.rating;
        name = place.name;

        //var labelIndex = 50;
        address = "<div>" + place.formatted_address + "</div>"
        var marker = new google.maps.Marker({
            map: map,
            position: place.geometry.location,

        });

          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
          place.formatted_address + '</div>');

          infowindow.open(map, marker);


         google.maps.event.addListener(map, 'idle', function(e) {

// Prevents card from being added more than once (i.e. when page is resized and google maps re-renders)
if ( $( ".place-card" ).length === 0 ) {
$(".gm-style").append('<div style="position: absolute; left: 0px; top: 34px;"> <div style="margin: 10px; padding: 1px; -webkit-box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px; box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px; border-radius: 2px; background-color: white;"> <div> <div class="place-card place-card-large"> <div class="place-desc-large"> <div class="place-name"></div><div class="image-place"><img id="photoLocation" src="" style="position: static; top: 0px;left: 0px;width: 175px;height: 165px;"></div><div class="address"></div></div><div class="navigate"> <div class="navigate"> <a class="navigate-link" href="https://www.google.com/maps/dir//33.53151,-88.422063/@33.53151,-88.422063,18z" target="_blank"> <div class="icon navigate-icon"></div><div class="navigate-text"> Directions </div></a> </div></div><div class="review-box"> <div id="review-number" class="review-number"></div><a href="http://www.google.co.in/search?q=Porter,+Singley,+%26+Crane+Family+Dentistry,+2900+Bluecutt+Rd+%232,+Columbus,+MS+39705,+EE.+UU.&ludocid=10983190192268436899#lrd=0x8886e9683657d225:0x986c2149d6b6e5a3,1" class="review-box-link" target="_blank">Reviews</a> </div><div class="saved-from-source-link" style="display:none"> </div><div class="maps-links-box-exp"> <div class="time-to-location-info-exp" style="display:none"> <span class="drive-icon-exp experiment-icon"></span><a class="time-to-location-text-exp" style="display:none" target="_blank"></a><a class="time-to-location-text-exp" style="display:none" target="_blank"></a> </div><div class="google-maps-link"> <a href="https://www.google.com.py/maps/place/Porter,+Singley,+%26+Crane+Family+Dentistry/@33.53151,-88.4242517,17z/data=!3m1!4b1!4m5!3m4!1s0x8886e9683657d225:0x986c2149d6b6e5a3!8m2!3d33.53151!4d-88.422063?hl=es-419" target="_blank">View larger map</a> </div></div></div></div>');
    //$(".review-number").append(rating);
    $("#review-number").append(rating);
       fillCard(place);
       //function to resize the map, responsive.
       google.maps.event.addDomListener(window, 'resize', function() {
            var center = googleMap.getCenter();
            google.maps.event.trigger(googleMap, "resize");
            googleMap.setCenter(center);
        });
   }
  });


      }
    });
              function fillCard(place){
                 var photos = place.photos;
                 var countRatig = rating.toString().slice(0,1);
                $(".address").append(address);
                $(".place-name").append(name);
                $("#photoLocation").attr("src", photos[1].getUrl({'maxWidth': 175, 'maxHeight': 138}))
                for (var i=0; i<countRatig; ++i ){
                    $("#review-number:nth-child(1)").append('<div class="icon rating-star rating-full-star"></div>');
                }
      }

  }
</script>

3 个答案:

答案 0 :(得分:0)

尝试使用jQuery代替$,因为如果我记得,wordpress会使用jQuery.noConflict()模式。

示例:

if ( jQuery( ".place-card" ).length === 0 ) { ...

答案 1 :(得分:0)

将每个$符号更改为jQuery

wordpress

不支持

$

答案 2 :(得分:0)

最好将整个功能放在文档就绪功能中。因此脚本在页面加载后执行。你可以像这样保持$符号:

jQuery(document).ready(function( $ ) {

  // Your function goes here...
  if ( $( ".place-card" ).length === 0 ) {
   //...
  }

});