覆盖google.maps.Marker.prototype.setPosition返回错误

时间:2010-08-15 04:00:40

标签: javascript oop google-maps

抓住google.maps.Marker.prototype.setPosition调用

(function(){
 var setPos = google.maps.Marker.prototype.setPosition;
 google.maps.Marker.prototype.setPosition = function(latLng){ 
  console.log(["setPos", latLng]);
  setPos(latLng);
 };
})();

返回错误

this.set is not a function
http://maps.gstatic.com/intl/cs_ALL/mapfiles/api-3/2/0/main.js
Line 28

为什么呢?感谢

3 个答案:

答案 0 :(得分:0)

初始google.maps.Marker.prototype.setPosition将在标记的范围内调用,但通过将其存储在局部变量中,您可以将其范围更改为本地变量。

因此,该函数没有所需的this变量。您可以尝试使用dojo.hitch或阅读this stackoverflow answer

来解决此问题

另外,您应该阅读有关范围和闭包的this

答案 1 :(得分:0)

你可以直接写,

markerObject.setPosition(latlng);

请参阅Marker methods

答案 2 :(得分:0)

您始终可以扩展原型并添加自己的方法。

<div class="row">
<div class="col-xs-12 col-sm-10 col-md-8 col-lg-6">
<table class="table">
....
</table>
</div></div>

这样就可以保持范围不变。

相关问题