【问题标题】:google maps variable is not defined谷歌地图变量未定义
【发布时间】:2018-12-15 01:47:12
【问题描述】:

我正在尝试使用从带有元框的自定义帖子生成的多个标记创建地图。作为最终结果,我希望使用带有帖子标题、一些描述和帖子链接的标记的地图。

我想我已经完成了一半,但我遇到了类似的错误

未捕获的 ReferenceError:未定义投资数据

我不知道我做错了什么?请你帮助我好吗?

我的代码:

$args = array('post_type'=> 'investments'); $the_query = new WP_Query( $args );
        if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        $investmentsData[$row]['meta'] = get_post_meta( $post->ID, 'investments_location_details', true );
        $investmentsData[$row]['investment'] = [
          'id' => get_the_id(),
          'title' => get_the_title(),
        ];

          foreach ($investmentsData as $key => $value) {
            $investment = $value['tabs'];
            $meta = $value['meta'];
          }

            wp_localize_script('jquery-core', 'investmentsData', $investmentsData);
          ?>
          <div id="map"></div>

          <script type="text/javascript">
             var map = new google.maps.Map(document.getElementById('map'), {
                 zoom: 5,
                 center: new google.maps.LatLng(52.351168, 18.5801487),
                 mapTypeId: google.maps.MapTypeId.ROADMAP
             });
             var infowindow = new google.maps.InfoWindow();
             var marker, i;
             var j = investmentsData.length;
             function initMap() {
                 for (i = 0; i < j; i++) {
                     marker = new google.maps.Marker({
                         position: new google.maps.LatLng(investmentsData[i].lat, investmentsData[i].lng),
                         map: map
                     });

                     google.maps.event.addListener(marker, 'click', (function(marker, i) {
                         return function() {
                             infowindow.setContent(investmentsData[i].title);
                             infowindow.open(map, marker);
                         }
                     })(marker, i));
                 }
             };
         </script>

      <?php
        endwhile;
        endif;
        wp_reset_postdata();
        wp_reset_query();
      ?>

【问题讨论】:

    标签: javascript php wordpress google-maps-api-3


    【解决方案1】:

    尝试添加以下代码并在 var j 中进行更改。

    var locations = <?php echo json_encode($investmentsData) ?>
    var j = locations.length;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 2020-09-20
      • 2016-07-25
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多