0

I'm trying to dynamically add multiple GeoJSON data to my map. as shown below. However I don't see the data getting rendered.

$http.get("/kp-data").success(function(data, status){

                angular.forEach(data, function(k,v){
                        $scope.geojson[k.carId] = {data : k.data,
                                resetStyleOnMouseout: true,
                                style: {
                                    fillColor: k.color,
                                    weight: 2,
                                    opacity: 1,
                                    color: 'white',
                                    dashArray: '3',
                                    fillOpacity: 0.7
                                }
                        }
                });

In this example the geojson data USA and JPN is hardcoded. I modified the code for dynamic addition. The code works if I modified as below for a single geojson data

   $http.get("/kp-data").success(function(data, status){

            angular.forEach(data, function(k,v){
                    if(v==1){
                    $scope.geojson = {data : k.data,
                            resetStyleOnMouseout: true,
                            style: {
                                fillColor: k.color,
                                weight: 2,
                                opacity: 1,
                                color: 'white',
                                dashArray: '3',
                                fillOpacity: 0.7
                            }
                    }
                 }
            });
halfer
  • 19,824
  • 17
  • 99
  • 186
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112

2 Answers2

0

Do you have some error message ?

And you should declear $scope.geojson as an object before you call angular.forEach function.

like this .

stackoverYC
  • 490
  • 2
  • 4
  • 13
0

After declare $scope.jeojson = [], length of this is 0, so in loop $scope.jeojson[index] is undefined (index # 0). I think you should try something as: $scope.jeojson.push(json)...

And, i recommend you to use "for" rather than "angular.forEach". Hope it good for you!

TVT. Jake
  • 279
  • 2
  • 7