6

This is my first time on stackoverflow and working with Openlayers & Google Maps.

I've been browsing different forums & sites, including OpenLayers.org, to solve my issue. I've done searches on a combination of the following: openlayers, google map projections, and spherical mercator... but I have not found a solution.

Problem: The KML data from a web service call (func setDataSource) is shifting as I zoom in and out of the map. My guess is that the projections in my code are wrong or perhaps wrongly placed. I don't have any background on map projections so it is difficult to digest mapping terminology online :-(. Can someone help?

   //start here
   var options = {
   projection: new OpenLayers.Projection("EPSG:900913"),
   displayProjection: new OpenLayers.Projection("EPSG:4326"),
   units: "m",
   numZoomLevels: 18,
              maxResolution: 156543.0339,
              maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
                                              20037508, 20037508)}; 

   //*map = new OpenLayers.Map('map');

     map = new OpenLayers.Map('map', options);

    var gphy = new OpenLayers.Layer.Google(
             "Google Street",
    {'sphericalMercator':true});

   // Add the background images via WMS
  var bglayer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
               "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'}, {'reproject': true});

  //map.addLayer(bglayer);
    map.addLayers([gphy, bglayer]);
  map.addControl(new OpenLayers.Control.LayerSwitcher());

   map.zoomToMaxExtent(); //* Zoom all the way out, this command also initalizes the map
  OpenLayers.Console.log("initialized");
    }

function setDataSource() {
 OpenLayers.Console.log("Setting data source to " + OpenLayers.Util.getElement('loc').value);
 if (layer != undefined) {map.removeLayer(layer)};
 if (selectControl != undefined) {map.removeControl(selectControl)};

 // Encode the destination url as a parameter string.
 var params = OpenLayers.Util.getParameterString({url:OpenLayers.Util.getElement('loc').value})

 //  Make the http request to the transformer, with the destination url as a parameter.
 layer = new OpenLayers.Layer.GML("KML", transformerURL + params, 
           {
            format: OpenLayers.Format.KML, 
            formatOptions: {
            extractStyles: true, 
            extractAttributes: true,
  maxDepth: 2,

  //projection: new OpenLayers.Projection("EPSG:4326"),
             }
             });
  map.addLayer(layer); 

Thank you!!!

winwaed
  • 7,645
  • 6
  • 36
  • 81
Tiffany
  • 151
  • 2
  • 7
  • 3
    The guys and girls over at [GIS.stackexchange.com](http://gis.stackexchange.com/) would probably find it easier to help you next time.. – DefenestrationDay Jul 07 '11 at 12:36
  • maybe you can help me with this poblem [Openlayers Google Map Borders][1] [1]: http://stackoverflow.com/questions/29120583/how-to-remove-countries-borders-from-a-google-map-integrated-in-opnelayers – Ouadie El Khabbaz Mar 18 '15 at 14:18

1 Answers1

7

I figured out the problem. Instead of GML, I tried using Vector instead like this:

layer = new OpenLayers.Layer.Vector("KML", {
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: transformerURL + params,
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });     

I found the solution in this sundials example: http://openlayers.org/dev/examples/sundials-spherical-mercator.html :-) Hope this helps anyone w/ the same problem.

Tiffany
  • 151
  • 2
  • 7
  • 1
    I had a similar problem. After switching to the latest OpenLayers version, I didn't know where to place `projection` and without it, my KML files were all collapsed to the (0,0) coordinate. This answer solved it for me, thanks. – Petr Aug 27 '12 at 14:22