首页 文章

OpenLayers:解析的GeoJSON点始终显示在coords(0,0)

提问于
浏览
4

这是我第一次使用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

2 回答

  • 7

    这是正确的解决方案:

    var geojson_format = new OpenLayers.Format.GeoJSON({
                    'internalProjection': new OpenLayers.Projection("EPSG:900913"),
                    'externalProjection': new OpenLayers.Projection("EPSG:4326")
                });
    

    来源:https://gist.github.com/1118357

  • 3

    嗨,听起来你需要将long / lat coordinaites转换为正确的显示坐标:

    您可以声明投影,然后转换几何体特征:

    var projWGS84 = new OpenLayers.Projection("EPSG:4326");
    var proj900913 = new OpenLayers.Projection("EPSG:900913");
    
    feature.geometry.transform(projWGS84, proj900913);
    

    或者“动态”获取 Map 投影更像是这样的:

    var projWGS84 = new OpenLayers.Projection("EPSG:4326");    
    feature.geometry.transform(projWGS84, map.getProjectionObject());
    

    显然,如果您使用的是我不同的输入投影,请将“ESPG:4326”更改为您需要的任何内容 .

    HTH

    C

    编辑:

    在你的情况下,你需要写如下:

    geojsonData.geometry.transform(projWGS84, map.getProjectionObject());
    

相关问题