diff --git a/dist/Leaflet.BlurredLocation.js b/dist/Leaflet.BlurredLocation.js index be846ec4..b3595879 100644 --- a/dist/Leaflet.BlurredLocation.js +++ b/dist/Leaflet.BlurredLocation.js @@ -564,6 +564,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 || { @@ -573,6 +574,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,{}) @@ -627,7 +630,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()) @@ -653,6 +656,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(); @@ -764,10 +795,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() { @@ -864,6 +897,9 @@ BlurredLocation = function BlurredLocation(options) { getPrecision: getPrecision, setZoom: setZoom, getZoom: getZoom, + getZoomFromCoordinates: getZoomFromCoordinates, + getPrecisionFromCoordinates: getPrecisionFromCoordinates, + getPrecisionFromNum: getPrecisionFromNum, Interface: Interface, getFullLon: getFullLon, getFullLat: getFullLat, @@ -873,6 +909,7 @@ BlurredLocation = function BlurredLocation(options) { map: options.map, updateRectangleOnPan: updateRectangleOnPan, setZoomByPrecision: setZoomByPrecision, + getZoomByPrecision: getZoomByPrecision, disableCenterShade: disableCenterShade, enableCenterShade: enableCenterShade, geocodeStringAndPan: Geocoding.geocodeStringAndPan, diff --git a/examples/index.html b/examples/index.html index 1d92e957..a9fad00c 100644 --- a/examples/index.html +++ b/examples/index.html @@ -120,7 +120,8 @@

D. By dragging the map

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); diff --git a/package.json b/package.json index 5216bd47..33c88dcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leaflet-blurred-location", - "version": "1.3.1", + "version": "1.4.1", "description": "", "main": "Gruntfile.js", "scripts": { diff --git a/spec/javascripts/test_spec.js b/spec/javascripts/test_spec.js index 9331bfd7..1708bcea 100644 --- a/spec/javascripts/test_spec.js +++ b/spec/javascripts/test_spec.js @@ -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); @@ -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); + }); + }); diff --git a/src/blurredLocation.js b/src/blurredLocation.js index b7150d55..e6b569ec 100644 --- a/src/blurredLocation.js +++ b/src/blurredLocation.js @@ -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 || { @@ -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,{}) @@ -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()) @@ -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(); @@ -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() { @@ -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, @@ -313,6 +349,7 @@ BlurredLocation = function BlurredLocation(options) { map: options.map, updateRectangleOnPan: updateRectangleOnPan, setZoomByPrecision: setZoomByPrecision, + getZoomByPrecision: getZoomByPrecision, disableCenterShade: disableCenterShade, enableCenterShade: enableCenterShade, geocodeStringAndPan: Geocoding.geocodeStringAndPan,