在Wordpress网站上获取Map API密钥错误

时间:2018-12-04 02:05:34

标签: wordpress api dictionary key fusion

此页面无法正确加载我的Google地图。我正在尝试帮助一个朋友,但是我很沮丧。在API密钥中调用的脚本只有一次,但是出现以下错误:

您已在此页面上多次包含Google Maps JavaScript API。这可能会导致意外错误。

无效的地图密钥

我格式化错误吗?

<?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 async defer src="https://maps.googleapis.com/maps/api/js?key=AlzaSyBYE1TZJEILsqvtODWyqh0okG7Ts1PNzv8&callback=initMap"
  type="text/javascript"></script>

<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>

1 个答案:

答案 0 :(得分:0)

您的回调函数是“初始化”的,因此请将此函数放在api键之后的url中,并重新创建api。然后尝试。