openlayers 3 ol.proj.transform正在将动态地理位置点转换为零

时间:2017-10-22 19:40:20

标签: javascript arrays json openlayers-3

我正在尝试使用stackoverflow中的示例,使用openlayers 3. *将动态数据插入到地图中。 问题是:当我将数字直接插入语句时,代码运行正常,如下所示:

 geometry: new ol.geom.Point(ol.proj.transform([-72.0704,46.678],'EPSG:4326','EPSG:3857')),

但当我尝试按照以下代码模拟json数据时:发生了一些魔术:

  • 当只替换经度时,我得到了右侧的标记 位置,
  • 当仅替换纬度时,纬度将转换为零,
  • 当替换经度和纬度时,标记将不会显示 起来。

    function AddMarkers() {
    var iconFeatures=[];
    data  = [{"longitude":"-72.0704","latitude":"46.678"},{"longitude":"-73.1234","latitude":"45.678"},{"longitude":"46.738586","latitude":"24.774265"}];
    
    var iconFeature = new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.transform([data[0].longitude,46.678], 'EPSG:4326',     
      'EPSG:3857')),
      name: 'Null Island',
      population: 4000,
      rainfall: 500
    });
    
    var iconFeature1 = new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.transform([28.7753927 , data[1].latitude], 'EPSG:4326',     
      'EPSG:3857')),
      name: 'Null Island Two',
      population: 4001,
      rainfall: 501
    });
    var iconFeature2 = new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.transform([data[2].longitude,data[2].latitude], 'EPSG:4326',     
      'EPSG:3857')),
      name: 'Null Island Two',
      population: 4002,
      rainfall: 502
    });
    iconFeatures.push(iconFeature);
    iconFeatures.push(iconFeature1);
    iconFeatures.push(iconFeature2);
    
    var vectorSource = new ol.source.Vector({
      features: iconFeatures //add an array of features
    });
    
    var iconStyle = new ol.style.Style({
      image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.75,
        src: 'images/icon.png'
      }))
    });
    
    
    var vectorLayer = new ol.layer.Vector({
      source: vectorSource,
      style: iconStyle
    });
    
    return vectorLayer;
    

    }

那么,为什么只有在使用json数组中的数据时才会发生这种魔法,但是当数字直接插入到语句中时就不会发生。

1 个答案:

答案 0 :(得分:0)

我正在使用php json编码json_encode()并为数据添加引号,因此从经度和纬度值中删除(修剪)引号。