Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Heatmap works

  • Loading branch information...
commit 9635dd0842c4c3b7f27e8565e69d597205e0d7dd 1 parent f19dcd2
Davy-jones-Giant-squid authored
Showing with 74 additions and 17 deletions.
  1. +68 −3 chicagocrime/templates/heatmap.html
  2. +6 −14 scrape_crime/views.py
View
71 chicagocrime/templates/heatmap.html
@@ -13,14 +13,79 @@
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=visualization"></script>
<script>
+var map, pointarray, heatmap;
+var js_obj = {{obj}};
+var taxiData = [];
-var message = "{{obj}}";
-alert(message);
+for(var i=0; i<js_obj.length; i++){
+ taxiData.push(new google.maps.LatLng(js_obj[i][0], js_obj[i][1]));
+}
+
+function initialize() {
+ var mapOptions = {
+ zoom: 10,
+ center: new google.maps.LatLng(41.87, -87.62),
+ mapTypeId: google.maps.MapTypeId.SATELLITE
+ };
+
+ map = new google.maps.Map(document.getElementById('map-canvas'),
+ mapOptions);
+
+ var pointArray = new google.maps.MVCArray(taxiData);
+
+ heatmap = new google.maps.visualization.HeatmapLayer({
+ data: pointArray
+ });
+
+ heatmap.setMap(map);
+}
+
+function toggleHeatmap() {
+ heatmap.setMap(heatmap.getMap() ? null : map);
+}
+
+function changeGradient() {
+ var gradient = [
+ 'rgba(0, 255, 255, 0)',
+ 'rgba(0, 255, 255, 1)',
+ 'rgba(0, 191, 255, 1)',
+ 'rgba(0, 127, 255, 1)',
+ 'rgba(0, 63, 255, 1)',
+ 'rgba(0, 0, 255, 1)',
+ 'rgba(0, 0, 223, 1)',
+ 'rgba(0, 0, 191, 1)',
+ 'rgba(0, 0, 159, 1)',
+ 'rgba(0, 0, 127, 1)',
+ 'rgba(63, 0, 91, 1)',
+ 'rgba(127, 0, 63, 1)',
+ 'rgba(191, 0, 31, 1)',
+ 'rgba(255, 0, 0, 1)'
+ ]
+ heatmap.setOptions({
+ gradient: heatmap.get('gradient') ? null : gradient
+ });
+}
+
+function changeRadius() {
+ heatmap.setOptions({radius: heatmap.get('radius') ? null : 20});
+}
+
+function changeOpacity() {
+ heatmap.setOptions({opacity: heatmap.get('opacity') ? null : 0.2});
+}
+
+google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
-
+ <div id="panel">
+ <button onclick="toggleHeatmap()">Toggle Heatmap</button>
+ <button onclick="changeGradient()">Change gradient</button>
+ <button onclick="changeRadius()">Change radius</button>
+ <button onclick="changeOpacity()">Change opacity</button>
+ </div>
+ <div id="map-canvas"></div>
</body>
</html>
View
20 scrape_crime/views.py
@@ -3,7 +3,7 @@
from gmapi import maps
from gmapi.forms.widgets import GoogleMap
from django.http import HttpResponse
-import simplejson
+
from scrape_crime.models import Crime, Location
@@ -11,21 +11,13 @@ class MapForm(forms.Form):
map = forms.Field(widget=GoogleMap(attrs={'width':510, 'height': 500}))
def heatmap(request):
- """
- obj = Crime.objects.all()
- obj_list = []
+ obj = Crime.objects.exclude(location__latitude=None)
+ list_obj = []
for i in obj:
- try:
- obj_list.append((i.location.latitude, i.location.longitude))
- except AttributeError:
- pass
- json_list = simplejson.dumps(obj_list)
- cxt = {'obj': json_list}
- return render_to_response('heatmap.html', cxt)
- """
- obj = 'hello!'
- cxt = {'obj': obj}
+ list_obj.append([i.location.latitude, i.location.longitude])
+ cxt = {'obj': list_obj}
return render_to_response('heatmap.html', cxt)
+
def map(request, latitude=41.87, longitude=-87.62):
distance = 0.03
Please sign in to comment.
Something went wrong with that request. Please try again.