diff --git a/api/bounds.php b/api/bounds.php index bf0a1b3..6eb24dc 100755 --- a/api/bounds.php +++ b/api/bounds.php @@ -1,65 +1,71 @@ . */ require '../load.php'; http_json_header(); $lat_n = @$_GET['lat_n']; $lat_s = @$_GET['lat_s']; $lng_e = @$_GET['lng_e']; $lng_w = @$_GET['lng_w']; $results = array(); if( ! empty($lat_n) && ! empty($lat_s) && ! empty($lng_e) && ! empty($lng_w) ) { $results = $GLOBALS['db']->getResults( sprintf( "SELECT station.idImpianto, station.gestore, station.latitudine, station.longitudine FROM station WHERE " . "station.latitudine BETWEEN %f AND %f AND " . "station.longitudine BETWEEN %f AND %f", $lat_s, $lat_n, $lng_w, $lng_e ) ); $n_results = count( $results ); - if($n_results > 90) { - $results->error = true; - } else { + $results->error = $n_results > 90; + + if(! $results->error) { $results->error = false; + // "intify" + $n = count($n_results); + for($i=0; $i<$n_results; $i++) { + $results[$i]->idImpianto = (int) $results[$i]->idImpianto; + $results[$i]->prezzo = (float) $results[$i]->prezzo; + } for($i=0; $i<$n_results; $i++) { $results[$i]->prezzi = $db->getResults( sprintf( "SELECT DISTINCT price.prezzo, price.isSelf, descCarburante " . "FROM price, station " . "WHERE price.idImpianto = %d AND price.idImpianto = station.idImpianto " . "ORDER BY price.prezzo" , $results[$i]->idImpianto ) ); } } } echo json_encode($results); diff --git a/facile.js b/facile.js index f1aa96e..cd63eb5 100755 --- a/facile.js +++ b/facile.js @@ -1,293 +1,290 @@ var map; -var euros = 40; +var EUROS = 40; var errortooresults = true; var trovalatuazona = true; var nessunfornitore = true; -var impianti = []; var ZOOM_OPENING_SEARCH_RESULT = 17; +var Impianti = { + all: [], + exists: function(id) { + var i = 0; + while(i < this.all.length && this.all[i++] !== id); + return Impianti.all[i-1] === id; + }, + add: function(id) { + this.all.push(id); + } +}; var Overworld = { $el: undefined, $action: undefined, visible: true, running: false, hide: function() { if(Overworld.running) { return; } Overworld.running = true; Overworld.$el.hide("drop", {direction: "up"}, "slow", function() { Overworld.$action.show("drop", {direction: "up"}, "fast", function() { Overworld.running = false; }); }); }, show: function() { if(Overworld.running) { return; } Overworld.running = true; map.closePopup(); Overworld.$el.find("input").val(""); Overworld.$action.hide("drop", {direction: "up"}, "slow", function() { Overworld.$el.show("drop", {direction: "up"}, "fast", function() { Overworld.$el.find("input").focus(); Overworld.running = false; }); }); } }; $(document).ready(function() { Overworld.$action = $("#overworld-buttons"); Overworld.$el = $("#overworld"); Overworld.$action.hide().click(function() { Overworld.show(); }); $("form").submit(function(event) { suggest_nominatim_addresses( $("input[name=search_address]").val() ); event.preventDefault(); }); map = L.map('map'); // create the tile layer with correct attribution var osmUrl='http://{s}.tile.osm.org/{z}/{x}/{y}.png'; var osmAttrib='Map data Facile.it © OpenStreetMap contributors'; var osm = new L.TileLayer(osmUrl, {minZoom: 8, maxZoom: 17, attribution: osmAttrib}); map.addLayer(osm); map.setView([45.49, 9.21], 15); map.on("moveend", function() { getMarkersInBounds(); return true; }); map.on("popupopen", function() { Overworld.hide(); }); fitDocument(); map.locate({setView: true, enableHighAccuracy: true, maxZoom: 16}); map.on("locationfound", function(e) { var radius = e.accuracy / 2; enfatizeLatLng(e.latlng, 2000); }); map.on("locationerror", function(e) { Materialize.toast("Posizione non disponibile. Zomma!", 3000); }); getMarkersInBounds(); }); function getMarkersInBounds(preCallback, postCallback) { var zoom = map.getZoom(); if(zoom < 13) { trovalatuazona && Materialize.toast("Trova la tua zona, zomma!", 3000); trovalatuazona = false; return; } - trovalatuazona = true; var bounds = map.getBounds(); - var data = {}; - data.lat_n = bounds.getNorth(); - data.lat_s = bounds.getSouth(); - data.lng_w = bounds.getWest(); - data.lng_e = bounds.getEast(); - - $.getJSON( - "/api/bounds.php", data, - function(json) { - if(json.error) { - errortooresults && Materialize.toast("Vedo molte stazioni. Zomma per scoprirle!", 2000); - errortooresults = false; - return; - } - errortooresults = true; + var data = { + lat_n: bounds.getNorth(), + lat_s: bounds.getSouth(), + lng_w: bounds.getWest(), + lng_e: bounds.getEast() + }; + + $.getJSON("/api/bounds.php", data, function(json) { + if(json.error) { + errortooresults && Materialize.toast("Vedo molte stazioni. Zomma per scoprirle!", 2000); + errortooresults = false; + return; + } + errortooresults = true; - if(json.length === 0) { - nessunfornitore && Materialize.toast("Nessun fornitore in questa zona", 2000); - nessunfornitore = false; - return; - } - nessunfornitore = true; + if(json.length === 0) { + nessunfornitore && Materialize.toast("Nessun fornitore in questa zona", 2000); + nessunfornitore = false; + return; + } + nessunfornitore = true; - if(json.length > 90) { - Materialize.toast("Vedo " + json.length + " risultati! Zooma per scoprirli", 2000); - return; - } + if(json.length > 90) { + Materialize.toast("Vedo " + json.length + " risultati! Zooma per scoprirli", 2000); + return; + } - if(preCallback) { - var r = preCallback(bounds, json); - if(r === false) { - return; - } + if(preCallback && preCallback(bounds, json) === false ) { + return; + } + + var minPriceId = 0; + var minPrice = 999.0; + for(var i=0; i" + json[i].gestore + ":
"; + for(var j=0; j"; } else { - //console.log("Inserito" + json[i].idImpianto); - impianti.push(json[i].idImpianto); + txt += ""; } - var txt = "Litri ogni " + euros + " € per " + json[i].gestore + ":
" + litres + " L
"; - for(var j=0; j"; - } else { - txt += ""; - } - txt += ""; - txt += ""; - txt += ""; + txt += ""; + txt += "
" + litres + " Lper il " + json[i].prezzi[j].descCarburante + ""; - if(json[i].prezzi[j].isSelf === "1") { - txt += "(self)"; - } else { - txt += ""; - } - txt += "
per il " + json[i].prezzi[j].descCarburante + ""; + if(json[i].prezzi[j].isSelf === "1") { + txt += "(self)"; + } else { + txt += ""; } - txt += "
"; - - txt += "

+ PREFERITI"; - txt += "segnala errore

"; - - L.marker( - [json[i].latitudine, json[i].longitudine], - { - bounceOnAdd: true, - bounceOnAddOptions: {duration: 500, height: 100}, - bounceOnAddCallback: function() {} - } - ) - .bindPopup(txt) - .addTo(map); - + txt += ""; + txt += ""; } + txt += ""; - if(postCallback) { - postCallback(bounds, json); - } + txt += "

+ PREFERITI"; + txt += "segnala errore

"; + + L.marker([json[i].latitudine, json[i].longitudine], { + bounceOnAdd: true, + bounceOnAddOptions: {duration: 500, height: 100}, + bounceOnAddCallback: function() {} + }).bindPopup(txt).addTo(map).options.idImpianto = json[i].idImpianto; } - ); + + + + + postCallback && postCallback(bounds, json); + }); } function suggest_nominatim_addresses(query) { $.ajax({ url: "https://nominatim.openstreetmap.org/search", jsonp: "nominatimJsonp", dataType: "jsonp", data: { q: query, format: "json", json_callback: "nominatimJsonp" } }); } function nominatimJsonp(json) { var $searchResults = $("#modal-search-addr-results"); var $list = $searchResults.find("ol").empty(); if(json.length > 0) { for(i=0; i") .append( $('') .text(json[i].display_name) .data("latLng", {lat: json[i].lat, lng: json[i].lon}) .data("bounds", json[i].boundingbox) .click(function(event) { $searchResults.closeModal(); var latLng = $(this).data("latLng"); var bounds = $(this).data("bounds"); var adaptedBounds = [ [bounds[0], bounds[3]], [bounds[1], bounds[2]] ]; map.fitBounds( adaptedBounds ); getMarkersInBounds(); Overworld.hide(); event.preventDefault(); }) ) .append( $("
") ) .append( $("") .text(json[i].licence) ) ); - } + } // Result links $searchResults.find("a").click(function() { $searchResults.closeModal(); }); } else { $list.append( $("
  • ").text( "Nessun indirizzo trovato" ) ); } $searchResults.openModal(); } -function enfatizeLatLng(latLng, duration) { +function enfatizeLatLng(latLng, duration, radius) { duration = duration || 2000; - var circle = L.circle(latLng, 100).addTo(map); + radius = radius || 100; + var circle = L.circle(latLng, radius).addTo(map); window.setTimeout(function () { map.removeLayer(circle); }, duration); } function fitDocument() { var w = $("body").width(); var h = $("body").height(); $("#map").width( w ).height( h ); } $(window).resize(function() { + // jQuery's windowResize is a bit crazy setTimeout(fitDocument, 500); setTimeout(fitDocument, 2000); setTimeout(fitDocument, 4000); }); $(document).on("click", ".add-favorites", function() { Materialize.toast("Aggiunto ai tuoi preferiti. Facile!", 3000); }); $(document).on("click", ".segnala-errore", function() { Materialize.toast("La tua segnalazione è preziosa. Grazie!", 3000); }); - -function impiantoExists(idImpianto) { - for(var i=0; i. */ function get_header($uid, $args = array()) { $args = merge_args_defaults( $args, [ 'theme' => 'default' ] ); switch($args['theme']) { case 'default': enqueue_css('materialize'); enqueue_css('materialize.icons'); enqueue_css('leaflet'); enqueue_css('my-facile'); enqueue_js('jquery'); enqueue_js('jquery.ui'); enqueue_js('leaflet'); enqueue_js('leaflet.bouncemarker'); enqueue_js('materialize'); enqueue_js('my-facile'); break; } $menuEntry = get_menu_entry($uid); $menuEntry || error( sprintf( _("Il menu '%s' non esiste"), $uid ) ); $title = $menuEntry->extra['title']; $desc = $menuEntry->extra['desc']; header("Content-Type: text/html; charset=utf-8"); ?> <?php echo SITE_NAME ?>

    url, $title, $desc, 'orange-text' ) ?>

    url, $title, $desc, 'orange-text' ) ?>

    getValue( "SELECT COUNT(*) as count FROM station", 'count' ), HTML::property('class', 'station-counter') ) ) ?>
    - Ultimo aggiornamento: 3 ore fa! + Ultimo aggiornamento: 4 ore fa!

    navigation