I have a JSON array fetched from MongoDB server using AngularJS which looks like this:
My controller.js:
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
$scope.stack=[];
$scope.loadData = function(){
var request =$http({
method: "post",
url: "my_api",
headers:{'Content-Type':'application/x-www-form-urlencoded' },
transformRequest: function() {
var str = [];
str.push(encodeURIComponent("collection") + "=" + encodeURIComponent("emergencyalerts"));
return str.join("&");
}
});
request.success(function(response){
console.log("success");
for(var i = 0; i < response.length; i++) {
$scope.stack.push('new google.maps.LatLng('+response[i].location+')');
}
console.log($scope.stack);
});
};
});
In console:
$scope.stack= Array ["new google.maps.LatLng(33.75218,-118.29082)",
"new google.maps.LatLng(33.77276,-118.20692)",
"new google.maps.LatLng(33.90358,-118.18547)",
"new google.maps.LatLng(33.80297,-118.08174)"
]
I want to plot these points in Google HeatMap.
My html page:
<body ng-app='myApp' ng-controller='myCtrl' ng-init="loadData()">
<div id="map"></div>
<script>
var map, heatmap;
function initMap(getNum) {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 34.022352, lng: -118.285117},
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var gradients = {
color: [
'rgba(0, 0, 0, 0)',
'rgba(0, 0, 0, 0)',
'rgba(0, 0, 0, 0)',
'rgba(0, 0, 159, 1)',
'rgba(255, 0, 0, 1)',
'rgba(255, 0, 0, 1)'
]
};
heatmap = new google.maps.visualization.HeatmapLayer({
data: $scope.stack,
radius: 13,
opacity: 100,
map: map
});
heatmap.set('gradient', gradients['color']);
}
</script>
Error: ReferenceError: $scope is not defined
How to resolve it and display lat lng points in real-time by fetching data from Mongo DB?