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

发布于 2024-11-16 16:57:47 字数 1004 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-11-23 16:57:47

这是正确的解决方案:

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

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

This is the right solution:

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

source: https://gist.github.com/1118357

拥抱影子 2024-11-23 16:57:47

嗨,听起来您需要将长/纬度坐标转换为正确的显示坐标:

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

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

feature.geometry.transform(projWGS84, proj900913);

或者让地图投影“动态”更像这样:

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

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

HTH

C

编辑:

在您的情况下,您需要编写如下内容:

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

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:

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

feature.geometry.transform(projWGS84, proj900913);

Or get the map projection "on the fly" more like this:

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

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:

geojsonData.geometry.transform(projWGS84, map.getProjectionObject());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文