javascript - Reutilize maps markers for weighted heatMaps -
i'm using functions markers via ajax, , draw in map_canvas div. works perfectly, , i'm trying reuse "gmarkers" variable create heatmap...with no luck. how can create heatmap data?
i prefer use same data i'm loading function weighting heatmap variable: llamados.
how can start?
var gmarkers = []; function initialize() { var myoptions = { zoom: 10, center: mapcenter, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid('map_canvas'), myoptions); current_zoom = map.getzoom(); var mycontrol = document.getelementbyid('descripcion'); map.controls[google.maps.controlposition.right_bottom].push(mycontrol); } google.maps.event.adddomlistener(window, 'load', initialize); var infowindow = new google.maps.infowindow(); var bounds = new google.maps.latlngbounds(); var marker, i; var mylatlng; function createmarker (obj, i) { mylatlng = new google.maps.latlng(obj['lat'], obj['lon']); marker = new google.maps.marker({ position: mylatlng, animation: google.maps.animation.drop, title: obj['nodo'], llamados: obj['llamados'], icon: obj['icono'], map: map }); google.maps.event.addlistener(marker, 'click', (function(marker, i) { return function() { infowindow.setcontent('nodo: ' + obj['nodo'] + '; llamados: ' + obj['llamados']); infowindow.open(map, marker); } })(marker, i)); gmarkers.push(marker); } function ejecutarajax(){ $.ajax({ beforesend: function() { }, cache: false, // data: params, datatype: 'json', timeout: 0, type: 'post', url: 'traerllamados.php', success: function(data) { if (data) { var data = data; var obj; cantidad=object.keys(data).length; for(var in data){ createmarker(data[i]); }; } else { alert('no data'); } }, }); }
given existing code, should able add heatmap map (after markers loaded) doing:
var heatmaparray = []; (var i=0; i<gmarkers.length; i++) { heatmaparray.push({location: gmarkers[i].getposition(), weight: gmarkers[i].llamados}); } var heatmap = new google.maps.visualization.heatmaplayer({ data: heatmaparray }); heatmap.setmap(map);
code snippet:
var gmarkers = []; var mapcenter = new google.maps.latlng(37.782, -122.447); function initialize() { var myoptions = { zoom: 15, center: mapcenter, maptypeid: google.maps.maptypeid.satellite }; map = new google.maps.map(document.getelementbyid('map_canvas'), myoptions); current_zoom = map.getzoom(); var mycontrol = document.getelementbyid('descripcion'); map.controls[google.maps.controlposition.right_bottom].push(mycontrol); (var = 0; < heatmapdata.length; i++) { // translate obj expected createmarker var obj = { lat: heatmapdata[i].location.lat(), lon: heatmapdata[i].location.lng(), nodo: "nodo" + i, llamados: heatmapdata[i].weight, icono: "http://maps.google.com/mapfiles/ms/micons/blue.png" } createmarker(obj, i); } var heatmaparray = []; (var = 0; < gmarkers.length; i++) { heatmaparray.push({ location: gmarkers[i].getposition(), weight: gmarkers[i].llamados }); } var heatmap = new google.maps.visualization.heatmaplayer({ data: heatmaparray }); heatmap.setmap(map); } google.maps.event.adddomlistener(window, 'load', initialize); var infowindow = new google.maps.infowindow(); var bounds = new google.maps.latlngbounds(); var marker, i; var mylatlng; function createmarker(obj, i) { mylatlng = new google.maps.latlng(obj['lat'], obj['lon']); marker = new google.maps.marker({ position: mylatlng, animation: google.maps.animation.drop, title: obj['nodo'], llamados: obj['llamados'], icon: obj['icono'], map: map }); google.maps.event.addlistener(marker, 'click', (function(marker, i) { return function() { infowindow.setcontent('nodo: ' + obj['nodo'] + '; llamados: ' + obj['llamados']); infowindow.open(map, marker); } })(marker, i)); gmarkers.push(marker); } google.maps.event.adddomlistener(window, "load", initialize); /* data points defined mixture of weightedlocation , latlng objects */ var heatmapdata = [{ location: new google.maps.latlng(37.782, -122.447), weight: 0.5 }, { location: new google.maps.latlng(37.782, -122.445), weight: 1 }, { location: new google.maps.latlng(37.782, -122.443), weight: 2 }, { location: new google.maps.latlng(37.782, -122.441), weight: 3 }, { location: new google.maps.latlng(37.782, -122.439), weight: 2 }, { location: new google.maps.latlng(37.782, -122.437), weight: 10 }, { location: new google.maps.latlng(37.782, -122.435), weight: 0.5 }, { location: new google.maps.latlng(37.785, -122.447), weight: 3 }, { location: new google.maps.latlng(37.785, -122.445), weight: 2 }, { location: new google.maps.latlng(37.785, -122.443), weight: 5 }, { location: new google.maps.latlng(37.785, -122.441), weight: 0.5 }, { location: new google.maps.latlng(37.785, -122.439), weight: 1 }, { location: new google.maps.latlng(37.785, -122.437), weight: 2 }, { location: new google.maps.latlng(37.785, -122.435), weight: 3 }];
html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,visualization"></script> <div id="map_canvas"></div>
Comments
Post a Comment