javascript OpenLayers:自动将 EPSG 4326 -Coordinates 从文本图层转换为地图投影 (EPSG:900913)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11291457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:39:45  来源:igfitidea点击:

OpenLayers: Automatically transform EPSG 4326 -Coordinates from Text Layer into map`s projection (EPSG:900913)

javascriptopenlayersopenstreetmap

提问by Ronny

currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView to present a map which is provided by my own tileserver. I`m using OpenLayers to show this map and now I want to use a Text Layer to show some custom POI`s. Unfortunately, the map uses 90013 projection:

目前,我正在使用 Mono4Droid 实现地图应用程序,并在那里使用 WebView 来呈现由我自己的 tileserver 提供的地图。我正在使用 OpenLayers 来显示此地图,现在我想使用文本层来显示一些自定义 POI。不幸的是,地图使用了 90013 投影:

map = new OpenLayers.Map ("map", {
            controls:[
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoomBar(),
                new OpenLayers.Control.Permalink(),
                new OpenLayers.Control.ScaleLine({geodesic: true}),
                new OpenLayers.Control.Permalink('permalink'),
                new OpenLayers.Control.MousePosition(),                    
                new OpenLayers.Control.Attribution()],
            //maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
            maxResolution: 156543.0339,
            numZoomLevels: 19,
            units: 'm',
            projection: new OpenLayers.Projection("EPSG:900913"),
            displayProjection: new OpenLayers.Projection("EPSG:900913")
        } );

So when I want to display the Markes provided by the TextLayer, I have to use coordinates in the right format. To center the map is easy:

所以当我想显示 TextLayer 提供的 Markes 时,我必须使用正确格式的坐标。使地图居中很容易:

var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            map.setCenter (lonLat, zoom);

But how can I do this transformation automatically for the TextLayer? I want to have the lat lon values according to the well-known EPSG4326 into the Text Layer data file (columns lat long) and not the values according to 900913 (what I`m doing at the moment to have it work).

但是如何为 TextLayer 自动执行此转换?我想将根据众所周知的 EPSG4326 的经纬度值放入文本层数据文件(经纬度列)中,而不是根据 900913 的值(我目前正在做的工作)。

Is there a way to let OpenLayers transform the Text Layer`s coordinates automatically to the format used in the map? Maybe a callback function to override like onShow?

有没有办法让 OpenLayers 将文本图层的坐标自动转换为地图中使用的格式?也许是一个回调函数来覆盖像 onShow?

Thanks for your help! Otherwise I would have to translate the lat lon values myself and put the calculated value in the text file which will make performance to suffer...

谢谢你的帮助!否则我将不得不自己翻译经纬度值并将计算出的值放在文本文件中,这会使性能受到影响......

回答by Ronny

Finally, I found the answer myself and I must say I`m very impressed! OpenLayers is quite powerful! Here is my solution: In the options of the map-object you have to set displayOptions to the projection of your layer, in my case it`s the following to support EPSG:4326:

最后,我自己找到了答案,我必须说我印象非常深刻!OpenLayers 非常强大!这是我的解决方案:在地图对象的选项中,您必须将 displayOptions 设置为图层的投影,在我的情况下,以下内容支持 EPSG:4326:

displayProjection: new OpenLayers.Projection("EPSG:4326")

Then you only have to take care that the layer uses this projection (if it`s not the default value already). In my case, Text Layer didn`t work correctly on Android (in a WebView): I couldn`t open a popup when clicking on the image of a marker from the textfile. Instead, I`m using a Vector-Layer according to the sundials-Example:

然后你只需要注意图层使用这个投影(如果它不是默认值)。就我而言,文本层在 Android 上无法正常工作(在 WebView 中):单击文本文件中的标记图像时,我无法打开弹出窗口。相反,我根据日晷示例使用矢量层:

new OpenLayers.Layer.Vector("TextLayer", {
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "layerdata/chargingstationsmaptxtlayer.txt",
                format: new OpenLayers.Format.Text({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });

In the text-file I can now use the LatLon-values of the EPSG-4326 format. It`s so easy ;-)

在文本文件中,我现在可以使用 EPSG-4326 格式的 LatLon 值。太简单了 ;-)