簡體   English   中英

openlayers 3 ol.proj.transform將動態地理位置點轉換為零

[英]openlayers 3 ol.proj.transform is converting dynamic geolocation points to zero

我正在嘗試使用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數組中的數據時才發生這種魔術,而當數字直接插入語句時卻沒有發生這種魔術的原因。

我正在使用php json編碼json_encode()並在數據中添加引號,因此從經度和緯度值中刪除(修剪)引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM