如何根据标记类型设置不同颜色的标记,以及如何确定它们的坐标?,我正在使用 openlayers.org 库

how can I set markers with different colours depending on the markertype, and how to determinate their coordinates?, Im using openlayers.org library

我开始使用来自 openlayers.org 的 openlayers javascript 库。

我想为每种设备类型(摄像头标记、服务器标记等)设置不同颜色的动态标记,我尝试了不同的方法来设置它,但它实际上不起作用。 这是我正在开发的地图:http://manotazsoluciones.com/map/.

我面临的另一个问题是设置坐标时。例如:如果我在标记上设置 [0,0] 坐标,当我使用点击事件时,标记会得到另一个坐标,例如 [30000,-7.081154551613622e-10].

这是我用来在 manotazsoluciones 上显示地图的代码。com/map

<!DOCTYPE html>
<html>
<head>
<title>Manotaz Soluciones</title>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.6.0/ol.css" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.6.0/ol.js"></script>

</head>
<body>
<div class="container-fluid">
  <div class="row-fluid">
    <div class="col-md-10 col-md-offset-1">
      <div id="map" class="map">
        <div id="popup">
          <div id="popup-content"></div>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
$( document ).ready(function() {

/**************** DRAG AND DROP EVENTS ****************/
  /**
   * Define a namespace for the application.
   */
  window.app = {};
  var app = window.app;
  /**
   * @constructor
   * @extends {ol.interaction.Pointer}
   */
  app.Drag = function() {

    ol.interaction.Pointer.call(this, {
      handleDownEvent: app.Drag.prototype.handleDownEvent,
      handleDragEvent: app.Drag.prototype.handleDragEvent,
      handleMoveEvent: app.Drag.prototype.handleMoveEvent,
      handleUpEvent: app.Drag.prototype.handleUpEvent
    });

    /**
     * @type {ol.Pixel}
     * @private
     */
    this.coordinate_ = null;

    /**
     * @type {string|undefined}
     * @private
     */
    this.cursor_ = 'pointer';

    /**
     * @type {ol.Feature}
     * @private
     */
    this.feature_ = null;

    /**
     * @type {string|undefined}
     * @private
     */
    this.previousCursor_ = undefined;

  };

  ol.inherits(app.Drag, ol.interaction.Pointer);


  /**
   * @param {ol.MapBrowserEvent} evt Map browser event.
   * @return {boolean} `true` to start the drag sequence.
   */
  app.Drag.prototype.handleDownEvent = function(evt) {
    var map = evt.map;

    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {
          return feature;
        });

    if (feature) {
      this.coordinate_ = evt.coordinate;
      this.feature_ = feature;
    }

    return !!feature;
  };


  /**
   * @param {ol.MapBrowserEvent} evt Map browser event.
   */
  app.Drag.prototype.handleDragEvent = function(evt) {
    var map = evt.map;

    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {
          return feature;
        });

    var deltaX = evt.coordinate[0] - this.coordinate_[0];
    var deltaY = evt.coordinate[1] - this.coordinate_[1];

    var geometry = /** @type {ol.geom.SimpleGeometry} */
        (this.feature_.getGeometry());
    geometry.translate(deltaX, deltaY);

    this.coordinate_[0] = evt.coordinate[0];
    this.coordinate_[1] = evt.coordinate[1];
  };

  /**
   * @param {ol.MapBrowserEvent} evt Event.
   */
  app.Drag.prototype.handleMoveEvent = function(evt) {
    if (this.cursor_) {
      var map = evt.map;
      var feature = map.forEachFeatureAtPixel(evt.pixel,
          function(feature, layer) {
            return feature;
          });
      var element = evt.map.getTargetElement();
      if (feature) {
        if (element.style.cursor != this.cursor_) {
          this.previousCursor_ = element.style.cursor;
          element.style.cursor = this.cursor_;
        }
      } else if (this.previousCursor_ !== undefined) {
        element.style.cursor = this.previousCursor_;
        this.previousCursor_ = undefined;
      }
    }
  };

  /**
   * @param {ol.MapBrowserEvent} evt Map browser event.
   * @return {boolean} `false` to stop the drag sequence.
   */
  app.Drag.prototype.handleUpEvent = function(evt) {
    this.coordinate_ = null;
    this.feature_ = null;
    return false;
  };
  /*************** DRAG AND DROP EVENTS END *************/

你可以忽略上面的拖放事件,因为它工作正常

  var devices = [
    {
      'id' : 1,
      'device' : 'cam',
      'brand' : 'dahua',
      'coordinates' : [0,0]
    },

    {
      'id' : 2,
      'device' : 'cam',
      'brand' : 'vivotes',
      'coordinates' : [0,1]
    },

    {
      'id' : 3,
      'device' : 'cam',
      'brand' : 'dahua',
      'coordinates' : [0, 2]
    },

    {
      'id' : 4,
      'device' : 'rack',
      'brand' : 'dahua',
      'coordinates' : [0, 3]
    }
  ];

上面的代码只是我要展示的资源的一个例子

  var circle = [];
  for (var i = 0; i < devices.length; i++) {

    circle[i] = new ol.Feature(
      new ol.geom.Circle(
        ol.proj.transform(devices[i].coordinates, 'EPSG:4326', 'EPSG:3857'),//usar latitud, longitud, coord sys
        30000
      )
    );
  }

在 var circle 上我保存了每个标记的坐标和大小。

  var styles = [
    new ol.style.Style({
      image: new ol.style.Icon({ //@type {olx.style.IconOptions}
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 1,
        population: 4000,
        rainfall: 500
      }),

      fill: new ol.style.Fill({
        color: [150, 150, 255, 1]
      })
    })
  ];

我在样式中为所有标记设置颜色,但我想根据设备类型更改他的值

  // RENDER DEVICES
  var objects = new ol.source.Vector({ features: circle })
  var bullets = new ol.layer.Vector({
    source : objects,
    style: styles
  });

上面我正在设置标记和样式。

//layers-capaImagen, propiedades imagen principal
  var extent = ol.proj.transformExtent([-50, 50, 50, -40], 'EPSG:4326', 'EPSG:3857');
  var imgProjection = new ol.proj.Projection({
    code: 'xkcd-image',
    units: 'pixels',
    extent: [0, 0, 1024, 968]
  });

  var capaImagen = new ol.layer.Image();
  source = new ol.source.ImageStatic({
    url: 'plano-vertical-knits.jpg',
    imageExtent: extent,
    projection: imgProjection,
    center: ol.extent.getCenter(imgProjection.getExtent()),
    extent: imgProjection.getExtent()
  });

  capaImagen.setSource(source);
  //end propiedades imagen principal



  //map features before render
  var features = {

    controls : ol.control.defaults({attribution : false}).extend([ new ol.control.FullScreen() ]),
    interactions: ol.interaction.defaults().extend([new app.Drag()]),

    layers : [capaImagen, bullets],

    view: new ol.View({ center: [0, 0], zoom: 3 }),

    target : 'map'
  };

  var map = new ol.Map(features);

以上是我用他们的特征渲染地图

  // display popup on click
  var pops = document.getElementById('popup');
  var popupContent = document.getElementById('popup-content');

  var popup = new ol.Overlay({/** @type {olx.OverlayOptions} */ 
    element: pops,
    autoPan: true,
    stopEvent: false,
    positioning: 'bottom-center',
    autoPanAnimation: {
      duration: 250
    }
  });


  map.addOverlay(popup);


  /* events ON map */
  map.on('click', function(evt) {

    var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {

      return feature;
    });

    if (feature) {

      var geometry = feature.getGeometry();
      var firstCoord = geometry.getFirstCoordinate();
      var lastCoord = geometry.getLastCoordinate();

      popup.setPosition(firstCoord);
      $(pops).popover({
        'placement': 'top',
        'html': true,
        'content': feature.get('name')
      });


//var latlong = ol.proj.transform([firstCoord, lastCoord], 'EPSG:4326', 'EPSG:3857');    
      popupContent.innerHTML = '<p>You clicked here:</p><p>'+lastCoord+'</p>';
      $(pops).popover('show');
    }
  });

  // change mouse cursor when over marker
  map.on('pointermove', function(e) {
    if (e.dragging) {

      $('#popup-content').empty();
      return;
    }
  });
  /* events ON map  END */

});
</script>
</body>
</html>

点击函数我试图获取坐标,坐标显示了另一个值。

我提取了您的一些代码并添加了一些修改。

  1. 风格问题

每个功能都需要引用您保存在阵列设备中的属性:

            for (var i = 0; i < devices.length; i++) {
                circle[i] = new ol.Feature(
                        {geometry: new ol.geom.Circle(
                                    ol.proj.transform(devices[i].coordinates, ), 'EPSG:4326', 'EPSG:3857'),//usar latitud, longitud, coord sys
                                    1
                                    ),
                            device: devices[i].device}
                );
            }

此外,需要为每个需要的样式设置不同的样式属性。类似的东西应该可以工作:

var bullets = new ol.layer.Vector({
                source: objects,
                style: function (el, resolution) {//this is a style function
                    console.log(el, el.getProperties().device);
                    if (el.getProperties().device === 'cam')
                        return styles;
                    return styles2;

                }
            });
  1. 坐标有问题 我认为这种情况下的问题是由于投影造成的。您定义了自定义投影(基于像素)并将其应用于图像。对于地图视图,您没有定义任何投影(因此它仍然是默认值 EPSG:3857)。圆的所有坐标由'EPSG:4326'转换为'EPSG:3857'。因此,您在弹出窗口中看到的值在 EPSG:3857 中(不是经度和纬度)。如果您决定继续使用EPSG:3857,您还应该通过以下方式将静态图像适配到此坐标系:

    来源=新ol.source.ImageStatic({ ...............
    图片范围:.....

如您在文档中所见,imageExtent 是图像在地图坐标中的范围。但是在您的代码中 imageExtent 只是图像的像素范围......