Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create getZoomFromCoordinates(lat, lon) #224

Merged
merged 8 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions dist/Leaflet.BlurredLocation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ <h3>D. By dragging the map</b></h3>
lngId: 'lng'
},
AddScaleDisplay: true ,
AddBlurryScale: true
AddBlurryScale: true,
precisionTable: {'-2': 2, '-1': 3, '0': 6, '1': 10, '2': 13, '3': 16}
}

var blurredLocation = new BlurredLocation(options);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leaflet-blurred-location",
"version": "1.3.1",
"version": "1.4.1",
"description": "",
"main": "Gruntfile.js",
"scripts": {
Expand Down
32 changes: 30 additions & 2 deletions spec/javascripts/test_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ describe("Basic testing", function() {

it("Checks if setZoomByPrecision pans the map to correct zoom", function() {
blurredLocation.setZoomByPrecision(2);
expect(blurredLocation.getPrecision()).toBe(2);
expect(blurredLocation.getZoom()).toBe(13);
blurredLocation.setZoomByPrecision(1);
expect(blurredLocation.getPrecision()).toBe(1);
expect(blurredLocation.getZoom()).toBe(10);
});

it("Checks if getZoomByPrecision returns the correct zoom", function() {
expect(blurredLocation.getZoomByPrecision(2)).toBe(13);
expect(blurredLocation.getZoomByPrecision(-1)).toBe(3);
});

it("Checks if getDistanceMetrics returns correct scale", function() {
blurredLocation.setZoomByPrecision(2);
expect(blurredLocation.getDistanceMetrics()).toBe(1.41);
Expand All @@ -104,4 +109,27 @@ describe("Basic testing", function() {
// expect(blurredLocation.getLon()).toBe(-58.3815591);
// });

it("Checks if getZoom returns the correct zoom", function() {
blurredLocation.setZoom(5);
expect(blurredLocation.getZoom()).toBe(5);
});

it("Checks if getPrecisionFromNum returns the correct precision value", function() {
expect(blurredLocation.getPrecisionFromNum(5.321)).toBe(3);
expect(blurredLocation.getPrecisionFromNum(5)).toBe(0);
expect(blurredLocation.getPrecisionFromNum(60)).toBe(-1);
});

it("Checks if getPrecisionFromCoordinates returns the highest precision", function() {
expect(blurredLocation.getPrecisionFromCoordinates(5.321, 1.12)).toBe(3);
expect(blurredLocation.getPrecisionFromCoordinates(5.32, 10.1)).toBe(2);
expect(blurredLocation.getPrecisionFromCoordinates(50, 55)).toBe(0);
});

it("Checks if getZoomFromCoordinates returns the correct zoom", function() {
expect(blurredLocation.getZoomFromCoordinates(5.321, 1.122)).toBe(16);
expect(blurredLocation.getZoomFromCoordinates(5.32, 10.15)).toBe(13);
expect(blurredLocation.getZoomFromCoordinates(51, 55)).toBe(6);
});

});
45 changes: 41 additions & 4 deletions src/blurredLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BlurredLocation = function BlurredLocation(options) {
var blurred = true;
var DEFAULT_PRECISION = 6;
require('leaflet-graticule');
var default_precision_table = {'-2': 2, '-1': 3, '0':6, '1':10, '2':13, '3':16};

options = options || {};
options.location = options.location || {
Expand All @@ -13,6 +14,8 @@ BlurredLocation = function BlurredLocation(options) {

options.zoom = options.zoom || 6;

options.precisionTable = options.precisionTable || default_precision_table;

options.mapID = options.mapID || 'map'

options.map = options.map || new L.Map(options.mapID,{})
Expand Down Expand Up @@ -67,7 +70,7 @@ BlurredLocation = function BlurredLocation(options) {
options.blurryScaleNames = options.blurryScaleNames || {
"urban":["country", "state", "district", "neighborhood","block", "building"],
"rural":["country", "county", "town", "village", "house", "house"],
}
}

function getLat() {
if(isBlurred())
Expand All @@ -93,6 +96,34 @@ BlurredLocation = function BlurredLocation(options) {
function getZoom() {
return options.map.getZoom();
}

function getZoomFromCoordinates(lat, lon) {
return getZoomByPrecision(getPrecisionFromCoordinates(lat, lon));
}

function getPrecisionFromCoordinates(lat, lon) {
var latPrecision = getPrecisionFromNum(lat);
var lonPrecision = getPrecisionFromNum(lon);
return latPrecision > lonPrecision ? latPrecision : lonPrecision;
}

function getPrecisionFromNum(num) {
var numArr = num.toString().split('.');
var precision = 0;

if(numArr.length === 1) {
if(Math.floor(num/100) == num/100) {
precision = -2;
} else if (Math.floor(num/10) == num/10) {
precision = -1;
} else {
precision = 0;
}
} else {
precision = (numArr[1].length >= 3) ? 3 : numArr[1].length;
}
return precision;
}

function getSize() {
return options.map.getSize();
Expand Down Expand Up @@ -204,10 +235,12 @@ BlurredLocation = function BlurredLocation(options) {
$("#"+InterfaceOptions.lngId).val(getLon()) ;
}


function setZoomByPrecision(precision) {
var precisionTable = {'-2': 2, '-1': 3, '0':6, '1':10, '2':13, '3':16};
setZoom(precisionTable[precision]);
setZoom(options.precisionTable[precision]);
}

function getZoomByPrecision(precision) {
return options.precisionTable[precision];
}

function enableCenterShade() {
Expand Down Expand Up @@ -304,6 +337,9 @@ BlurredLocation = function BlurredLocation(options) {
getPrecision: getPrecision,
setZoom: setZoom,
getZoom: getZoom,
getZoomFromCoordinates: getZoomFromCoordinates,
getPrecisionFromCoordinates: getPrecisionFromCoordinates,
getPrecisionFromNum: getPrecisionFromNum,
Interface: Interface,
getFullLon: getFullLon,
getFullLat: getFullLat,
Expand All @@ -313,6 +349,7 @@ BlurredLocation = function BlurredLocation(options) {
map: options.map,
updateRectangleOnPan: updateRectangleOnPan,
setZoomByPrecision: setZoomByPrecision,
getZoomByPrecision: getZoomByPrecision,
disableCenterShade: disableCenterShade,
enableCenterShade: enableCenterShade,
geocodeStringAndPan: Geocoding.geocodeStringAndPan,
Expand Down