【发布时间】:2014-06-17 11:06:11
【问题描述】:
我有一个简单的烧瓶函数,它使用有效的 GeoJSON 字符串呈现模板:
@app.route('/json', methods=['POST'])
def json():
polygon = Polygon([[[0,1],[1,0],[0,0],[0,1]]])
return render_template('json.html',string=polygon)
在我的 json.html 文件中,我尝试使用 OpenLayers 渲染此 GeoJSON:
function init(){
map = new OpenLayers.Map( 'map' );
layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'} );
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
var fc = {{string}}; //Here is the JSON string
var geojson_format = new OpenLayers.Format.GeoJSON();
var vector_layer = new OpenLayers.Layer.Vector();
map.addLayer(vector_layer);
vector_layer.addFeatures(geojson_format.read(fc));
但这失败了," 字符变成了'。我已经尝试过在这个question 中看到的字符串格式,但它没有用。
编辑:
我确实忘记将我的 json 转储到一个实际的字符串中,我正在使用 geojson 库所以添加了函数
dumps(polygon)
解决了这个问题,但是我仍然无法在 OpenLayers 中解析 GeoJSON,即使它是根据 geojsonlint.com 的有效字符串
这是从烧瓶发送的字符串创建变量的 Javascript 代码:
var geoJson = '{{string}}';
下面是它在源页面中的样子:
'{"type": "Polygon", "coordinates": [[[22.739485934746977, 39.26596659794341], [22.73902517923571, 39.266115931275074], [22.738329551588276, 39.26493626464484], [22.738796023230854, 39.26477459496181], [22.739485934746977, 39.26596659794341]]]}';
我在渲染引号字符时仍然遇到问题。
【问题讨论】:
标签: python json flask openlayers geojson