未捕获的ReferenceError:未在初始化时定义google

时间:2018-12-18 00:01:16

标签: function reference initialization loading

我无法弄清楚我的代码出了什么问题。我收到了这个ReferenceError,并且我知道初始化函数存在问题,但是我不知道要四处移动什么以便正确加载。

这是在多个位置的Wordpress网站上使用的,并且该特定页面是唯一无法使用的页面。

这是我的代码:

<section class="copy first">
<div class="container">
<h2 id="map_anchor"><?php the_field('pg_map_title') ?></h2>
<p><?php the_field('pg_map_text') ?></p>
</div>
</section>
<?php  
$lat = 41.01413965527589;
$long = -102.53826150000003;
$pg_lat = get_field('pg_latitude');
$pg_long = get_field('pg_longitude');
if ( !is_null($pg_long) ) {
	$long = $pg_long;
}
if ( !is_null($pg_lat) ) {
	$lat = $pg_lat;
}

	$map_theme = get_field('pg_map_styles');

    $styles_file = get_template_directory() . '/assets/styles/map_styles.json';
    $map_styles = file_get_contents($styles_file);
?>


<script>
        function initialize() {
        var map;
        var layer_0;
        var layer_1;
        var styles = <?php echo $map_styles; ?>;

	var map_style = [];
	var user_style = styles['<?php echo $map_theme; ?>'];
	if (user_style && user_style['style'] != 'undefined') {
		map_style = user_style['style'];
	}
else {
	var default_style = styles['Light Dream'];
	if (default_style && default_style['style'] != 'undefined') {
		map_style = default_style['style'];
	}
}
        map = new google.maps.Map(document.getElementById('map-canvas'), {
          center: new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>),
          zoom: 15,
styles: map_style
        });

        layer_0 = new google.maps.FusionTablesLayer({
          suppressInfoWindows: true,
          clickable: false,
          query: {
            select: "col8",
            from: "13zAsmjWdE-uVeVPkr-1qqUQGfZFgPBnBwbNpsWEE"
          },
          map: map,
          styleId: 2,
          templateId: 2
        });
        layer_1 = new google.maps.FusionTablesLayer({
          query: {
            select: "col2",
            from: "1nhy-62Mdybjlipp3_kPrmZb_E4Z7fFjBVhNarqGk"
          },
          map: map,
          styleId: 2,
          templateId: 2
        });
      }
		
      window.onload = initialize;
    </script>
<section class="interactive">
<div class="map-container">
<div class="map-overlay top"></div>
<div id="map-canvas"></div>   
<div class="map-overlay bottom"></div>
</div>
</section>

0 个答案:

没有答案