From 0e1c1f16cb66c584184ed4a6d36532b363a00b95 Mon Sep 17 00:00:00 2001 From: MattMo Date: Tue, 4 May 2021 14:21:39 -0700 Subject: [PATCH] Added zipcode reverse function to get zipcode from latlng. --- Zip2LatLng.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Zip2LatLng.js b/Zip2LatLng.js index bef3708..2ec1bec 100644 --- a/Zip2LatLng.js +++ b/Zip2LatLng.js @@ -61511,9 +61511,39 @@ class Zip2LatLng { lng: (r[1] / 100000) * -1, city: c[r[2]], state: s[r[3]], - abbreviation: a[r[4]] + abbreviation: a[r[4]], + zipcode: zip }; } + + static reverse(lat, lng) { + var b = Number.MAX_VALUE; + var r = null; + var zip = 0; + for (var i = 0; i < z.length; i++) { + if (z[i]) { + var d = Math.abs(((z[i][1] / 100000) * -1) - lng) + Math.abs((z[i][0] / 100000) - lat); + if (d <= b) { + r = z[i]; + zip = i; + b = d; + } + } + } + + if (r) { + return { + lat: r[0] / 100000, + lng: (r[1] / 100000) * -1, + city: c[r[2]], + state: s[r[3]], + abbreviation: a[r[4]], + zipcode: zip + }; + } else { + return null; + } + } } window.Zip2LatLng = Zip2LatLng;