2016-07-25 119 views
1

我正在使用Ionic和ng-map。我必須顯示不同的KML(一次一個,並根據用戶請求)。 KML在地圖上加載,但僅適用於遠程KML。所以這一個工程:ng-map只能打開遠程KML文件而不能打開本地文件?

<div style="height: 100%"> 
    <ng-map center="41.876,-87.624" zoom="15" map-type-id="HYBRID" style="display:block; width: 100%; height:100%;"> 
    <kml-layer url="http://googlemaps.github.io/js-v2-samples/ggeoxml/cta.kml"></kml-layer> 
    </ng-map> 
</div> 

我本地的KML處於kml文件夾內www目錄在我的離子的應用程序。

enter image description here

而且我加載本地KML文件是這樣的(相同的語法遠程KML文件):

<div style="height: 100%"> 
    <ng-map center="41.876,-87.624" zoom="15" map-type-id="HYBRID" style="display:block; width: 100%; height:100%;"> 
    <kml-layer url="./kml/cta.kml"></kml-layer> 
    </ng-map> 
</div> 

但KML線不出現像遠程KML文件。地圖已加載,但沒有在KML中指定的行。

那麼,ng-map是否需要遠程KML來加載KML規格?這是ng-map或Ionic目錄上的錯誤嗎?

我沒有收到任何錯誤或其他任何信息,表明除了遠程KML文件不像本地KML文件看起來似乎沒有讀取規格,它沒有任何錯誤。

回答

1

它既不是ng-map庫也不是Ionic的錯誤。 ng-map庫使用KML Layers,而這又不支持從本地主機或文件系統加載KML文件。

基本上,你這裏有兩種選擇:

1)放置公共服務器其中谷歌服務器可以訪問它的KML文件,因爲KML的解析和渲染是由谷歌服務器上完成

2 )利用第三方庫,如geoxml3 library它允許您加載KML文件,並解析它託管在本地主機上,如下圖所示:

的Html

<div ng-app="mapApp" ng-controller="mapController"> 
    <ng-map center="41.876,-87.624" zoom="15" map-type-id="HYBRID" style="display:block; width: 100%; height:100%;"/> 
</div> 

的js

angular.module('mapApp', ['ngMap']) 
    .controller('mapController', function($scope, NgMap) { 

     NgMap.getMap().then(function(map) { 
      $scope.map = map; 
      var myParser = new geoXML3.parser({ map: map }); 
      myParser.parse('cta.kml'); 
     }); 
    }); 

Demo

+0

謝謝!我試過這個,它的工作原理。但它只是第一次。如你所知,我正在使用Ionic。所以我使用'ionicModal'加載地圖。從第二次開始,我打開模式,KML信息不再顯示。這是一個https://plnkr.co/edit/EnN1q9tvWVrpiXPTcVoc?p=preview你知道爲什麼或任何解決這個問題? – junerockwell