【问题标题】:OpenLayers Google Maps Projection Problem w/ KMLOpenLayers Google Maps Projection Problem w/KML
【发布时间】:2009-11-04 01:55:46
【问题描述】:

这是我第一次使用 stackoverflow 并使用 Openlayers 和 Google 地图。

我一直在浏览不同的论坛和网站,包括 OpenLayers.org,以解决我的问题。我已经对以下组合进行了搜索:openlayers、谷歌地图投影和球形墨卡托……但我还没有找到解决方案。

问题:当我放大和缩小地图时,来自 Web 服务调用 (func setDataSource) 的 KML 数据正在发生变化。我的猜测是我的代码中的预测是错误的,或者可能是错误的放置。我对地图投影没有任何背景,因此很难在线消化地图术语:-(。有人可以帮忙吗?

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

谢谢!!!

【问题讨论】:

标签: google-maps openlayers projection proj4js


【解决方案1】:

我发现了问题所在。而不是 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
                })
            })
        });     

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

【讨论】:

  • 我遇到了类似的问题。切换到最新的 OpenLayers 版本后,我不知道在哪里放置 projection 并且没有它,我的 KML 文件全部折叠到 (0,0) 坐标。这个答案为我解决了,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2018-01-05
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多