Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Last active November 4, 2022 10:38
Show Gist options
  • Save nicholastay/db046f8d8bb9ed29302a978f552756b8 to your computer and use it in GitHub Desktop.
Save nicholastay/db046f8d8bb9ed29302a978f552756b8 to your computer and use it in GitHub Desktop.
MUMapBooster - Adds location to Monash Maps (https://maps.monash.edu) searches to improve relevance of results
// ==UserScript==
// @name Monash Maps location search booster
// @namespace nick.tay.blue
// @match https://maps.monash.edu/
// @grant none
// @version 0.1.2
// @author Nicholas Tay <[email protected]>
// @description Adds location as context to the MazeMap API before searching to improve relevance of searches
// ==/UserScript==
// MazeMap API's search result sorting is kinda weird, probably due to 'relevance' vs 'location'.
// It doesn't seem to work very well, though.
// This option allows you to just apply naive location sorting to the results instead.
// It's not a very good solution in some cases but is better in generic cases like 'toilet', so
// IF YOU USE THIS: if you want to turn it off, just add a space!
const MUMAPBOOST_USE_CUSTOM_LOC_SORT = true;
let orig = Mazemap.Search.SearchController.prototype.search;
Mazemap.Search.SearchController.prototype.search = function(query) {
let useCustom = MUMAPBOOST_USE_CUSTOM_LOC_SORT && !query.includes(" ");
// lat lng by mapbox's centre, refresh on every search
let loc = window.muMap.getCenter();
this.options = {
...this.options,
...loc,
rows: useCustom ? 20 : 75, // restrict harder with this, as more to be wrong
boostbydistance: true,
searchdiameter: 50
};
console.log("[MUMapBooster] Boosting search.");
let p = orig.apply(this, arguments);
if (!useCustom)
return p;
return p.then(response => {
response.results.features.sort((a, b) => {
// Apply basic pythag...
return Math.sqrt(
Math.pow(a.geometry.coordinates[1] - loc.lat, 2)
+ Math.pow(a.geometry.coordinates[0] - loc.lng, 2)
)
>
Math.sqrt(
Math.pow(b.geometry.coordinates[1] - loc.lat, 2)
+ Math.pow(b.geometry.coordinates[0] - loc.lng, 2)
);
});
console.log(response);
return response;
});
};
console.log("[MUMapBooster] Injected.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment