简体   繁体   English

使用用户定义的函数时地图消失

[英]Map disappears when using a user-defined function

I'm new to Js and I have a problem I can't fix. 我是Js的新手,遇到一个无法解决的问题。 I'm trying to write a function that makes faster the creation of markers on a Google map. 我正在尝试编写一个函数,以更快地在Google地图上创建标记。 The problem is that when I want to call the function either the whole map disappears or if I place the call function elsewhere the map don't disappears but the function does not work. 问题是,当我要调用该函数时,整个地图都会消失,或者如果我将调用函数放置在其他地方,地图不会消失,但该功能将无法正常工作。 I think my problem is a syntax problem. 我认为我的问题是语法问题。 Here is the code with call function placed in a random place for illustrative purposes: 这是带有调用函数的代码,出于说明目的,将其放在随机位置:

var map;
 function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
   center: {lat: 37.621389, lng: -122.378945},
   zoom: 9
 });

 function addMarker(posx, title) {
  var marker = new google.maps.Marker( {
   position: posx,
   map: map,
   title: title
 });
 }
}

addMarker ({lat: 37.621389, lng: -122.378945}, {'SFO'});

It's my first time asking here so please if I need to provide more information or any other tips regarding asking properly and comply with the standards of SO, let me know. 这是我的第一次询问,因此,请问我是否需要提供更多信息或有关正确询问并遵守SO标准的其他提示。

The problem relays in the Maps API asynchronous loading. 问题在Maps API异步加载中中继。 I am guessing the first error in the console. 我猜控制台中的第一个错误。

Uncaught ReferenceError: addMarker is not defined 未捕获的ReferenceError:未定义addMarker

Move addMarker definition to the global scope and call it inside iniMap when you know for sure the map is fully loaded and its instance is defined. addMarker定义移至全局作用域,并在确保确定地图已完全加载且已定义其实例时在iniMap调用它。 Also the second parametr title should be a string, not an object. 同样,第二个参数title应该是字符串,而不是对象。

var map;

function initMap() {

    map = new google.maps.Map(document.getElementById('map'), {
     center: {lat: 37.621389, lng: -122.378945},
     zoom: 9
    });
    // now add the marker.
    addMarker ({lat: 37.621389, lng: -122.378945}, 'SFO');
    //_____________________________________________^___^

}

function addMarker(posx, title) {
   var marker = new google.maps.Marker( {
     position: posx,
     map: map,
     title: title
   });
}

Two issues: 两个问题:

  1. You need to call the addMarker function after the map is initialized by initMap . 通过initMap初始化地图 ,需要调用addMarker函数。 The simplest way to do that is to call it inside the initMap function. 最简单的方法是在initMap函数中调用它。

  2. The second argument to addMarker (the marker's title) needs to be a string. addMarker的第二个参数(标记的标题)需要为字符串。

 html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <div id="map"></div> <script> var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: { lat: 37.621389, lng: -122.378945 }, zoom: 9 }); function addMarker(posx, title) { var marker = new google.maps.Marker({ position: posx, map: map, title: title }); } addMarker({lat: 37.621389,lng: -122.378945}, 'SFO'); } </script> <script src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script> 

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

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