我有使用AngularJS它看起來像這樣一個JSON陣列獲取來自MongoDB的服務器:訪問AngularJS範圍變量來填充谷歌地圖
我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);
});
};
});
在控制檯:
$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)"
]
我想繪製在谷歌熱圖中這些點。
我的HTML頁面:
<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>
錯誤:的ReferenceError:$範圍沒有定義
如何解決它,並從蒙戈DB獲取數據顯示在實時緯度經度點?
u能提供代碼的詳細信息/ –
穆罕默德,我現在已經編輯我的問題!你能幫我解決這個錯誤嗎?我需要從API獲取lat-lng點並在熱圖上實時顯示。 – RPS