有条理地使用复选框更新Bing地图上的标记

时间:2015-02-03 13:37:42

标签: javascript ember.js bing-maps

我正在尝试更新属性,这是一个函数,用于在bing maps api中设置引脚。

我想固定具有布尔属性的项目" setLocation"设为true。如果我硬编码布尔值,这是有效的,但如果在应用程序中更改了值,则引脚不会在地图中更新。

这是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'),

});

提前致谢

2 个答案:

答案 0 :(得分:1)

有几件事:

  1. 您只在地图上绘制一次图钉on('didInsertElement')这仅在组件首次在屏幕上呈现时调用。
  2. 由于pins是一个数组,因此您需要使用pins.@each.setLocation来监视每个元素setLocation属性的更改(请参阅here
  3. 您可以执行以下操作:

    // 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

答案 1 :(得分:0)

我不是100%肯定,但我怀疑你的App.COORD应该是一个ember对象的集合(它会给你双向绑定):

var coord = Ember.object.extend();

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

给那个旋转

相关问题