简体   繁体   中英

Projections and OpenLayers.Geometry.Point in openlayers

I'm trying to show a map with three layers (google maps layers, wms layer and points layer) this is my code:

     var map = new OpenLayers.Map({
         div: "map",
         maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508.34)
     });


     var capaGoogle = new OpenLayers.Layer.Google(
                "Google Satellite",
                { type: G_SATELLITE_MAP, sphericalMercator: true, transparent: true }
            );

     var wmsOverlay = new OpenLayers.Layer.WMS("OpenLayers WMS",
         "http://localhost:1979/geoserver/wms",
         { layers: 'world:PYCIUDADES', transparent: true }, { isBaseLayer: false });


     var vectorLayer = new OpenLayers.Layer.Vector("vector");

     vectorLayer.addFeatures([
        new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(-57.635021, -25.276987)

        ),
        new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(-56.759034, -22.71539)
        )

        ]

    );

     map.addLayers([wmsOverlay, vectorLayer, capaGoogle]);

     map.addControl(new OpenLayers.Control.LayerSwitcher());
     var center = new OpenLayers.LonLat(-57.58, -25.27).transform(
     new OpenLayers.Projection("EPSG:4326"),
     map.getProjectionObject()
     )
     map.setCenter(center, 6);

the "vectorLayer" layer must be above of my map, but I get this (my wms layer is in south america, my points have to be also in south america, but they're near africa):

http://i45.tinypic.com/34y40zk.png

What can I do?

Thanks in advance

You should to transform your point's coordinates:

var epsg4326 = new OpenLayers.Projection('EPSG:4326');
var epsg900913 = new OpenLayers.Projection('EPSG:900913');

vectorLayer.addFeatures([
    new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(-57.635021, -25.276987).transform(epsg4326, epsg900913)
    ),
    new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(-56.759034, -22.71539).transform(epsg4326, epsg900913)
    )
]);

"next to Africa" = longitude 0 latitude 0

when you simply enter Longitude and Latitude, OL considers this a WGS84 projection (standard).

but since you are using a Google layer (CapaGoogle), which is a mercator-referenced projection, you end up using two reference projections (4326 = WGS84 AND 900913 = mercator) simultaneously and the map server disconsiders the location of your markers, since they are "incorrect". therefore, it simply places them at (0,0).

as DrNextGIS said, you must "transform your points coordinates" so that everything on your map uses the same projection.

you would NOT have this problem if you were using a simple OpenLayer map (without Google or OSM).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM