我正在使用 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
是的,我知道回答这样的问题非常困难,但如果有人知道这个错误呢?
当我评论这一行时,我的代码运行正常,
我可以不使用这个函数吗 -> 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/