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

Conditionally update markers on Bing map with checkboxes

本文关键字:地图 Bing 更新 复选框 有条件      更新时间:2023-09-26

我正在尝试更新一个属性,这是一个函数,以在必应地图 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'),
});

提前致谢

有几件事:

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

您可以执行以下操作:

// 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'),

在这里工作演示

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

试一试