admin管理员组

文章数量:1310034

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

.png

What can I do?

Thanks in advance

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./34y40zk.png

What can I do?

Thanks in advance

Share Improve this question asked May 31, 2012 at 22:40 Naty BizzNaty Bizz 2,3427 gold badges35 silver badges56 bronze badges 1
  • naty chica, fijate, i would be very wary of using any google mapping interface professionally. they have been steadily clamping down and restricting free use of their API and other resources. i just threw away 6 months of coding because they changed their licensing (again) and wanted to charge me a minimum of 18 thousand US$ per year. there is NO guarantee that whatever tiling you are using will still be free of charge in the near future. i strongly advise you to contact them (in brazil, they are represented by apontador..br) BEFORE investing your time in developing ANYTHING. – tony gil Commented Jun 4, 2012 at 21:17
Add a ment  | 

2 Answers 2

Reset to default 6

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

本文标签: javascriptProjections and OpenLayersGeometryPoint in openlayersStack Overflow