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

Conditionally update markers on Bing map with checkboxes

我正在尝试更新一个属性,它是一个函数,用于在 bing 地图 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'),

});

提前致谢

我不是 100% 确定,但我怀疑您的 App.COORD 应该是 ember 对象的集合(这将为您提供 2 种方式绑定):

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
  })
];

试一试

几件事:

  1. 您只在地图上绘制图钉一次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