2014-10-31 92 views
0

我想在谷歌地圖上放置針點。但問題是我有這麼多的位置針點,我做這件事的方式有點困難。有一個簡單的方法去做這個?在谷歌地圖上放置針點

的方法,我做

.controller('MapCtrl', function($scope, $ionicLoading, $compile, $state) { 
     function initialize() { 

     //variables for adding the coordinates of the locations 

     var Chunnakam = new google.maps.LatLng(9.741527 ,80.033931); 
     var Nelliady = new google.maps.LatLng( 9.828288,80.220362); 
     var Karaditoku_Kilinochchi = new google.maps.LatLng(9.380289,80.377); 
     //alot more of these come after this 

,我將分這樣

var marker = new google.maps.Marker({ 
      position: Chunnakam, 
      map: map, 
      title: 'Chunnakam' 
     }); 

     var marker2 = new google.maps.Marker({ 
      position: Nelliady, 
      map: map, 
      title: 'Nelliady' 
     }); 

// 
     var marker3 = new google.maps.Marker({ 
      position: Karaditoku_Kilinochchi, 
      map: map, 
      title: 'Karaditoku Kilinochchi' 
     }); 

有沒有一種簡單的方法來做到這一點?謝謝。

回答

0

您可以創建對象的數組,並迭代是:

var towns = [ { marker: null, name: 'Chunnakam', lat: 9.741527, lng: 80.033931 }, 
       { marker: null, name: 'Nelliady', lat: 9.828288, lng: 80.220362 }, 
       // add new towns here 
      ]; 


angular.forEach(towns, function(town) { 
    town.marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(town.lat, town.lng), 
     map: map, 
     title: town.name 
    }); 
}