javascript - 使用复选框有条件地更新 Bing map 上的标记

标签 javascript ember.js bing-maps

我正在尝试更新一个属性(这是一个函数),以在 bing map API 中设置图钉。

我想固定 bool 属性“setLocation”设置为 true 的项目。如果我对 bool 值进行硬编码,则这是有效的,但如果该值在应用程序中发生更改,则引脚不会在 map 中更新。

这是 html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ember Starter Kit</title>
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v2.0.0.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.9.1/ember.js"></script>
      <script charset="UTF-8" type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
</head>
<body>

  <script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>

    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="index">
    {{#each id in model}}
      {{input type='checkbox' checked=id.setLocation}}Location{{id.id}}
    {{/each}}
    {{bing-map height=590 pins=model}}
  </script>


</body>
</html>

JavaScript:

App = Ember.Application.create();

App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return App.COORD;
  }
});

App.COORD=[
  {
    id:1,
    latitude: 34.05,
    longitude: -118.25,
    setLocation: true
  },
  {
    id:2,
    latitude: 25.77,
    longitude: -80.19,
    setLocation: true
  },
  {
    id:3,
    latitude: 28.53,
    longitude: -81.37,
    setLocation: true
  }
];

App.BingMapComponent = Ember.Component.extend({
  attributeBindings: ['style'],
  classNames: ['bing-map'],
  bingKey: "AkmMkZ5YpX8xsXHY4uBxD8Gz2S5f3GkTRebOw0t4voyb7gFryc0ElW4toY3cJbTt",
  width: '45%',
  height: '100%',
  latitude: 0,
  longitude: 0,
  latitudePin:undefined,
  longitudePin:undefined,
  zoom: 1,
  pins: null, // passed in from controller
  mapTypeId: 'r', // r:road, a:aerial


  init: function(){
    this._super();
    if (!this.get('bingKey')){
      throw('Missing bingKey');
    }
    this.api = Microsoft.Maps;
    this.map = null;
  },

  style: function(){
    return "position: relative; width: %@px; height: %@px".fmt(
      this.get('width'),
      this.get('height')
    );
  }.property('width', 'height'),

  center: function(){
    var latitude  = parseFloat(this.get('latitude'));
    var longitude = parseFloat(this.get('longitude'));
    longitude = this.api.Location.normalizeLongitude(longitude);

    return new this.api.Location(latitude, longitude);
  }.property('latitude', 'longitude'),

  mapOptions: function(){
    return {
      center:      this.get('center'),
      zoom:        parseInt(this.get('zoom'),10),
      mapTypeId:   this.get('mapTypeId')
    };
  }.property('center','zoom','mapTypeId'),

  createMap: function(){
    var el = this.$()[0];
    var options = this.get('mapOptions');
    options.credentials = this.get('bingKey');
    this.map = new Microsoft.Maps.Map(el, options);

    var getPin = this.get('getPin'); 

    for(var i=0; i<getPin.length; i++){
      var pin = new Microsoft.Maps.Pushpin(getPin[i]);
      this.map.entities.push(pin);
    }


  }.on('didInsertElement'),

    getPin: function(){

    var pins = this.get('pins');
    var location=[];

    pins.forEach(function(pin){
      if(pin.setLocation){
        location.push(new Microsoft.Maps.Location(pin.latitude, pin.longitude));
      } 
    }); 
    return location;
  }.property('pins'),

    removeMap: function(){
      this.map.dispose();
    }.on('willDestroyElement'),

});

提前致谢

最佳答案

有几件事:

  1. 您仅在 map 上绘制图钉一次 on('didInsertElement') 这仅在组件第一次在屏幕上呈现时调用。
  2. 由于 pins 是一个数组,因此您需要使用 pins.@each.setLocation 来监视每个元素的 setLocation 属性的更改(参见here)

您可以执行以下操作:

// Observer that calls createMap() any time setLocation changes 
pinObserver: function(){
  this.createMap();
  // NOTICE HOW THIS IS USING @each
}.observes('pins.@each.setLocation'),

getPin: function(){

  var pins = this.get('pins');
  var location=[];

  pins.forEach(function(pin){
    if(pin.setLocation){
      location.push(new Microsoft.Maps.Location(pin.latitude, pin.longitude));
    } 
  }); 
  return location;

  // NOTICE HOW THIS IS USING @each
}.property('pins.@each.setLocation'),

工作演示 here

关于javascript - 使用复选框有条件地更新 Bing map 上的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28300302/

相关文章:

javascript - execCommand中“粘贴为纯文本”的JavaScript技巧

javascript - 在某些情况下我应该如何处理从 Controller 调用模态窗口

javascript - 在 Ember 中使用 promise

ember.js - 生成 ember 应用程序的最佳方法是什么?

javascript - 在 Bing map 上绘制多边形奇怪的行为 setLocations

javascript - 如何使用纯 JavaScript 清除 Microsoft VirtualEarth 6.3 中的整个形状层?

javascript - 使用通用搜索值搜索输入

javascript - 使用 iimPlay 时 iMacro/JS RuntimeError 933

ruby-on-rails - 在 Rails View 中显示 Ember Handlebars 模板

javascript - 从 Bing Phonebook API 获取业务类型/行业