简体   繁体   English

OpenLayers谷歌地图投影问题与KML

[英]OpenLayers Google Maps Projection Problem w/ KML

This is my first time on stackoverflow and working with Openlayers & Google Maps. 这是我第一次使用stackoverflow并使用Openlayers和Google Maps。

I've been browsing different forums & sites, including OpenLayers.org, to solve my issue. 我一直在浏览不同的论坛和网站,包括OpenLayers.org,以解决我的问题。 I've done searches on a combination of the following: openlayers, google map projections, and spherical mercator... but I have not found a solution. 我已经完成了以下组​​合的搜索:openlayers,谷歌地图投影和球形墨卡托...但我还没有找到解决方案。

Problem: The KML data from a web service call (func setDataSource) is shifting as I zoom in and out of the map. 问题:当我放大和缩小地图时,来自Web服务调用(func setDataSource)的KML数据正在移动。 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!!! 谢谢!!!

I figured out the problem. 我解决了这个问题。 Instead of GML, I tried using Vector instead like this: 而不是GML,我尝试使用Vector,而不是这样:

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. 我在这个日程示例中找到了解决方案: http//openlayers.org/dev/examples/sundials-spherical-mercator.html :-)希望这可以帮助任何人解决同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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