【问题标题】:Attaching custom ID with pushpin in Bing maps在 Bing 地图中使用图钉附加自定义 ID
【发布时间】:2012-12-04 23:06:37
【问题描述】:

我想将一个唯一的 ID 号(取自 DB)添加到我放在 Bing 地图上的图钉中。 当我不使用任何主题(如 Bing 主题)时,它可以完美运行,如下所示:

var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lon), 
{icon: some_icon, width: 34, height: 43});

pushpin.customid = 13;
map.entities.push(pushpin);

然后我可以稍后通过 Pushpin 对象访问这个“customid”属性。

但是,当我激活 Bing 主题时,一切正常,除了这些对我的应用程序至关重要的自定义值。

关于将某些唯一 ID 附加到这些图钉的其他方法的任何想法,或解决此问题的想法?

【问题讨论】:

    标签: bing-maps pushpin


    【解决方案1】:

    只需像这样向图钉添加自定义属性:

    pushpin.MyID = '一些 id';

    然后您可以使用此博文中描述的方法通过 id 检索图钉:http://rbrundritt.wordpress.com/2012/04/02/get-entity-by-property-in-bing-maps-v7/

    【讨论】:

      【解决方案2】:

      我使用一个普通的 JavaScript 对象来包装 Pushpin 及其功能,因此我没有将自定义属性存储在 Pushpin 上,而是将它们存储在包装器对象上。

      例如,假设我们指定了地图和位置,然后我们可以创建我们的对象:

      var Pin = function(location, map) {
          this._pushpin = new Microsoft.Maps.Pushpin(location);
          map.entities.push(this._pushpin);
          this.id = (new Date).valueOf();
          var self = this;
          Microsoft.Maps.Events.addHandler(this._pushpin, 'click', function() {
              console.log(self.id);
          })
      };
      

      这样,当我们稍后需要引用图钉的 id 时,例如当点击图钉时,我们可以获取包装对象的 id。

      使用公共 API 扩展 Bing 地图对象是一种很好的做法。

      写了一篇关于它的博文here

      【讨论】:

        【解决方案3】:

        您可以使用 pushpin.getId() 默认方法并重新定义它以返回 custid,而不是使用 pushpin.custid 属性

        var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lon), 
                                                {icon: some_icon, width: 34, height: 43});
        
        var custid=13;
        pushpin.getId = function (){
            return custid;
        }
        map.entities.push(pushpin);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多