Javascript google地图商店定位器将硬编码初始化修改为动态
我试图修改这个例子 javascript代码如下所示: 我遇到的困难是改变这一点:Javascript google地图商店定位器将硬编码初始化修改为动态,javascript,google-maps-api-3,this,typeerror,Javascript,Google Maps Api 3,This,Typeerror,我试图修改这个例子 javascript代码如下所示: 我遇到的困难是改变这一点: MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet( new storeLocator.Feature('Wheelchair-YES', 'Wheelchair access'), new storeLocator.Feature('Audio-YES', 'Audio') ); 从函数创建FeatureSet,
MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(
new storeLocator.Feature('Wheelchair-YES', 'Wheelchair access'),
new storeLocator.Feature('Audio-YES', 'Audio')
);
从函数创建FeatureSet,例如,我有一个解析JSON对象的函数
WPmmDataSource.prototype.setFeatures_ = function(json) {
var features = [];
// convert features JSON to js object
var rows = jQuery.parseJSON(json);
// iterate through features collection
jQuery.each(rows, function(i, row){
var feature = new storeLocator.Feature(row.slug + '-YES', row.name)
features.push(feature);
});
return new storeLocator.FeatureSet(features);
};
然后将第一个代码段更改为
WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);
返回一个错误:
Uncaught TypeError: Object [object Window] has no method 'setFeatures_'
我认为您只需对
wpmmdasource.prototype
和您的setFeatures\uu
方法进行一些更改:
WPmmDataSource.prototype = {
FEATURES_ : null,
setFeatures_ : function( json ) {
//Set up an empty FEATURES_ FeatureSet
this.FEATURES_ = new storeLocator.FeatureSet();
//avoid the use of "this" within the jQuery loop by creating a local var
var featureSet = this.FEATURES_;
// convert features JSON to js object
var rows = jQuery.parseJSON( json );
// iterate through features collection
jQuery.each( rows, function( i, row ) {
featureSet.add(
new storeLocator.Feature( row.slug + '-YES', row.name ) );
});
}
}
这样,您就不必通过从setFeatures_uu
返回值来进行赋值了;它可以直接访问功能
成员。所以这句话:
WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);
不再需要了。这也意味着,稍后,当您创建了wpmmdasource
的实例时,您的代码可以如下工作:
var wpmd = new WPmmDataSource( /* whatever options, etc. you want */ );
wpmd.setFeatures_( json );
// Thereafter, wpmd will have its FEATURES_ set
我不确定你想要达到什么,但我相信这会让你克服目前的障碍。我希望这能让您向前迈进-我在这里提供了一个演示:您可以在ConsoleAnks中看到错误消息,但是功能需要能够被类方法访问,因为您可以看到它被用来在parse函数中将功能分配给存储。如果我使用您的代码,features.add(this.features_u2;.getById('轮椅-'+行.轮椅));函数在getById上引发错误,因为未定义特性。有什么建议吗?当然有。很乐意帮忙。我已经对上面的代码进行了调整,以正确创建
功能,并确保wpmmdasource
JavaScript类中的所有方法都可以使用它。谢谢Sean,在您的帮助下,我终于找到了答案。感谢你的努力。我只需要将特性保存到parse函数中的一个变量中,就可以让它最终工作。