2

サーバーから取得した KML ファイルを OpenLayers に表示させようとしています。何らかの理由でこれは機能しません。同様の質問が寄せられていますが、実際の例は見つかりませんでした。私がしたことは、OpenLayers ディストリビューションの例の 1 つを改善することでした: kml-track.js

私が見つけたものでそれを改善しました。これは、それがどのように見えるかです。明らかな何かが欠けているように感じます。どんなポインタでも大歓迎です


var map ;

function init() {
  var mercator = new OpenLayers.Projection("EPSG:900913");
  var geographic = new OpenLayers.Projection("EPSG:4326");

  //note that I have host equal to location//   //Math.Random will stop caching//
  var mykmlurl = 'http://myserver/kml-track.kml';

  map = new OpenLayers.Map({
    div: "map",
    projection: mercator,
    layers: [
      new OpenLayers.Layer.OSM(),

      //Defiine your KML layer//
      new OpenLayers.Layer.Vector("This Is My KML Layer", {
        //Set your projection and strategies//
        projection: geographic,
        strategies: [new OpenLayers.Strategy.Fixed()],
        //set the protocol with a url//
        protocol: new OpenLayers.Protocol.HTTP({
          //set the url to your variable//
          url: mykmlurl,
          //format this layer as KML//
          format: new OpenLayers.Format.KML({
            //maxDepth is how deep it will follow network links//
            maxDepth: 1,
            //extract styles from the KML Layer//
            extractStyles: true,
            //extract attributes from the KML Layer//
            extractAttributes: true
          })
        }),
        styleMap: new OpenLayers.StyleMap({
          "default": new OpenLayers.Style({
            graphicName: "circle",
            pointRadius: 2,
            fillOpacity: 0.5,
            fillColor: "#ffcc66",
            strokeColor: "#666633",
            strokeWidth: 1
          })
        })
      })
    ],
    center: new OpenLayers.LonLat(-93.2735, 44.8349).transform(geographic, mercator),
    zoom: 8
  });


 //function called// //timer// //layer to refresh//
  window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);
}

function UpdateKmlLayer(layer) {
  //setting loaded to false unloads the layer//
  layer.loaded = false;
  //setting visibility to true forces a reload of the layer//
  layer.setVisibility(true);
  //the refresh will force it to get the new KML data//
  layer.refresh({ force: true, params: { 'key': Math.random()} });
}

4

1 に答える 1

2

これは、OpenLayers で KML レイヤーを表示する方法の例です。

http://openlayers.org/dev/examples/kml-layer.html

ページを開くときにエラーが表示されますか? または、正常に実行されても何も表示されませんか? エラーが発生しない場合は、プロジェクションの設定方法に問題があることを示している可能性があります (つまり、フィーチャが期待する場所に表示されない可能性があります)。

于 2012-11-14T09:11:56.807 に答える