I am working with the google maps javascript api and am attempting to update a heat map when the user clicks on a div. The lat long data the is passed into the heatmap come from local json files. At the moment I can get my web page to load with the map and the heatmap overlay showing up properly. The problem is when I click on a div and expect the heatmap data to get updated, the heatmap just disappears. Through my debugging I've discovered that the $.ajax
call in my getPoints(file)
method gets skipped when I run getPoints(file)
in my on click listener. Does anyone know what might be going wrong? Here is my code :
var map, heatmap;
function initMap() {
var sanFran = {
lat: 37.775299,
lng: -122.432432
}
map = new google.maps.Map(document.getElementById('Map'), {
zoom: 10,
center: sanFran,
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints("2016-07-02--11-56-24.json"),
map: map
});
};
function getPoints(file) {
retArr = [];
var weblink = "http://localhost:8080/trips/" + file;
$.ajax({
url: weblink,
dataType:"json",
success:function(data){
for(var i = 0; i< data.coords.length; i++){
console.log(data.coords[i]);
retArr.push(new google.maps.LatLng(data.coords[i].lat, data.coords[i].lng))
}
}
});
return retArr;
}
$.each(jsTrips,function(k,v){
$("#"+k).on("click",function(){
heatmap.set('data', getPoints(v));
})
})
The json data follows the format:
{
"start_time":some time,
"coords": [{lat,long},{lat,long},{lat,long}],
"end time":some time
}
If any more details are need just let me know and I'll update the question.
Thanks!!!