OpenLayers:解析的 GeoJSON 点始终显示在 coords(0 , 0) 处
这是我第一次使用 OpenLayers,我不明白我做错了什么。
我尝试显示从 GeoJSON 解析的一个简单点。数据似乎被正确解析(我用控制台检查),但无论我给出什么点,它总是显示在我猜想的向量层上的 LonLat(0,0) 位置。
我做错了什么?
var map, baseLayer, placesLayer, geojsonParser ; // data below have been simplified and reformated to enhance readability var geojsonData = { "type":"Feature", "geometry": { "type":"Point", "coordinates":[-4.0280599594116,5.3411102294922] }, "properties": { "id":273, "name":"ABIDJAN" } }; $(document).ready(function(){ map = new OpenLayers.Map('map'); baseLayer = new OpenLayers.Layer.OSM(); placesLayer = new OpenLayers.Layer.Vector(); geojsonParser = new OpenLayers.Format.GeoJSON(); placesLayer.addFeatures(geojsonParser.read(geojsonData)); map.addLayers([baseLayer,placesLayer]); map.setCenter( new OpenLayers.LonLat(-4, 5.3).transform( new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject() ), 5 ); }); // document ready
this is the first time i use OpenLayers and i don't understand what i'm doing wrong.
I try to display a simple point parsed from GeoJSON. The data seems to be parsed correctly (i checked with the console) but whatever point i give, it always displays at a position i guess to be LonLat(0,0) on my vector layer.
What am i doing wrong ?
var map, baseLayer, placesLayer, geojsonParser ; // data below have been simplified and reformated to enhance readability var geojsonData = { "type":"Feature", "geometry": { "type":"Point", "coordinates":[-4.0280599594116,5.3411102294922] }, "properties": { "id":273, "name":"ABIDJAN" } }; $(document).ready(function(){ map = new OpenLayers.Map('map'); baseLayer = new OpenLayers.Layer.OSM(); placesLayer = new OpenLayers.Layer.Vector(); geojsonParser = new OpenLayers.Format.GeoJSON(); placesLayer.addFeatures(geojsonParser.read(geojsonData)); map.addLayers([baseLayer,placesLayer]); map.setCenter( new OpenLayers.LonLat(-4, 5.3).transform( new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject() ), 5 ); }); // document ready
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的解决方案:
来源:https://gist.github.com/1118357
This is the right solution:
source: https://gist.github.com/1118357
嗨,听起来您需要将长/纬度坐标转换为正确的显示坐标:
您可以声明投影,然后转换您的几何特征:
或者让地图投影“动态”更像这样:
显然,如果您是使用我的不同输入投影将“ESPG:4326”更改为您需要的任何内容。
HTH
C
编辑:
在您的情况下,您需要编写如下内容:
Hi it sounds like you need to transform the long/lat coordinaites into the correct display coordinates:
You can either declare the projections and then transform your geometry feature:
Or get the map projection "on the fly" more like this:
Obviously if you are using a different input projection from me change "ESPG:4326" to whatever you require.
HTH
C
EDIT:
In your case you would need to write something like: