Permalink
Browse files

some templates and views

  • Loading branch information...
1 parent 547a537 commit f19dcd27995111d7a17fd5086486ea0f5cd8d426 Davy-jones-Giant-squid committed Jul 10, 2013
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Maps Engine Layer</title>
+ <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
+ <meta charset="utf-8">
+ <style>
+ html, body, #map-canvas {
+ margin: 10;
+ padding: 10;
+ height: 100%;
+ }
+ </style>
+ <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=visualization"></script>
+ <script>
+
+var message = "{{obj}}";
+alert(message);
+
+ </script>
+ </head>
+
+ <body>
+
+ </body>
+</html>
@@ -10,16 +10,16 @@
{{ form.media.js }}
<div class="gmap" style="position:relative;width:510px;height:510px;" id="id_map"></div>
<div class="{&quot;arg&quot;:[&quot;div&quot;,{&quot;mapTypeId&quot;:{&quot;val&quot;:&quot;MapTypeId.ROADMAP&quot;},&quot;mapTypeControlOptions&quot;:{&quot;style&quot;:{&quot;val&quot;:&quot;MapTypeControlStyle.DROPDOWN_MENU&quot;}},&quot;center&quot;:{&quot;arg&quot;:[38,-97],&quot;cls&quot;:&quot;LatLng&quot;},&quot;zoom&quot;:3}],&quot;cls&quot;:&quot;Map&quot;}" style="position:absolute;width:510px;height:510px"></div>
- <img style="position:absolute;z-index:1" width="510" height="510" alt="Google Map" src="http://maps.google.com/maps/api/staticmap?sensor=false&amp;center=38,-97&amp;maptype=roadmap&amp;zoom=3&amp;size=510x510" />
-</div>
-<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
-<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
-<script type="text/javascript" src="gmapi/js/jquery.gmapi.js"></script>
-<script type="text/javascript">
- window.myobj = {
- markerOver: function() { this.openInfoWindow(); },
- markerOut: function() { this.closeInfoWindow(); }
- };
-</script>
+ <img style="position:absolute;z-index:1" width="510" height="510" alt="Google Map" src="http://maps.google.com/maps/api/staticmap?sensor=false&amp;center=38,-97&amp;maptype=roadmap&amp;zoom=3&amp;size=510x510" /></div>
+
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
+ <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
+ <script type="text/javascript" src="gmapi/js/jquery.gmapi.js"></script>
+ <script type="text/javascript">
+ window.myobj = {
+ markerOver: function() { this.openInfoWindow(); },
+ markerOut: function() { this.closeInfoWindow(); }
+ };
+ </script>
</body>
</html>
@@ -0,0 +1,36 @@
+<!-- for tutorial : https://developers.google.com/maps/documentation/javascript/tutorial#api_key-->
+
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+ <title>Google Maps API Sample</title>
+ <script src="//maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script>
+ <script type="text/javascript">
+
+ function initialize() {
+ if (GBrowserIsCompatible()) {
+ var map = new GMap2(document.getElementById("map_canvas"));
+ map.setCenter(new GLatLng({{ latitude }}, {{ longitude }}), 13);
+
+ // Add 10 markers to the map at random locations
+ var bounds = map.getBounds();
+ var southWest = bounds.getSouthWest();
+ var northEast = bounds.getNorthEast();
+ var lngSpan = northEast.lng() - southWest.lng();
+ var latSpan = northEast.lat() - southWest.lat();
+ for (var i = 0; i < {{ len(crimes); i++) {
+ var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
+ southWest.lng() + lngSpan * Math.random());
+ map.addOverlay(new GMarker(point));
+ }
+ }
+ }
+
+
+ </script>
+ </head>
+ <body onload="initialize()" onunload="GUnload()" style="font-family: Arial;border: 0 none;">
+ <div id="map_canvas" style="width: 500px; height: 300px"></div>
+ </body>
+</html>
@@ -8,6 +8,8 @@
# Examples:
(r'', include('gmapi.urls.media')),
(r'^$', 'scrape_crime.views.map'),
+ (r'^2$', 'scrape_crime.views.map2'),
+ (r'^heatmap/', 'scrape_crime.views.heatmap'),
# url(r'^chicagocrime/', include('chicagocrime.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
@@ -1,3 +1,5 @@
+import math
+
from django.db import models
class Crime(models.Model):
@@ -19,11 +21,27 @@ class Crime(models.Model):
def __unicode__(self):
- return self.id
+ return u'Crime case number %s' % self.case_number
class Meta:
ordering = ['description']
+ def in_range(self, latitude, longitude, distance):
+ """
+ Given latitude and longitude numbers, as well as a distance,
+ return True, if the latitude and longitude are within 'distance'
+ units of latitude and longitude of the point given. Otherwise,
+ return False
+ """
+ if not latitude or not longitude or not self.location:
+ return False
+
+ if math.sqrt((latitude - self.location.latitude)**2 +
+ (longitude - self.location.longitude)**2) < distance:
+ return True
+ else:
+ return False
+
class Location(models.Model):
latitude = models.FloatField(null=True, blank=True)
@@ -33,4 +51,4 @@ class Location(models.Model):
block = models.CharField(max_length=255, null=True, blank='')
def __unicode__(self):
- return self.block
+ return u'Location: %s, %s' % (self.latitude, self.longitude)
View
@@ -2,35 +2,77 @@
from django.shortcuts import render_to_response
from gmapi import maps
from gmapi.forms.widgets import GoogleMap
+from django.http import HttpResponse
+import simplejson
from scrape_crime.models import Crime, Location
class MapForm(forms.Form):
map = forms.Field(widget=GoogleMap(attrs={'width':510, 'height': 500}))
+def heatmap(request):
+ """
+ obj = Crime.objects.all()
+ obj_list = []
+ 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}
+ return render_to_response('heatmap.html', cxt)
-def map(request):
- myLatlng = maps.LatLng(41.87,-87.62)
+def map(request, latitude=41.87, longitude=-87.62):
+ distance = 0.03
+ myLatlng = maps.LatLng(latitude, longitude)
gmap = maps.Map(opts = {
'center': myLatlng,
'mapTypeId': maps.MapTypeId.ROADMAP,
'zoom': 13,
'mapTpeControlOptions':{
'style': maps.MapTypeControlStyle.DROPDOWN_MENU
},
- })
-
- obj = Crime.objects.order_by('id')
- for i in obj[:10]:
- marker = maps.Marker(opts = {
- 'position': maps.LatLng(i.location.latitude, i.location.longitude),
- 'map' : gmap,
- })
- maps.event.addListener(marker, 'mouseover','myobj.markerOver')
- maps.event.addListener(marker, 'mouseout', 'myobj.markerOut')
- info = maps.InfoWindow({
- 'content': i.primary_type,
- })
- info.open(gmap, marker)
+ })
+
+
+ obj = Crime.objects.filter(location__latitude__gte=latitude - distance,
+ location__latitude__lte=latitude + distance, location__longitude__gte=longitude - distance,
+ location__longitude__lte=longitude + distance)
+ for i in obj:
+ if i.in_range(latitude, longitude, distance):
+ try:
+ marker = maps.Marker(opts = {
+ 'position': maps.LatLng(i.location.latitude, i.location.longitude),
+ 'map' : gmap,
+ })
+ maps.event.addListener(marker, 'mouseover','myobj.markerOver')
+ maps.event.addListener(marker, 'mouseout', 'myobj.markerOut')
+ info = maps.InfoWindow({
+ 'content': i.primary_type,
+ })
+ info.open(gmap, marker)
+ except AttributeError:
+ pass
+
context = {'form': MapForm(initial={'map':gmap})}
return render_to_response('map.html', context)
+
+
+def map2(request, latitude=41.87, longitude=-87.62):
+ distance = 0.03
+ objs = Crime.objects.filter(location__latitude__gte=latitude - distance,
+ location__latitude__lte=latitude + distance, location__longitude__gte=longitude - distance,
+ location__longitude__lte=longitude + distance)
+ cxt = {
+ 'latitude': latitude,
+ 'longitude': longitude,
+ 'distance': distance,
+ 'crimes': objs,
+ }
+
+ return render_to_response('map2.html', cxt)

0 comments on commit f19dcd2

Please sign in to comment.