2017-10-13 64 views
1

我有後端列表,我有2個地方。谷歌熱圖只顯示一個地方

這是後端的代碼。

public JsonResult GetData() 
    { 

     List<Park> stations = new List<Park>(); 

     stations.Add(new Park() 
     { 
      Id = 2, 
      GeoLat = 37.608644, 
      GeoLong = 55.75226, 
      Weight = 12 
     }); 

     stations.Add(new Park() 
     { 
      Id = 3, 
      GeoLat = 30.5807913, 
      GeoLong = 50.493239, 

      Weight = 12 
     }); 

     return Json(stations.ToArray(), JsonRequestBehavior.AllowGet); 
    } 

而且我得到它的客戶端這樣

function getPoints() { 

     $.getJSON('@Url.Action("GetData", "Home")', 
      function (data) { 
       var marker; 
       // Проходим по всем данным и устанавливаем для них маркеры 
       $.each(data, 
        function(i, item) { 
         marker = [ 
          { 
           'location': new google.maps.LatLng(item.GeoLong, item.GeoLat), 
           'map': map, 
           'weight': item.Weight 
          } 
         ]; 
        }); 



       var pointArray = new google.maps.MVCArray(marker); 
       console.log(pointArray); 
       heatmap = new google.maps.visualization.HeatmapLayer({ 
        data: pointArray 
       }); 
       heatmap.setMap(map); 


      }); 
    }; 

和更新地圖在這個方法中

function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), 
      { 
       zoom: 13, 
       center: { lat: 55.752622, lng: 37.617567 }, 
       mapTypeId: 'satellite' 
      }); 
     getPoints(); 
    } 

但我只看到一個地方。我不明白爲什麼,因爲我在這一行設置了斷點

return Json(stations.ToArray(), JsonRequestBehavior.AllowGet); 

我返回2個地方。

哪裏可以解決我的問題?

感謝對幫助

回答

0
var marker; 

應改爲:

var marker = []; 

marker = [ 
    { 
     'location': new google.maps.LatLng(item.GeoLong, item.GeoLat), 
     'map': map, 
     'weight': item.Weight 
    } 
]; 

應改爲:

marker.push({ 
    'location': new google.maps.LatLng(item.GeoLong, item.GeoLat), 
    'map': map, 
    'weight': item.Weight 
}); 

現有代碼的問題在於,您是指定爲單個元素數組(因此您只能有效地顯示最後一個標記)。您目前不是添加到該陣列。我的更改使用push來解決此問題。

+0

是的。它有助於。謝謝 –