【问题标题】:Yandex Maps API: add multiple placemarksYandex Maps API:添加多个地标
【发布时间】:2019-07-15 16:09:18
【问题描述】:

我了解如何创建一个地标,如果我需要创建多个地标,我会这样做:

ymaps.ready(init);

function init() {   

  var map = new ymaps.Map('map', {
    center: [55.76, 37.64], // lat, long
    zoom: 5,
    controls: ['zoomControl', 'fullscreenControl']
  });

  var data = [
    {
      name: 'Moskow',
      coordinates: [55.684758, 37.738521]
    },
    {
      name: 'Saint Petersburg',
      coordinates: [59.939095, 30.315868]
    },
  ];

  for (var i = 0; i < data.length; i++) {
    map.geoObjects.add(new ymaps.Placemark([data[i]['coordinates'][0], data[i]['coordinates'][1]], {
        balloonContent: data[i]['name']
    }));
  }

}

但是有没有更好的方法可以在没有循环的情况下将一堆数据添加到地图中?

【问题讨论】:

    标签: javascript yandex-maps


    【解决方案1】:

    是的,使用对象管理器并将您的数据组织到对象集合中。 Yandex Maps API 为这种情况提供了便捷的方法。

    这是根据您的数据的简单示例:

    ymaps.ready(init);
    
    function init() {
    
      var map = new ymaps.Map('map', {
        center: [55.76, 37.64], // lat, long
        zoom: 5,
        controls: ['zoomControl', 'fullscreenControl']
      });
    
      // Objects collection
      var collection = {
        type: "FeatureCollection",
        features: [
          {
            type: "Feature",
            id: 0,
            geometry: {
              type: "Point",
              coordinates: [55.684758, 37.738521]
            },
            properties: {
              balloonContent: "Moskow"
            }
          },
          {
            type: "Feature",
            id: 1,
            geometry: {
              type: "Point",
              coordinates: [59.939095, 30.315868]
            },
            properties: {
              balloonContent: "Saint Petersburg"
            }
          }
        ]
      };
    
      // Object Manager
      objectManager = new ymaps.ObjectManager({ clusterize: true });
      objectManager.add(collection);
    
      map.geoObjects.add(objectManager);
    
    }
    

    了解Object Manager。在那里您可以找到更多示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      相关资源
      最近更新 更多