2016-06-09 35 views
0

我想一個是MultiPolygon負載從SQL地理傑森對象和它不工作3負載幾何形狀..的OpenLayers從SQL

是什麼工作(這將創建幾何對象)...

var geoJsonObj = { 
    'type': 'Feature', 
    'geometry': { 
     "coordinates": [ 
      [[[-91.0759333619999, 40.15440933399983], 
       [-91.066378752, 40.154309680999823], 
       [-91.066282352, 40.157927062999832], 
       [-91.0751007809999, 40.157994385999814], 
       [-91.0758658189999, 40.157997289999805], 
       [-91.075866624, 40.157608482999827], 
       [-91.0758737049999, 40.157300970999813], 
       [-91.0759333619999, 40.15440933399983]]] 
     ] 
     , "type": "MultiPolygon" 
    } 

}; 

什麼行不通......

var geoJsonObj = { 
    'type': 'Feature', 
    'geometry': webMapValues.geometry 
}; 

其中webMapValues.geometry從SQL填充和具有的價值...

"{ 
"coordinates": 
[[[ 
[-91.0759333619999,40.15440933399983], 
[-91.066378752,40.154309680999823], 
[-91.066282352,40.157927062999832], 
[-91.0751007809999,40.157994385999814], 
[-91.0758658189999,40.157997289999805], 
[-91.075866624,40.157608482999827], 
[-91.0758737049999,40.157300970999813], 
[-91.0759333619999,40.15440933399983] 
]]] 
,"type":"MultiPolygon"}" 

注意唯一的區別是SQL加載的變量中的值在「」引號內。

我試過了一些「格式」的解決方案,但似乎跑到了死衚衕。

任何幫助,非常感謝!

回答

0

實際的答案是使用JSON.parse這樣......

JSON.parse(response.FieldList[key].Shape) 

從SQL回來結構已經適當GeoJSON的對象託比斯佩特的觀點和OL3需要一個字符串我猜。

0

您需要將JSON字符串解析爲一個對象,然後您可以將它與geoJSON對象框合併並添加到OpenLayers。

var geomStr = '{"coordinates":[[[[-91.0759333619999, 40.15440933399983],[-91.066378752, 40.154309680999823],[-91.066282352, 40.157927062999832],[-91.0751007809999, 40.157994385999814],[-91.0758658189999, 40.157997289999805],[-91.075866624, 40.157608482999827],[-91.0758737049999, 40.157300970999813],[-91.0759333619999, 40.15440933399983]]]],"type":"MultiPolygon"}'; 
 

 
var geoJson = { 
 
    "type": "FeatureCollection", 
 
    "features": [{ 
 
     "type": "Feature", 
 
     "geometry": JSON.parse(geomStr) 
 
    }] 
 
}; 
 

 
var vectorLayer = new ol.layer.Vector({ 
 
    source: new ol.source.Vector({ 
 
     features: new ol.format.GeoJSON().readFeatures(geoJson, { 
 
      dataProjection: 'EPSG:4326', 
 
      featureProjection: 'EPSG:3857' 
 
     }) 
 
    }) 
 
}); 
 

 
var map = new ol.Map({ 
 
    target: 'map', 
 
    controls: [], 
 
    layers: [new ol.layer.Tile({ 
 
     source: new ol.source.OSM() 
 
    }), vectorLayer], 
 
    view: new ol.View({ 
 
     center: [0, 0], 
 
     zoom: 10 
 
    }) 
 
}); 
 

 
map.getView().fit(
 
    vectorLayer.getSource().getExtent(), 
 
    map.getSize());
html, body { 
 
    margin: 0; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 

 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.16.0/ol.css" rel="stylesheet"/> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.16.0/ol.js"></script> 
 
<div id="map"></div>

+0

黨...只是看到你到了那裏,而我是用代碼片段播放。 –