谷歌地图商店定位器修改硬编码初始化为动态

pau*_*aul 5 javascript this typeerror google-maps-api-3

我正在尝试修改此示例 http://storelocator.googlecode.com/git/examples/panel.html

javascript代码在这里:https: //gist.github.com/2725336

我遇到困难的方面是改变这个:

MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(
  new storeLocator.Feature('Wheelchair-YES', 'Wheelchair access'),
  new storeLocator.Feature('Audio-YES', 'Audio')
);
Run Code Online (Sandbox Code Playgroud)

从函数创建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);
    };
Run Code Online (Sandbox Code Playgroud)

所以然后将第一个代码段更改为类似的内容

WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);
Run Code Online (Sandbox Code Playgroud)

返回错误:

Uncaught TypeError: Object [object Window] has no method 'setFeatures_'
Run Code Online (Sandbox Code Playgroud)

Sea*_*key 1

我认为你只需要对WPmmDataSource.prototype你的setFeatures_方法进行一些改变:

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 ) );
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您就不必通过从setFeatures_;返回值来完成分配。它可以直接访问该FEATURES_成员。所以这一行:

WPmmDataSource.prototype.FEATURES_ = this.setFeatures_(wpmm_features);
Run Code Online (Sandbox Code Playgroud)

不再需要了。这也意味着稍后,当您创建 的实例时WPmmDataSource,您的代码可以像这样工作:

var wpmd = new WPmmDataSource( /* whatever options, etc. you want */ );
wpmd.setFeatures_( json );
// Thereafter, wpmd will have its FEATURES_ set
Run Code Online (Sandbox Code Playgroud)

我不太确定你想要实现什么目标,但我相信这会让你克服当前的障碍。我希望这能让你继续前进 -