diff --git a/api.php b/api.php deleted file mode 100755 index 0cf4b83..0000000 --- a/api.php +++ /dev/null @@ -1,30 +0,0 @@ -. - */ - -require 'load.php'; - -// DB#getResults() ti dà un array di oggetti -$price = $db->getResults( - "SELECT * " . - "FROM price " . - "LIMIT 5" -); - -http_json_header(); - -echo json_encode($price, JSON_PRETTY_PRINT); diff --git a/api/bounds.php b/api/bounds.php new file mode 100755 index 0000000..bf0a1b3 --- /dev/null +++ b/api/bounds.php @@ -0,0 +1,65 @@ +. + */ + +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 = false; + + + 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/api/latlng.php b/api/latlng.php deleted file mode 100755 index 6fc8e7b..0000000 --- a/api/latlng.php +++ /dev/null @@ -1,35 +0,0 @@ -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" - , - $result[$i]->idImpianto - ) ); - } -} - -http_json_header(); - -echo json_encode( - $result - , JSON_PRETTY_PRINT -); diff --git a/facile.css b/facile.css index 6fa540f..92b1744 100755 --- a/facile.css +++ b/facile.css @@ -1,35 +1,41 @@ html, body { height:100%; } #map { position:absolute; top:0; left:0; width:100%; height:100%; } #overworld { position:absolute; top:0; left:50px; z-index:100; margin-right:10px; } +.leaflet-popup-content { + font-family: "Roboto"; + font-size:15px; +} .prices td { padding:4px !important; white-space: nowrap; - font-family: "Roboto"; } #overworld .card-panel { - padding: 5px 2px; + padding: 2px 20px; +} +#overworld h1 { + font-size:3.2rem; } #overworld-buttons { position:absolute; top:5px; left:50px; z-index:100; } b.station-counter { position:relative; width:50px; } diff --git a/facile.js b/facile.js index fcb753a..72fa901 100755 --- a/facile.js +++ b/facile.js @@ -1,253 +1,289 @@ var map; +var trovalatuazona = true; +var nessunfornitore = true; var impianti = []; -var clickMarker = L.marker(); -var roundMarkers = L.layerGroup(); -var roundCircle; -var radiusExtra = 400; var ZOOM_OPENING_SEARCH_RESULT = 17; var Overworld = { $el: undefined, $action: undefined, visible: true, running: false, hide: function() { if(Overworld.running) { return; } Overworld.running = true; Overworld.$el.hide("fast", function() { Overworld.$action.show("fast", function() { Overworld.running = false; }); }); }, show: function() { if(Overworld.running) { return; } Overworld.running = true; Overworld.$el.find("input").val(""); Overworld.$action.hide("fast", function() { Overworld.$el.show("fast", function() { + Overworld.$el.find("input").focus(); Overworld.running = false; }); }); } }; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $(document).ready(function() { var $stationCounter = $(".station-counter"); var count = parseInt( $stationCounter.text() ); for(var i=count; i<10; i++) { } 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'); -fitDocument(); + // create the tile layer with correct attribution -var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; +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.addLayer(roundMarkers); map.setView([42.5, 12.9], 7); -map.on("click", function(e) { - var popLocation = e.latlng; - getMarkers( - popLocation, - 1000, - function(latLng, radius, json) { - if(json.length === 0) { - return false; - } - }, - function(latLng, radius, json) { - if(json.length > 0) { - console.log(radius); - map.setView(latLng, radius); - enfatizeLatLng(latLng, radius); - } - } - ); +map.on("dragend", function() { + getMarkersInBounds(); + return true; }); +map.on("popupopen", function() { + Overworld.hide(); +}); + +fitDocument(); + +getMarkersInBounds(); + + + // On ready }); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -function getMarkers(popLocation, radius, preCallback, postCallback) { - radius = radius || 900; +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/latlng.php", { - lat: popLocation.lat, - lng: popLocation.lng, - radius: radius - }, + "/api/bounds.php", data, function(json) { - console.log(json); + if(json.length === 0) { + nessunfornitore && Materialize.toast("Nessun fornitore in questa zona", 2000); + nessunfornitore = false; + return; + } + nessunfornitore = true; + + if(json.length > 90) { + Materialize.toast("Ottimo! " + json.length + " risultati. Zooma per caricare la tua zona!", 2000); + return; + } if(preCallback) { - var r = preCallback(popLocation, radius, json); + var r = preCallback(bounds, json); if(r === false) { return; } } for(var i=0; i
Prezzi:
"; + if( impiantoExists( json[i].idImpianto ) ) { + //console.log("Esiste già" + json[i].idImpianto); + continue; + } else { + //console.log("Inserito" + json[i].idImpianto); + impianti.push(json[i].idImpianto); + } + var txt = "Prezzi per " + json[i].gestore + ":
"; for(var j=0; j"; } else { txt += ""; } txt += ""; txt += ""; txt += ""; } txt += "
" + json[i].prezzi[j].prezzo + " €per il " + json[i].prezzi[j].descCarburante + ""; if(json[i].prezzi[j].isSelf === "1") { txt += "self-service"; } else { txt += ""; } txt += "
"; - txt += "

+ PREFERITI

"; + 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(roundMarkers); + .addTo(map); } if(postCallback) { - postCallback(popLocation, radius, json); + 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) { duration = duration || 2000; var circle = L.circle(latLng, 100).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() { setTimeout(fitDocument, 500); setTimeout(fitDocument, 1000); setTimeout(fitDocument, 2000); setTimeout(fitDocument, 3000); setTimeout(fitDocument, 4000); }); $(document).on("click", ".add-favorites", function() { - Materialize.toast("Aggiunto ai tuoi preferiti!", 3000); + 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. */ // Lasciate questo file intonso per ora define('INCLUDES', 'includes'); define('MATERIALIZE', INCLUDES . _ . 'materialize'); define('JQUERY', INCLUDES . _ . 'jquery'); define('LEAFLET', INCLUDES . _ . 'leaflet'); define('SITE_NAME', 'Facile.it'); register_js( 'jquery', URL . _ . JQUERY . '/jquery-2.1.4.min.js' ); register_js( 'jquery.ui', URL . _ . INCLUDES . '/jquery-ui/jquery-ui.min.js' ); register_js( 'materialize', URL . _ . MATERIALIZE . '/js/materialize.min.js' ); register_js( 'leaflet', URL . _ . LEAFLET . '/leaflet.js' ); register_js( 'leaflet.bouncemarker', URL . _ . INCLUDES . '/leaflet.bouncemarker/bouncemarker.js' ); register_js( 'my-facile', URL . '/facile.js' ); register_css( 'materialize', URL . _ . MATERIALIZE . '/css/materialize.min.css' ); register_css( 'leaflet', URL . _ . LEAFLET . '/leaflet.css' ); register_css( 'my-facile', URL . '/facile.css' ); register_css( 'materialize.icons', 'https://fonts.googleapis.com/icon?family=Material+Icons' ); add_menu_entries([ new MenuEntry( 'home', URL, _("Home"), null, [ 'title' => sprintf( - _("%s carburante"), + _("%s carburanti"), SITE_NAME ), 'desc' => _("Confronta e scegli il miglior prezzo") ] ) ]); require ABSPATH . _ . INCLUDES . _ . '/header.php'; require ABSPATH . _ . INCLUDES . _ . '/footer.php'; require ABSPATH . _ . INCLUDES . _ . '/functions.php';