diff --git a/pogom/app.py b/pogom/app.py index 9d470d7c..8543dfc2 100644 --- a/pogom/app.py +++ b/pogom/app.py @@ -176,7 +176,7 @@ def map_data(self): # TODO: Lured pokestops if request.args.get('gyms', 'true') == 'true': - d['gyms'] = Gym.get_all() + d['gyms'] = Gym.get_active() return jsonify(d) diff --git a/pogom/models.py b/pogom/models.py index 86d00a04..52b2bd35 100644 --- a/pogom/models.py +++ b/pogom/models.py @@ -4,6 +4,7 @@ import logging import random import math +from flask import request from peewee import Model, SqliteDatabase, InsertQuery, IntegerField, \ CharField, FloatField, BooleanField, DateTimeField, fn, SQL from datetime import datetime @@ -53,6 +54,10 @@ def get_active(cls): query = (Pokemon .select() .where(Pokemon.disappear_time > datetime.utcnow()) + .where(Pokemon.latitude < request.values.get('boundary-north', type=float)) + .where(Pokemon.latitude > request.values.get('boundary-south', type=float)) + .where(Pokemon.longitude > request.values.get('boundary-west', type=float)) + .where(Pokemon.longitude < request.values.get('boundary-east', type=float)) .dicts()) pokemons = [] @@ -122,7 +127,22 @@ class Gym(BaseModel): latitude = FloatField() longitude = FloatField() last_modified = DateTimeField() + + @classmethod + def get_active(cls): + query = (Gym + .select() + .where(Gym.latitude < request.values.get('boundary-north', type=float)) + .where(Gym.latitude > request.values.get('boundary-south', type=float)) + .where(Gym.longitude > request.values.get('boundary-west', type=float)) + .where(Gym.longitude < request.values.get('boundary-east', type=float)) + .dicts()) + + gyms = [] + for p in query: + gyms.append(p) + return gyms def parse_map(map_dict): pokemons = {} diff --git a/static/map.js b/static/map.js index 47bde4f9..fd0e3b4e 100644 --- a/static/map.js +++ b/static/map.js @@ -10,6 +10,11 @@ var $selectExclude = $("#exclude-pokemon"); var excludedPokemon = []; var map; +var rectangle; +var rectangles = []; +var rectangles_debug = 0; +var rectangles_load = {}; +var rectangle_markers = []; var scanLocations = new Map(); var coverCircles = []; var newLocationMarker; @@ -159,16 +164,21 @@ function initMap() { map = new google.maps.Map(document.getElementById('map'), { // Change this to geolocation? center: {lat: initLat, lng: initLng}, - zoom: 13, + zoom: 16, mapTypeControl: false, streetViewControl: false, disableAutoPan: true }); + rectangle = new google.maps.Rectangle(); updateScanLocations(initialScanLocations); updateMap(); updateHeatMap(); + google.maps.event.addListener(map, 'idle', function(event) { + updateMap(); + }); + if(is_logged_in()) { // on click listener for google.maps.event.addListener(map, 'click', function(event) { @@ -459,22 +469,147 @@ function updateScanLocations(updatedScanLocations) { // 'pokestops': document.getElementById('pokestops-checkbox').checked, // 'pokestops-lured': document.getElementById('pokestops-lured-checkbox').checked, function updateMap() { - $.ajax({ + if (!map){return} + if (!map.getBounds()){return} + var boundary_north = map.getBounds().getNorthEast().lat(); // y + var boundary_west = map.getBounds().getSouthWest().lng(); // x + var boundary_south = map.getBounds().getSouthWest().lat(); // y + var boundary_east = map.getBounds().getNorthEast().lng(); // x + var zoom = map.getZoom(); + + var boundary_round = 0.005; // 0.002 + + if (zoom >= 13){boundary_round = 0.05;} + else if (zoom >= 11){boundary_round = 0.2;} + else if (zoom >= 9){boundary_round = 1;} + else {boundary_round = 2} + +// console.log('map-data zoom=' + zoom + ' lat=' + boundary_north.toFixed(3) + '-' + boundary_south.toFixed(3) + ', lng=' + boundary_west.toFixed(3) + '-' + boundary_east.toFixed(3)); + + boundary_north = parseFloat((Math.ceil(boundary_north / boundary_round) * boundary_round).toFixed(3)); + boundary_south = parseFloat((Math.floor(boundary_south / boundary_round) * boundary_round).toFixed(3)); + boundary_west = parseFloat((Math.floor(boundary_west / boundary_round) * boundary_round).toFixed(3)); + boundary_east = parseFloat((Math.ceil(boundary_east / boundary_round) * boundary_round).toFixed(3)); + +/* + rectangle.setOptions({ + strokeColor: '#FF0000', + strokeOpacity: 0.8, + strokeWeight: 2, +// fillColor: '#FF0000', + fillOpacity: 0, + map: map, + bounds: { + north: boundary_north, + south: boundary_south, + east: boundary_east, + west: boundary_west + } + }); +*/ + + for (var i = 1; i<20; i+=1) + { + if (rectangles[i]){rectangles[i].setMap(null);} + if (rectangle_markers[i]){rectangle_markers[i].setMap(null);} + } + + if (zoom <= 8) + { + return + } + + var squares=0; + for (var i_y = boundary_south; i_y < boundary_north; i_y += boundary_round) + { + i_y = parseFloat(i_y.toFixed(3)); + if (i_y >= boundary_north){ + break + }; +// console.log('i_y=' + i_y); + + for (var i_x = boundary_west; i_x < boundary_east; i_x += boundary_round) + { + i_x = parseFloat(i_x.toFixed(3)); + if (i_x >= boundary_east){ + break + }; +// console.log('i_x=' + i_x); + + squares = squares + 1; + var i_x_=i_x.toFixed(3); + var i_x_to_ = (i_x + boundary_round); + i_x_to_ = i_x_to_.toFixed(3); + var i_y_=i_y.toFixed(3); + var i_y_to_ = (i_y + boundary_round); + i_y_to_ = i_y_to_.toFixed(3); + + if (squares > 20){break}; + + if (rectangles_debug) + { + if (!rectangles[squares]){rectangles[squares] = new google.maps.Rectangle();} + if (!rectangle_markers[squares]){rectangle_markers[squares] = new google.maps.Marker();} + rectangles[squares].setOptions({ + strokeColor: '#FF0000', + strokeOpacity: 0.8, + strokeWeight: 2, + // fillColor: '#FF0000', + fillOpacity: 0, + map: map, + bounds: { + north: parseFloat(i_y_to_), + south: parseFloat(i_y_), + east: parseFloat(i_x_to_), + west: parseFloat(i_x_) + } + }); + rectangle_markers[squares].setOptions({ + title: 'Square #' + squares, + map: map, + position: { + lat: parseFloat(i_y_) + (boundary_round/2), + lng: parseFloat(i_x_) + (boundary_round/2) + } + }); + console.log(' square[' + squares + '] x=' + i_x_ + '-' + i_x_to_ + ' y=' + i_y_ + '-' + i_y_to_); + } + // square start + + var square_str = i_y_to_ + ':' + i_y_ + ',' + i_x_ + ':' + i_x_to_; + if (rectangles_load[square_str] && rectangles_load[square_str].readyState == 1) + { +// console.log('still loading'); + continue; + } + + if (ga){ga('send', 'event', 'map', 'load', square_str);} + rectangles_load[square_str]=$.ajax({ url: "map-data", type: 'GET', - data: {'pokemon': localStorage.displayPokemons, - 'gyms': localStorage.displayGyms}, + data: { + 'boundary-north': i_y_to_, + 'boundary-west': i_x_, + 'boundary-south': i_y_, + 'boundary-east': i_x_to_, + 'pokemon': localStorage.displayPokemons, + 'gyms': localStorage.displayGyms + }, dataType: "json" }).done(function(result) { statusLabels(result["server_status"]); updateScanLocations(result['scan_locations']); - + $.each(result.pokemons, function(i, item){ if (!document.getElementById('pokemon-checkbox').checked) { return false; // in case the checkbox was unchecked in the meantime. } - + if (item.disappear_time < new Date().getTime()) // outdated json with old data (proxy?) + { + + } + else if (!(item.encounter_id in map_pokemons) && excludedPokemon.indexOf(item.pokemon_id) < 0) { // add marker to map and item to dict @@ -521,9 +656,14 @@ function updateMap() { $lastRequestLabel.addClass('label-danger'); $lastRequestLabel.html("Disconnected from Server") }); + + // square end + } + } + console.log('get map-data zoom=' + zoom + ' sqr size=' + boundary_round + ' cnt=' + squares + ' lat=' + boundary_north.toFixed(3) + '-' + boundary_south.toFixed(3) + ', lng=' + boundary_west.toFixed(3) + '-' + boundary_east.toFixed(3)); } -window.setInterval(updateMap, 10000); +window.setInterval(updateMap, 30000); $('#gyms-checkbox').change(function() { localStorage.displayGyms = this.checked;