javascript - 不是 googlemaps api javascript 中的函数错误

标签 javascript function google-maps

我正在使用 google api 在我的一个函数中显示车辆当前位置我有这样的代码

 function fromLatLngToDivPixel(map, latLng) {
    var overlay = new google.maps.OverlayView();
    overlay.draw = function () { };
    overlay.setMap(map);
    var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
    overlay.setMap(null);
    overlay = null
    return point;
}

但是在执行这段代码时,我在 overlay.setMap(null);

上遇到了错误

我无法执行此操作。 错误说

Uncaught TypeError: this.pa.remove is not a function

是的,我知道回答这样的问题非常困难,但如果有人知道这个错误呢?

我附上图片以便更好地引用。 enter image description here

当我评论这一行时,我的代码运行正常,

我可以不使用这个函数吗 -> overlay.setMap(null);

最佳答案

根据 OverlayView 的文档你必须实现 3 个方法:

You must implement three methods: onAdd(), draw(), and onRemove().

  • In the onAdd() method, you should create DOM objects and append them as children of the panes.
  • In the draw() method, you should position these elements.
  • In the onRemove() method, you should remove the objects from the DOM.

您没有实现 onRemove(或 onAdd)方法。

function fromLatLngToDivPixel(map, latLng) {
  var overlay = new google.maps.OverlayView();
  overlay.draw = function() {};
  overlay.onAdd = function() {};
  overlay.onRemove = function() {};
  overlay.setMap(map);
  var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
  overlay.setMap(null);
  overlay = null
  return point;
}

关于javascript - 不是 googlemaps api javascript 中的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50482595/

相关文章:

api - Google map 网站和 API : different results

javascript - 如何关闭使用 browser.windows.create() 打开的窗口?

javascript - 使用 jquery 隐藏和显示覆盖模态

function - Julia 在循环打印消息之前等待函数完成

php - 如何从文件中获取整个函数

javascript - Google map 标记簇总和值

javascript - 来自 Base64 图像的 AngularJS Bootstrap 轮播幻灯片数据

javascript - 使用 ajax 的 Addeventlistener 效果不佳

python - 检查Python中函数的参数

css - 如何在 "print"模式下查看谷歌地图?