简体   繁体   English

Javascript向google.maps.Map添加功能

[英]Javascript adding functions to google.maps.Map

I'm having a little difficulty adding two functions, one which calls the other, to the google.maps.Map object 我在将两个函数(其中一个调用另一个函数)添加到google.maps.Map对象时遇到了一些困难

google.maps.Map.prototype.addMarkerFromJson = function() {
    alert("hi");
}

google.maps.Map.prototype.getPictureDataFromJson = function (jsonObj) {
    this.addMarkerFromJson();
}

This code throws an error: 此代码引发错误:

Uncaught TypeError: Object #<Object> has no method 'addMarkerFromJson'

which is puzzling in itself, what made me even more confused is when I changed the names: 本身令人感到困惑,更使我困惑的是当我更改名称时:

google.maps.Map.prototype.anotherTestFunction = function() {
    alert("hi");
}

google.maps.Map.prototype.aTestFunction = function (jsonObj) {
    this.anotherTestFunction();
}

This still doesn't work (the alert doesn't get fired) but it also doesn't raise an error in console? 这仍然不起作用(不会触发警报),但是也不会在控制台中引发错误?

Would love to know exactly what I'm doing wrong here as well as the explaination for the wierdness! 很想知道我在这里做错了什么,以及对怪异的解释!

Edit: As I look at it again, I'm beginning to think that the this in this.addMarkerFromJson(); 编辑:再次查看时,我开始认为this.addMarkerFromJson();中的this this.addMarkerFromJson(); actually refers to the function() definition and not the Map object that I'm attaching it to? 实际引用的是function()定义,而不是我将其附加到的Map对象? In which case how would I go about calling the other function? 在这种情况下,我将如何调用另一个函数?

Answering my own question: 回答我自己的问题:

This was me being stupid! 这是我的愚蠢! I was using mapObject.getPictureDataFromJson() as the success callback for a jquery.get(), so this referred to the caller (the get function) rather than the map object. 我使用mapObject.getPictureDataFromJson()作为jquery.get()的成功回调,因此this指调用方(get函数)而不是map对象。

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

相关问题 Google.maps.Map Javascript API V3与jquery兼容 - Google.maps.Map Javascript API V3 compatibilty with jquery google.maps.Map() 已使用但未加载数据,是否会产生费用? - google.maps.Map() used but no data loaded, does that incur charges? 如何使用coffeescript扩展google.maps.Map - How can I extend google.maps.Map with coffeescript 从HTMLElement获取google.maps.Map实例 - Get google.maps.Map instance from a HTMLElement 如何使用react-google-maps访问google.maps.Map对象 - How to access google.maps.Map object with react-google-maps 如何使用google.maps.Map结果渲染Google地图 - How can I render google map with google.maps.Map result 为什么这不是 google.maps.Map 的地图实例? InvalidValueError: setMap: 不是 Map 的一个实例; - Why isn't this an instance of map of google.maps.Map? InvalidValueError: setMap: not an instance of Map; new google.maps.Map(document.getElementById('agmmap'),{}) 导致 agm map 消失 - new google.maps.Map(document.getElementById('agmmap'),{}) causes agm map to disappear 使用ng-repeat(angularJs)创建多个google.maps.Map对象 - create multiple google.maps.Map object with ng-repeat (angularJs) 如何在保留先前设置的同时设置google.maps.Map选项? - How to set google.maps.Map options while preserving the previous settings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM