views:

1604

answers:

1

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

+5  A: 

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