Commits

kiwi committed 01f9352

search: return only points inside bounding box

  • Participants
  • Parent commits dd46b33

Comments (0)

Files changed (2)

seedswap/seed/templates/search.html

 <script type="text/javascript">
     var map, layer;
     function init(){
-        map = new OpenLayers.Map( 'map');
+        map = new OpenLayers.Map( 'map' );
         layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
         map.addLayer(layer);
         map.setCenter(
 
     var $form = $( this ),
     term = $form.find( 'input[name="s"]' ).val();
-    center =  map.center.transform(
+    bounds =  map.getExtent().transform(
                 map.getProjectionObject(),
-                new OpenLayers.Projection("EPSG:4326")   )
+                new OpenLayers.Projection("EPSG:4326")   ).toArray()
     url = $form.attr( 'action' );
 
 
-    $.post( url, {  lat: center.lat,
-                    lon: center.lon  },
+    $.post( url, {  l: bounds[0],
+                    b: bounds[1],
+                    r: bounds[2],
+                    t: bounds[3]  },
       function( data ) {
 
 

seedswap/seed/views.py

 def search(request):
 
     if request.method == 'POST':
-        form = forms.SearchForm(request.POST)
-        if form.is_valid() or 1:
-            all_seeds = Seed.objects.all()
-            owners = set()
-            rdata = []
-            for seed in all_seeds:
-                owners.add(seed.owner)
-            for owner in owners:
-                lat = float(owner.location_lat)
-                lon = float(owner.location_lon)
-                #if abs(lat - form.POST['lat']) < 1 and \
-                #         abs(lon - form.POST['lon']) < 1:
+        #form = forms.SearchForm(request.POST)
+        all_seeds = Seed.objects.all()
+        owners = set()
+        rdata = []
+        for seed in all_seeds:
+            owners.add(seed.owner)
+        for owner in owners:
+            lat = float(owner.location_lat)
+            lon = float(owner.location_lon)
+            l, b, r ,t = (
+                float(request.POST['l']),
+                float(request.POST['b']),
+                float(request.POST['r']),
+                float(request.POST['t']) )
+            if  l < lon < r and \
+                b < lat < t:
                 rdata.append([lat, lon])
-            return HttpResponse(json.dumps(rdata), mimetype="application/json")
+        return HttpResponse(json.dumps(rdata), mimetype="application/json")
     else:
         form = forms.SearchForm()
         return render_to_response('search.html',