Javascript 谷歌地图API需要刷新才能正常工作
我在为我的个人网站编写谷歌地图API时遇到了问题。在我刷新浏览器之前,谷歌地图不会工作。下面是谷歌地图的脚本Javascript 谷歌地图API需要刷新才能正常工作,javascript,google-maps,Javascript,Google Maps,我在为我的个人网站编写谷歌地图API时遇到了问题。在我刷新浏览器之前,谷歌地图不会工作。下面是谷歌地图的脚本 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&v3"></script> <script type="text/javascript" src="<?php echo
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&v3"></script>
<script type="text/javascript" src="<?php echo get_bloginfo('template_url'); ?>/js/mk.js"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new MarkerWithLabel({
position: results[0].geometry.location,
map: map,
labelContent: address,
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: {opacity: 1.0}
});
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}
initialize();
codeAddress("<?php
global $post;
$pid = $post->ID;
$terms = wp_get_post_terms($pid,'ad_location');
foreach($terms as $term)
{
echo $term->name." ";
}
$location = get_post_meta($pid, "Location", true);
echo $location;
?>");
</script>
在将map变量传递给markerOptions之前,需要确保该变量已初始化
有点过于热心的调试告诉我,在页面失败的时候,映射仍然是未定义的
$(document).ready()
通常出现在body.onload之前,因此要么在$(document).ready(function(){…})的顶部调用initialize()
或将用于初始化的代码放入其中
此外,虽然不是严格必要的,但是您应该考虑封装MAP变量,而不是使用Global。p> 在将map变量传递给markerOptions之前,需要确保该变量已初始化
有点过于热心的调试告诉我,在页面失败的时候,映射仍然是未定义的
$(document).ready()
通常出现在body.onload之前,因此要么在$(document).ready(function(){…})的顶部调用initialize()
或将用于初始化的代码放入其中
此外,虽然不是严格必要的,但是您应该考虑封装MAP变量,而不是使用Global。p> 您是否正在循环中加载
脚本
?如果不是,mk.js'
做什么?顺便说一下,这些参数sensor=false&v3
是不需要的。您是否检查过是否有其他插件正在调用maps api?检查您的代码字体并查看它。您是否正在循环中加载脚本
?如果不是,mk.js'
做什么?顺便说一下,这些参数sensor=false&v3
是不需要的。您是否检查过是否有其他插件正在调用maps api?请检查您的代码字体并查看。感谢您抽出时间与Marcos交流。我将尝试将代码向上移动,看看它是如何工作的。谢谢您的时间,Marcos。我将尝试将代码向上移动,看看它是如何工作的。