最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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:

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:

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).

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?

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...

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:

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:

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).

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?

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...

Share Improve this question asked Jul 2, 2012 at 10:00 RonnyRonny 1661 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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:

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:

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 ;-)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论