2015-12-24 42 views
3

我想利用內幕NG-地圖NG重複如下動態添加地圖標記到地圖,設置不同的地圖標記圖標的基礎上NG-重複變量

<div id="map-canvas"> 
    <ng-map default-style="true"> 
     <marker id='ID{{school.id}}' ng-repeat="school in vm.schools" position="{{school.location}}" on-click="vm.showDetail(school)" icon="assets/img/marker-excellent.png"> 
     </marker> 

     <info-window id="marker-info"> 
      <div ng-non-bindable=""> 
       <h5>{{vm.school.name}}</h5> 
      </div> 
     </info-window> 
    </ng-map> 
</div> 

這裏這將創建多個標記值具有相同的標記圖標。我想根據{{school.rating}}的值使用不同的標記圖標。然而,我無法弄清楚如何改變標記圖標網址的基礎上評級的價值,而NG - 重複是在行動中呈現地圖上的標記。

目前,我正在做以下工作,但我認爲這是一個效率低下的方法。

<div id="map-canvas"> 
    <ng-map default-style="true"> 
     <marker id='ID{{school.id}}' ng-if="school.overallRating >= 4.5" ng-repeat="school in filteredSchools = (search.schools | filter:boardsFilter)" position="{{school.location}}" on-click="search.showDetail(school)" icon="assets/img/marker-excellent.png"> 
     </marker> 

     <marker id='ID{{school.id}}' ng-if="school.overallRating < 4.5 && school.overallRating >= 3.5" ng-repeat="school in filteredSchools = (search.schools | filter:boardsFilter)" position="{{school.location}}" on-click="search.showDetail(school)" icon="assets/img/marker-good.png"> 
     </marker> 

     <marker id='ID{{school.id}}' ng-if="school.overallRating < 3.5 && school.overallRating >= 2.0" ng-repeat="school in filteredSchools = (search.schools | filter:boardsFilter)" position="{{school.location}}" on-click="search.showDetail(school)" icon="assets/img/marker-average.png"> 
     </marker> 

     <info-window id="marker-info"> 
      <div ng-non-bindable=""> 
       <h5>{{vm.school.name}}</h5> 
      </div> 
     </info-window> 
    </ng-map> 
</div> 

在這裏,我已經把多種標記指令各有NG重複,但使用不同的條件NG-如果

我想知道是否有更有效的方式使用同一個標記指示去做ng-repeat

+0

你是否已經嘗試將字段添加到學校的對象(如 'marker_type')包含有關標記信息是呈現,然後將圖標屬性綁定到此字段? (例如,icon =「assets/img/{{school.marker_type}}。png」) – beaver

+0

這不會提供所需的功能,因爲標記類型是動態的並取決於評分。因此,數據庫不能爲每個學校行存儲標記類型。即使它存儲了它,也必須持續監控以便在評級值更改的情況下進行更新。所以這不是一個動態的解決方案。 – Avi

+0

是的,但我認爲來自數據庫的數據可以從後端服務檢索,後端服務可以動態地(計算)每個記錄(學校)的「marker_type」字段。然而,Vadim Gremyachev提出的解決方案是一種很好的客戶端方法。 – beaver

回答

4

您可以引入一個函數來顯示每個項目(學校評分)的標記圖標,如下所示。

工作實例

var app = angular.module('appMaps', ['ngMap']); 
 
app.controller('mapCtrl', function() { 
 
    var vm = this; 
 

 

 
    vm.schools = [ 
 
     { id: 1, name : "Brisbane", location: [-33.867396, 151.206854], overallRating: 3.2 }, 
 
     { id: 2, name: "Sydney", location: [-27.46758, 153.027892], overallRating: 4.6 }, 
 
     { id: 3, name: "Perth", location: [-31.953159, 115.849915], overallRating: 3.5 } 
 
    ]; 
 

 

 
    vm.getIcon = function (school) { 
 
     var iconsTable = { 
 
      2: "http://google.com/mapfiles/ms/micons/green.png", 
 
      3: "http://google.com/mapfiles/ms/micons/yellow.png", 
 
      4: "http://google.com/mapfiles/ms/micons/orange.png", 
 
      5: "http://google.com/mapfiles/ms/micons/red.png", 
 
     } 
 

 
     var iconUrl = iconsTable[Math.round(school.overallRating)] 
 
     if (iconUrl) 
 
      return iconUrl; 
 
     return "http://google.com/mapfiles/ms/micons/blue.png"; 
 
    }; 
 

 
});
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
 
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script> 
 
<div ng-app="appMaps" ng-controller="mapCtrl as vm"> 
 
     <map center="[-24.667856, 133.630694]" zoom="4"> 
 
      <marker id='ID{{school.id}}' ng-repeat="school in vm.schools" position="{{school.location}}" icon="{{vm.getIcon(school)}}" > 
 
      </marker> 
 
      
 
     </map> 
 
</div>

JSFiddle

+0

謝謝你的答案瓦丁。這按照要求工作。不過,這是我注意到的一件小事。如果我在getIcon函數中放置了一些日誌語句,看起來getIcon會被同一個學校多次調用。我希望這個函數被稱爲最多3次,因爲有3個學校記錄,但它似乎多次進入getIcon。這是AnglarJS的行爲還是可以修復的缺陷? – Avi