OpenLayers中的地图投影


22

我想在OpenLayers的Google地图图层上叠加一些投影为WGS-84的数据。但是我只是不能把它们放在正确的位置。我做了如下:

map = new OpenLayers.Map('map', {           
        numZoomLevels: 20,
        projection: new OpenLayers.Projection("EPSG:900913"),
        displayProjection: new OpenLayers.Projection("EPSG: 4326")
        });
googlelayer = new OpenLayers.Layer.Google("Google street", {sphericalMercator: true});
map.addLayer(googlelayer);
veclayer = new OpenLayers.Layer.Vector("vector", {
                                    projection: map.displayProjection
                                    };
var geojson_format = new OpenLayers.Format.GeoJSON();
veclayer.addFeatures(geojson_format.read(jsonData));

尽管我已经分配veclayer了4326投影,但是尽管我将displayProjection设置为4326,但它仍被解释为900913,并且显示协调系统也为900913。我会犯什么错误?

Answers:


16

我是“ preFeatureInsert”的忠实粉丝。

var veclayer = new OpenLayers.Layer.Vector("vector", {
           projection: map.displayProjection,
           preFeatureInsert: function(feature) {
           feature.geometry.transform(projWGS84,proj900913);
           }
        });

啊哈,行得通!非常感谢你。但是我不知道该属性的preFeatureInsert含义,无论如何,我在官方API文档中找不到它
ChanDon 2011年

1
就即时消息而言,它会在插入功能之前按定义对投影进行转换...不错且通用,不需要转换函数等
。– CatchingMonkey

6

冒号后面有一个空格。 Projection("EPSG: 4326")应该实际上是Projection("EPSG:4326"),之前没有空间4326


哈哈,真的有用!我的意思是显示投影。但是,覆盖的数据仍然不在正确的位置(与以前相同的错误位置)。有任何想法吗?
2011年

2
               map = new OpenLayers.Map('map',
                { numZoomLevels: 19,
                  units: 'm',
                  eventListeners: {"moveend": update_movend},
                  projection: new OpenLayers.Projection("EPSG:900913"),
                  displayProjection: new OpenLayers.Projection("EPSG:4326")

                });

        OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:900913", OpenLayers.Layer.SphericalMercator.projectForward);
        OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:4326", OpenLayers.Layer.SphericalMercator.projectInverse);


        var layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
        var layerTah = new OpenLayers.Layer.OSM.Osmarender("Osmarender");

        var gphy = new OpenLayers.Layer.Google(
            "Google Physical",
            {
                type: google.maps.MapTypeId.TERRAIN
            }
        );
//        gphy = map.projection;

        var gmap = new OpenLayers.Layer.Google(
            "Google Streets",
            {
                numZoomLevels: 20,
            }
        );
        //gmap = map.projection;

        layerMapnik.projection = map.projection;
        layerTah.projection = map.projection;

        vectors = new OpenLayers.Layer.Vector("Vector Layer");

          var help_layer = new OpenLayers.Layer.Vector("Waypoints", {
             projection: map.displayProjection,
             strategies: [new OpenLayers.Strategy.Fixed()],
         });

我张贴了我的旧代码段。我记得云团在投影中。addTransform应该可以解决您的问题。(proj4)


谢谢您的帮助。但这似乎行不通
ChanDon

1

您可能会落在那儿,因为您正在Google地图上寻找稍微偏移的矢量表示形式,而在其他基础层上却很好,并且将EPSG:4326转换为EPSG:900913或EPSG:3857并不能解决所有问题。

如果尚未显示OpenLayers Map对象,则Google Maps上的一个错误无法很好地评估div大小。因此,在调用transform()和redraw()之后,您可能需要myMapObject.updateSize();使Google知道屏幕上地图的实际比例。

如果您正在寻找将图层从一个投影转换为另一个投影的通用解决方案,则可以使用以下功能:

var myMap = new OpenLayers.Map('mapDiv');

function mapBaseLayerChanged(evtObj) {
  var mapProj, baseProj, map_this, newBase;
  map_this = this;
  newBase = evtObj.layer;
  mapProj = (map_this.projection && map_this.projection instanceof OpenLayers.Projection) ?
            map_this.projection : new OpenLayers.Projection(map_this.projection);
  baseProj = newBase.projection;
  if (!(baseProj.equals(mapProj))) {
     var center, maxExt;
     center = map_this.getCenter().transform(mapProj, baseProj);
     maxExt = newBase.maxExtent;
     map_this.projection = baseProj;
     map_this.maxExtent = maxExt;
     map_this.setCenter(center, map_this.getZoom(), false, true);
     // Transforms all sub-layers
     for (var i = 0; i < map_this.layers.length; i++) {
        if (map_this.layers[i].isBaseLayer == false) {
           map_this.layers[i].addOptions({projection: baseProj}, true);
           // Transforms all features of the vectors
           for (var j = 0; j < map_this.layers[i].features.length; j++) {
              map_this.layers[i].features[j].geometry.transform(mapProj, baseProj);
           }
           map_this.layers[i].redraw();
        }
     }
     //Solves Google's problem (and mine at the same occasion)
     map_this.updateSize();
  }
}
myMap.events.register('changebaselayer', myMap, mapBaseLayerChanged);

等等!


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.