简体   繁体   English

Google Maps Api v3标记。 Google未定义

[英]Google Maps Api v3 marker. google is undefined

I've a website with the google maps api which is loaded asynchronous. 我有一个带有google maps api的网站,该网站是异步加载的。 But this throws a errer: google is not found. 但这引发了错误:找不到google。 My code is: 我的代码是:

<script>

function initialize() {

  var mapOptions = {
    zoom: 10,
    center: new google.maps.LatLng(51.817116, 4.780616),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    panControl: false,
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    rotateControl: false
  };

  var map = new google.maps.Map(document.getElementById('maps'),
  mapOptions);
};

var customMarker = new google.maps.Marker({
  position: new google.maps.LatLng(51.817116, 4.780616),
  map: map
});

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3&sensor=false&' +
    'callback=initialize';
  document.body.appendChild(script);
}

addLoadEvent(loadScript);

</script>

When I delete the marker the code works correctly. 当我删除标记时,代码可以正常工作。 Why is'nt it working if I add the marker as specified in some examples? 如果我添加了某些示例中指定的标记,为什么它不起作用?

The addLoad is a load event. addLoad是一个加载事件。 That's not the problem... Can anybody help me to get this working? 那不是问题……有人可以帮我解决这个问题吗?

You can't use the Google Maps Javascript API v3 until it is loaded. 您必须先加载Google Maps Javascript API v3,然后才能使用它。 Your marker creation is running before the API is loaded. 您的标记创建正在加载API之前运行。 You need to move it in to the initialize function, which won't execute until the API is available. 您需要将其移到Initialize函数中,直到API可用时才执行。

<script>

function initialize() {

  var mapOptions = {
    zoom: 10,
    center: new google.maps.LatLng(51.817116, 4.780616),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    panControl: false,
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    rotateControl: false
  };

  var map = new google.maps.Map(document.getElementById('maps'),
  mapOptions);

  var customMarker = new google.maps.Marker({
    position: new google.maps.LatLng(51.817116, 4.780616),
    map: map
  });

};  // end of initialize


function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3&sensor=false&' +
    'callback=initialize';
  document.body.appendChild(script);
}

addLoadEvent(loadScript);

</script>

You're defining your map variable locally within initialize which means that none of the other functions can access it. 您要在initialize中本地定义地图变量,这意味着其他任何函数都无法访问它。 Declare it globally outside of initialize : initialize之外全局声明它:

var map;

function initialize() {
  // code
  map = new google.maps.Map(document.getElementById('maps', mapOptions);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM