Skip to content

Commit

Permalink
Fixed zoom level conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
musa11971 committed Oct 8, 2021
1 parent da6fda2 commit bd0f2fb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
52 changes: 39 additions & 13 deletions dutch-waze-kit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// Create DWK helper object
let dwk = {
version: '1.3',
// Constants
version: '1.4',

zoomLevels: {
/**
* min: the minimum zoom level (most zoomed out)
* max: the maximum zoom level (most zoomed in)
* amplify: how many levels to boost the result with when this zoom level is converted to
*/
waze: { min: 12, max: 22, amplify: 0 },
bag: { min: 0, max: 7, amplify: 2 },
satellietDataPortaal: { min: 8, max: 18, amplify: 4 },
googleMaps: { min: 0, max: 20, amplify: 6 },
mapillary: { min: 0, max: 20, amplify: 6 },
},

////////////
logHistory: [],

features: [
Expand Down Expand Up @@ -151,7 +167,7 @@ let dwk = {
getCurrentMapZoomLevel() {
let permalink = document.getElementsByClassName('permalink')[0].href;

return parseInt(permalink.match(/zoomLevel=([0-9])/)[1]);
return parseInt(permalink.match(/zoomLevel=([0-9]+)/)[1]);
},

// Shows a toast
Expand Down Expand Up @@ -182,27 +198,21 @@ let dwk = {

// Copies the given text to clipboard
copyToClipboard(text) {
let textArea = document.createElement("textarea");
let textArea = document.createElement('textarea');

// Place in the top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;

// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';

// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;

// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';

// Avoid flash of the white box if rendered for any reason.
textArea.style.background = 'transparent';

textArea.value = text;
Expand All @@ -212,14 +222,30 @@ let dwk = {
textArea.select();

try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
let successful = document.execCommand('copy');
let msg = successful ? 'successful' : 'unsuccessful';
console.log('Dutch Waze Kit: Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
console.log('Dutch Waze Kit: Oops, unable to copy');
}

document.body.removeChild(textArea);
},

// Converts a zoom level from one service to another
convertZoomLevel(level, fromZoomLevel, toZoomLevel) {
let result = (level - fromZoomLevel.min) * (toZoomLevel.max - toZoomLevel.min) / (fromZoomLevel.max - fromZoomLevel.min) + toZoomLevel.min;

// Amplify zoom result
result += toZoomLevel.amplify;

// Restrict zoom level to the maximum
if(result > toZoomLevel.max)
result = toZoomLevel.max;

dwk.log('Zoomlevel ' + level + ' omgezet van stelsel (' + fromZoomLevel.min + '-' + fromZoomLevel.max + ') naar stelsel (' + toZoomLevel.min + '-' + toZoomLevel.max + ') = ' + result);

return result;
}
};

Expand Down
2 changes: 1 addition & 1 deletion features/open-in-bag.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let openInBAG = {
// Find correct zoom level
let zoom = dwk.getCurrentMapZoomLevel();

if(zoom > 7) zoom = 7;
zoom = dwk.convertZoomLevel(zoom, dwk.zoomLevels.waze, dwk.zoomLevels.bag);

// Create and open URL
let url = 'https://bagviewer.kadaster.nl/lvbag/bag-viewer/index.html#?geometry.x=' + transformedCoordinates.x + '&geometry.y=' + transformedCoordinates.y + '&zoomlevel=' + zoom;
Expand Down
7 changes: 6 additions & 1 deletion features/open-in-google-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ let openInGoogleMaps = {
// Get coordinates
let coordinates = dwk.getCurrentMapCoordinates();

// Find correct zoom level
let zoom = dwk.getCurrentMapZoomLevel();

zoom = dwk.convertZoomLevel(zoom, dwk.zoomLevels.waze, dwk.zoomLevels.googleMaps);

// Create and open URL
let url = 'https://www.google.com/maps/dir/' + coordinates.y + ',' + coordinates.x + '//@'+ coordinates.y + ',' + coordinates.x +',18z/data=!4m5!4m4!1m1!4e2!1m0!3e0!5m1!1e1';
let url = 'https://www.google.com/maps/dir/' + coordinates.y + ',' + coordinates.x + '//@'+ coordinates.y + ',' + coordinates.x +',' + zoom + 'z/data=!4m5!4m4!1m1!4e2!1m0!3e0!5m1!1e1';
window.open(url, '_blank');
}
};
7 changes: 6 additions & 1 deletion features/open-in-mapillary.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ let openInMapillary = {
// Get coordinates
let coordinates = dwk.getCurrentMapCoordinates();

// Find correct zoom level
let zoom = dwk.getCurrentMapZoomLevel();

zoom = dwk.convertZoomLevel(zoom, dwk.zoomLevels.waze, dwk.zoomLevels.mapillary);

// Create and open URL
let url = 'https://www.mapillary.com/app/?lat=' + coordinates.y + '&lng=' + coordinates.x + '&z=15';
let url = 'https://www.mapillary.com/app/?lat=' + coordinates.y + '&lng=' + coordinates.x + '&z=' + zoom;
window.open(url, '_blank');
}
};
7 changes: 6 additions & 1 deletion features/open-in-satelliet-data-portaal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ let openInSatellietDataPortaal = {
// Get coordinates
let coordinates = dwk.getCurrentMapCoordinates();

// Find correct zoom level
let zoom = dwk.getCurrentMapZoomLevel();

zoom = dwk.convertZoomLevel(zoom, dwk.zoomLevels.waze, dwk.zoomLevels.satellietDataPortaal);

// Create and open URL
let url = 'https://www.satellietdataportaal.nl/?base=brtachtergrondkaart&loc=' + coordinates.y + '%2C' + coordinates.x + '%2C18z&overlay=mos-0';
let url = 'https://www.satellietdataportaal.nl/?base=brtachtergrondkaart&loc=' + coordinates.y + '%2C' + coordinates.x + '%2C' + zoom + 'z&overlay=mos-0';
window.open(url, '_blank');
}
};
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Dutch Waze Kit",
"author": "Musa Semou",
"version": "1.3",
"version": "1.4",
"description": "Verbeteringen voor WME, op maat gemaakt voor de Nederlandse Waze community.",
"permissions": ["contextMenus"],
"background": {
Expand Down

0 comments on commit bd0f2fb

Please sign in to comment.