From c4feaab68eb050293502660425d9dc65ccde8e7c Mon Sep 17 00:00:00 2001 From: Victor B Andersen Date: Mon, 5 Aug 2019 14:22:11 -0700 Subject: [PATCH 1/2] Fixing lat lng bounds google errors --- projectV7.js | 58 ++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/projectV7.js b/projectV7.js index c691cf4..7ea936a 100644 --- a/projectV7.js +++ b/projectV7.js @@ -1,6 +1,7 @@ $(function () { let circles = []; - let bounds; + let bounds = { north: NaN, east: NaN, south: NaN, west: NaN }; + console.log('Get funky'); // Document Ready const ADZUNA_BASE_URL = 'https://api.adzuna.com/v1/api/'; @@ -130,47 +131,36 @@ $(function () { } - function fixBounds(minimizedBoundsObject){ - let cityBounds = {}; - cityBounds.sw = minimizedBoundsObject.ga; - cityBounds.sw.lat = minimizedBoundsObject.ga.j; - cityBounds.sw.lng = minimizedBoundsObject.ga.l; - cityBounds.ne = minimizedBoundsObject.na; - cityBounds.ne.lat = minimizedBoundsObject.na.j; - cityBounds.ne.lng = minimizedBoundsObject.na.l; - delete cityBounds.ga; - delete cityBounds.na; - delete cityBounds.ne.l; - delete cityBounds.ne.j; - delete cityBounds.sw.l; - delete cityBounds.sw.j; - return new google.maps.LatLngBounds(cityBounds); - } - // GOOD ABSTRACTION, inside which has frustrating bullshit we want to ignore for now function addCityToMapBounds(cityBounds){ - return ; // not working yet - console.log('city bounds before conversion', cityBounds); - - cityBounds = new google.maps.LatLngBounds([ - new google.maps.LatLng(cityBounds.southwest.lat, cityBounds.southwest.lng), - new google.maps.LatLng(cityBounds.northeast.lat, cityBounds.northeast.lng) - ]); - console.log('display results for city bounds:', cityBounds); + if( isNaN(bounds.north) || cityBounds.northeast.lat > bounds.north ){ + // set incoming cityBounds as global bounds true north + bounds.north = cityBounds.northeast.lat; + } - // add location to bounds - if( bounds ){ - bounds.extend( cityBounds.southwest ); - bounds.extend( cityBounds.northeast ); - } else { - bounds = cityBounds; + if( isNaN(bounds.south) || cityBounds.southwest.lat < bounds.south ){ + // set incoming cityBounds as global bounds true south + bounds.south = cityBounds.southwest.lat; } + + if( isNaN(bounds.east) || cityBounds.northeast.lng > bounds.east ){ + // set incoming cityBounds as global bounds true east + bounds.east = cityBounds.northeast.lng; + } + + if( isNaN(bounds.west) || cityBounds.southwest.lat < bounds.west ){ + // set incoming cityBounds as global bounds true west + bounds.west = cityBounds.southwest.lng; + } + } function zoomToMapBounds(){ // center map on the global bounds - // map.fitBounds(bounds); // not working cause bounds suck + console.log("Asking google to center map on bounds: ", JSON.stringify(bounds) ) + // wait until this point to make an actual google lat lng bounds object + map.fitBounds(bounds); // not working cause bounds suck } @@ -468,7 +458,7 @@ $(function () { // Initializes Map function initMap() { - bounds = new google.maps.LatLngBounds(); + // bounds = new google.maps.LatLngBounds(); map = new google.maps.Map(document.getElementById('map'), { center: { lat: 37.77439, lng: -122.419416 }, zoom: 9, From 76b8e981aa1c30029428dab411723fd84ee5f038 Mon Sep 17 00:00:00 2001 From: Victor B Andersen Date: Mon, 5 Aug 2019 14:44:11 -0700 Subject: [PATCH 2/2] reset search results (bounds and map circles) --- projectV7.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/projectV7.js b/projectV7.js index 7ea936a..d2c1a06 100644 --- a/projectV7.js +++ b/projectV7.js @@ -1,6 +1,7 @@ $(function () { let circles = []; - let bounds = { north: NaN, east: NaN, south: NaN, west: NaN }; + let bounds; + resetMapBounds(); console.log('Get funky'); // Document Ready @@ -27,9 +28,22 @@ $(function () { .catch(err => console.error(err)); } + function resetSearchResults(){ + resetMapBounds(); + removeCirclesFromMap(); + // also reset form things, titles, images, data, means, etc. + } + function tryAllCities(country1, city1, country2, city2, jobCategory){ + + let numExpected = 2; + getLocAndJobForCity(country1, city1, jobCategory) .then(city1results => { + if( numExpected == 2 ){ + resetSearchResults(); + } + numExpected--; displayResults(city1results); }) .catch( city1error => { @@ -40,6 +54,10 @@ $(function () { // --- not dependent on each other getLocAndJobForCity(country2, city2, jobCategory) .then(city2results => { + if( numExpected == 2 ){ + resetSearchResults(); + } + numExpected--; displayResults(city2results); }) .catch( city2error => { @@ -156,6 +174,8 @@ $(function () { } + + function zoomToMapBounds(){ // center map on the global bounds console.log("Asking google to center map on bounds: ", JSON.stringify(bounds) ) @@ -163,9 +183,13 @@ $(function () { map.fitBounds(bounds); // not working cause bounds suck } + function resetMapBounds(){ + bounds = { north: NaN, east: NaN, south: NaN, west: NaN }; + } // Displays results in HTML for ONE city function displayResults(data) { + // put circle on map and track it let newCircle = addCircleToMap(data); circles.push(newCircle); @@ -414,6 +438,11 @@ $(function () { } } + function removeCirclesFromMap(){ + circles.forEach( circle => circle.setMap(null) ); + circles.length = 0; + } + // Submit Button function submitHandler() {