Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to overlay some data whose projection is WGS-84 on Google map layer in OpenLayers. But I just can't make them in the right place. I did as follows:

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

Though I have assigned veclayer in 4326 projection, but it is still interpreted as 900913, and the display coordination system is also 900913, though I set displayProjection to 4326. What mistake do I make?

share|improve this question

5 Answers

up vote 11 down vote accepted

Im a big fan of "preFeatureInsert"....

veclayer = new OpenLayers.Layer.Vector("vector", {
           projection: map.displayProjection,
           preFeatureInsert: function(feature) {
           feature.geometry.transform(projWGS84,proj900913);
           }
              }; 
share|improve this answer
Aha, it works! Thank you very much. But I wonder what the property preFeatureInsert means, anyhow, I can't find it in the official API doc~ – ChanDon Nov 18 '11 at 11:06
1  
As far as im concerned, it transforms the projections as defined, before the feature is inserted... Nice and generic, and no need for conversion functions etc. – ASPMapper Nov 18 '11 at 11:12

You have a space after the colon. Projection("EPSG: 4326") should actually be Projection("EPSG:4326"), no space before 4326.

share|improve this answer
Haha it really works! I mean for the display projection. However, the overlaid data is still not in the right place (the same wrong place as before). Any ideas? – ChanDon Nov 18 '11 at 6:11

I think you may need to use proj4 http://trac.osgeo.org/proj/ for reproject...

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer: please explain why you're recommending it as a solution. Answers that don't explain anything will be deleted. See Good Subjective, Bad Subjective for more information.

               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()],
         });

I posted my old code snippet. I remember that the clou is in the projections. addTransform should solve your problem. (proj4)

share|improve this answer
Thank you for your help. But it seems not to work – ChanDon Nov 18 '11 at 11:07

For Yahoo you can try these options:

baseLayerOptions:{
  sphericalMercator: true,
  maxExtent:new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34)
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.