2009-11-04 84 views
6

這是我第一次在stackoverflow上使用Openlayers &谷歌地圖。OpenLayers谷歌地圖投影問題帶有KML

我一直在瀏覽不同的論壇&網站,包括OpenLayers.org,以解決我的問題。我已經完成了以下組​​合的搜索:openlayers,google map投影和球形mercator ...但我還沒有找到解決方案。

問題:當我放大和縮小地圖時,來自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); 

謝謝!

+3

[GIS.stackexchange.com](http://gis.stackexchange.com/)上的傢伙和女孩可能會更容易地幫助你下次.. – DefenestrationDay 2011-07-07 12:36:57

+0

也許你可以幫助我這個問題 [的OpenLayers谷歌地圖邊框] [1] [1]:http://stackoverflow.com/questions/29120583/how-to-remove-countries-borders-from-a-google-地圖集成在操作員中 – 2015-03-18 14:18:31

回答

7

我想通了這個問題。相反, GML的,我嘗試使用向量,而不是像這樣:

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 :-)希望這有助於任何人瓦特/同樣的問題。

+1

我有一個類似的問題。在切換到最新的OpenLayers版本之後,我不知道在哪裏放置'projection',沒有它,我的KML文件全部摺疊到(0,0)座標。這個答案爲我解決了,謝謝。 – 2012-08-27 14:22:12