Skip to content
Browse files

Modularized javascript

Made gmaps api key determined programatically
  • Loading branch information...
1 parent 5ac8e4d commit 463cb69f97115bb104e9f4137215c8e3503a5e40 David Troy committed May 30, 2008
Showing with 157 additions and 152 deletions.
  1. +123 −0 geohash-demo.js
  2. +26 −130 geohash.js
  3. +1 −3 index.html
  4. +7 −19 matrix.txt
View
123 geohash-demo.js
@@ -0,0 +1,123 @@
+// geohash.js
+// Geohash library for Javascript
+// (c) 2008 David Troy
+// Code is available for free distribution under the MIT License
+
+function GScript(src) {document.write('<' + 'script src="' + src + '"' +' type="text/javascript"><' + '/script>');}
+
+if (window.location.substring(0,4)=='file')
+ var key = "ABQIAAAAS-9BXlmhAxzk5tMQ6009tBQ60YHOa08tQ3Rk7kk6p9CpE9bRLhRgOlUOLYUPHsGwp_XgmEwZWB1hnA";
+else
+ var key = "ABQIAAAAS-9BXlmhAxzk5tMQ6009tBSuPyGFyYqpbBL0yyePbwJ9Yzj2TRSRG70K1wsky3JHARggI0ccbJ3Y0A";
+
+GScript('http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + key);
+GScript('./geohash.js');
+GScript('./labeledmarker.js');
+
+var ZOOMLEVELS = { 3: 7, 4 : 10, 5 : 12, 6 : 15, 7 : 17, 8 : 17 };
+
+function wheelZoom(a) { (a.detail || -a.wheelDelta) < 0 ? map.zoomIn() : map.zoomOut(); }
+
+function sizeMap() {
+ map = new GMap2(document.getElementById("map"));
+
+ map.setCenter(new GLatLng(39.024,-76.51), 9);
+ map.addControl(new GSmallMapControl());
+}
+
+GeoHashBox.prototype.centerMap = function () {
+ map.setCenter(this.centerPoint, ZOOMLEVELS[this.geohash.length]);
+}
+
+GeoHashBox.prototype.showNeighbors = function () {
+ var geohashPrefix = this.geohash.substring(0,this.geohash.length-1);
+
+ this.neighbors.top = new GeoHashBox(calculateAdjacent(this.geohash, 'top'));
+ this.neighbors.bottom = new GeoHashBox(calculateAdjacent(this.geohash, 'bottom'));
+ this.neighbors.right = new GeoHashBox(calculateAdjacent(this.geohash, 'right'));
+ this.neighbors.left = new GeoHashBox(calculateAdjacent(this.geohash, 'left'));
+ this.neighbors.topleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'top'));
+ this.neighbors.topright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'top'));
+ this.neighbors.bottomright = new GeoHashBox(calculateAdjacent(this.neighbors.right.geohash, 'bottom'));
+ this.neighbors.bottomleft = new GeoHashBox(calculateAdjacent(this.neighbors.left.geohash, 'bottom'));
+}
+
+GeoHashBox.prototype.plot = function () {
+ var polyline = new GPolygon([
+ this.corners.topleft,
+ this.corners.topright,
+ this.corners.bottomright,
+ this.corners.bottomleft,
+ this.corners.topleft
+ ], "#007799", 3, 0.7, "#003366", 0.5, {geodesic:true});
+ map.addOverlay(polyline);
+ var marker = new LabeledMarker(new GLatLng(this.box.latitude[2],this.box.longitude[2]), this.options );
+ map.addOverlay(marker);
+}
+
+function GeoHashBox (geohash) {
+ this.geohash = geohash;
+ this.box = decodeGeoHash(geohash);
+ this.corners = {};
+ this.corners.topleft = new GLatLng(this.box.latitude[0], this.box.longitude[0]);
+ this.corners.topright = new GLatLng(this.box.latitude[1], this.box.longitude[0]);
+ this.corners.bottomright = new GLatLng(this.box.latitude[1], this.box.longitude[1]);
+ this.corners.bottomleft = new GLatLng(this.box.latitude[0], this.box.longitude[1]);
+ this.centerPoint = new GLatLng((this.box.latitude[0] + this.box.latitude[1])/2, (this.box.longitude[0] + this.box.longitude[1])/2);
+
+ this.options = {labelText : geohash};
+ var lastChr = this.geohash.charAt(this.geohash.length-1);
+ this.selfPos = BASE32.indexOf(lastChr);
+ this.neighbors = {};
+ this.plot();
+}
+
+function geocodeAddress () {
+ var address = document.getElementById("address").value;
+ var geocoder = new GClientGeocoder();
+ geocoder.getLatLng(address, plotGeoHash);
+}
+
+function plotGeoHash (gLatLng) {
+ var geohash = encodeGeoHash(gLatLng.lat(), gLatLng.lng());
+ document.getElementById("geoHash").value = geohash;
+ var resolution = document.getElementById("hashResolution").value;
+ geohash = geohash.substr(0,resolution);
+ var geoHashBox = new GeoHashBox(geohash);
+ geoHashBox.centerMap();
+ geoHashBox.showNeighbors();
+
+ boxList = document.getElementById("boxList");
+ boxList.innerHTML = "LEFT(geohash," + resolution + ") IN (";
+ var boxes = [];
+ for (var n in geoHashBox.neighbors) {
+ boxes.push("'"+geoHashBox.neighbors[n].geohash+"'");
+ }
+ boxList.innerHTML += boxes.join(',') + ")";
+
+ searchInfo = document.getElementById("searchInfo");
+ var xdistance = geoHashBox.neighbors.topleft.corners.topleft.distanceFrom(geoHashBox.neighbors.topright.corners.topright);
+ var ydistance = geoHashBox.neighbors.topleft.corners.topleft.distanceFrom(geoHashBox.neighbors.bottomleft.corners.bottomleft);
+ var searcharea = parseInt((xdistance/1000) * (ydistance/1000)*100)/100;
+ if (xdistance>2000) {
+ xdistance = parseInt(xdistance/10)/100;
+ ydistance = parseInt(ydistance/10)/100;
+ units = "km";
+ } else {
+ xdistance = parseInt(xdistance+0.5);
+ ydistance = parseInt(ydistance+0.5);
+ units = "m";
+ }
+ searchInfo.innerHTML = "w:" + xdistance + units + ", h:" + ydistance + units + " (" + searcharea + "km2)";
+}
+
+
+window.onload = function () {
+ if (GBrowserIsCompatible()) {
+ sizeMap();
+ GEvent.addDomListener(document.getElementById('map'), "DOMMouseScroll", wheelZoom);
+ GEvent.addDomListener(document.getElementById('map'), "mousewheel", wheelZoom);
+ } else {
+ alert("Sorry, your browser is lame!")
+ }
+}
View
156 geohash.js
@@ -1,12 +1,19 @@
-var BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz";
-var NEIGHBORS = { right : { even : "bc01fg45238967deuvhjyznpkmstqrwx" },
- left : { even : "238967debc01fg45kmstqrwxuvhjyznp" },
- top : { even : "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
- bottom : { even : "14365h7k9dcfesgujnmqp0r2twvyx8zb" } };
-var BORDERS = { right : { even : "bcfguvyz" },
- left : { even : "0145hjnp" },
- top : { even : "prxz" },
- bottom : { even : "028b" } };
+// geohash.js
+// Geohash library for Javascript
+// (c) 2008 David Troy
+// Distributed under the MIT License
+
+BITS = [16, 8, 4, 2, 1];
+
+BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz";
+NEIGHBORS = { right : { even : "bc01fg45238967deuvhjyznpkmstqrwx" },
+ left : { even : "238967debc01fg45kmstqrwxuvhjyznp" },
+ top : { even : "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
+ bottom : { even : "14365h7k9dcfesgujnmqp0r2twvyx8zb" } };
+BORDERS = { right : { even : "bcfguvyz" },
+ left : { even : "0145hjnp" },
+ top : { even : "prxz" },
+ bottom : { even : "028b" } };
NEIGHBORS.bottom.odd = NEIGHBORS.left.even;
NEIGHBORS.top.odd = NEIGHBORS.right.even;
@@ -18,26 +25,23 @@ BORDERS.top.odd = BORDERS.right.even;
BORDERS.left.odd = BORDERS.bottom.even;
BORDERS.right.odd = BORDERS.top.even;
-var ZOOMLEVELS = { 3: 7, 4 : 10, 5 : 12, 6 : 15, 7 : 17, 8 : 17 };
-
-BITS = [16, 8, 4, 2, 1];
-
-function wheelZoom(a) { (a.detail || -a.wheelDelta) < 0 ? map.zoomIn() : map.zoomOut(); }
-
-function sizeMap() {
- map = new GMap2(document.getElementById("map"));
-
- map.setCenter(new GLatLng(39.024,-76.51), 9);
- map.addControl(new GSmallMapControl());
-}
-
function refine_interval(interval, cd, mask) {
if (cd&mask)
interval[0] = (interval[0] + interval[1])/2;
else
interval[1] = (interval[0] + interval[1])/2;
}
+function calculateAdjacent(srcHash, dir) {
+ srcHash = srcHash.toLowerCase();
+ var lastChr = srcHash.charAt(srcHash.length-1);
+ var type = (srcHash.length % 2) ? 'odd' : 'even';
+ var base = srcHash.substring(0,srcHash.length-1);
+ if (BORDERS[dir][type].indexOf(lastChr)!=-1)
+ base = calculateAdjacent(base, dir);
+ return base + BASE32[NEIGHBORS[dir][type].indexOf(lastChr)];
+}
+
function decodeGeoHash(geohash) {
var is_even = 1;
var lat = []; var lon = [];
@@ -106,111 +110,3 @@ function encodeGeoHash(latitude, longitude) {
}
return geohash;
}
-
-function calculateHash(srcHash, dir) {
- srcHash = srcHash.toLowerCase();
- var lastChr = srcHash.charAt(srcHash.length-1);
- var type = (srcHash.length % 2) ? 'odd' : 'even';
- var base = srcHash.substring(0,srcHash.length-1);
- if (BORDERS[dir][type].indexOf(lastChr)!=-1)
- base = calculateHash(base, dir);
- return base + BASE32[NEIGHBORS[dir][type].indexOf(lastChr)];
-}
-
-GeoHashBox.prototype.centerMap = function () {
- map.setCenter(new GLatLng(this.centerPoint[0], this.centerPoint[1]), ZOOMLEVELS[this.geohash.length]);
-}
-
-GeoHashBox.prototype.showNeighbors = function () {
- var geohashPrefix = this.geohash.substring(0,this.geohash.length-1);
-
- this.neighbors.top = new GeoHashBox(calculateHash(this.geohash, 'top'));
- this.neighbors.bottom = new GeoHashBox(calculateHash(this.geohash, 'bottom'));
- this.neighbors.right = new GeoHashBox(calculateHash(this.geohash, 'right'));
- this.neighbors.left = new GeoHashBox(calculateHash(this.geohash, 'left'));
- this.neighbors.topleft = new GeoHashBox(calculateHash(this.neighbors.left.geohash, 'top'));
- this.neighbors.topright = new GeoHashBox(calculateHash(this.neighbors.right.geohash, 'top'));
- this.neighbors.bottomright = new GeoHashBox(calculateHash(this.neighbors.right.geohash, 'bottom'));
- this.neighbors.bottomleft = new GeoHashBox(calculateHash(this.neighbors.left.geohash, 'bottom'));
-}
-
-GeoHashBox.prototype.plot = function () {
- var polyline = new GPolygon([
- this.corners.topleft,
- this.corners.topright,
- this.corners.bottomright,
- this.corners.bottomleft,
- this.corners.topleft
- ], "#007799", 3, 0.7, "#003366", 0.5, {geodesic:true});
- map.addOverlay(polyline);
- var marker = new LabeledMarker(new GLatLng(this.box.latitude[2],this.box.longitude[2]), this.options );
- map.addOverlay(marker);
-}
-
-function GeoHashBox (geohash) {
- this.geohash = geohash;
- this.box = decodeGeoHash(geohash);
- this.corners = {};
- this.corners.topleft = new GLatLng(this.box.latitude[0], this.box.longitude[0]);
- this.corners.topright = new GLatLng(this.box.latitude[1], this.box.longitude[0]);
- this.corners.bottomright = new GLatLng(this.box.latitude[1], this.box.longitude[1]);
- this.corners.bottomleft = new GLatLng(this.box.latitude[0], this.box.longitude[1]);
-
- this.centerPoint = [ (this.box.latitude[0] + this.box.latitude[1])/2, (this.box.longitude[0] + this.box.longitude[1])/2];
- this.options = {labelText : geohash};
- var lastChr = this.geohash.charAt(this.geohash.length-1);
- this.selfPos = BASE32.indexOf(lastChr);
- this.neighbors = {};
- this.plot();
-}
-
-function geocodeAddress () {
- var address = document.getElementById("address").value;
- var geocoder = new GClientGeocoder();
- geocoder.getLatLng(address, plotGeoHash);
-}
-
-function plotGeoHash (gLatLng) {
- var geohash = encodeGeoHash(gLatLng.lat(), gLatLng.lng());
- document.getElementById("geoHash").value = geohash;
- var resolution = document.getElementById("hashResolution").value;
- geohash = geohash.substr(0,resolution);
- var geoHashBox = new GeoHashBox(geohash);
- geoHashBox.centerMap();
- geoHashBox.showNeighbors();
-
- boxList = document.getElementById("boxList");
- boxList.innerHTML = "LEFT(geohash," + resolution + ") IN (";
- var boxes = [];
- for (var n in geoHashBox.neighbors) {
- boxes.push("'"+geoHashBox.neighbors[n].geohash+"'");
- }
- boxList.innerHTML += boxes.join(',') + ")";
-
- searchInfo = document.getElementById("searchInfo");
- var xdistance = geoHashBox.neighbors.topleft.corners.topleft.distanceFrom(geoHashBox.neighbors.topright.corners.topright);
- var ydistance = geoHashBox.neighbors.topleft.corners.topleft.distanceFrom(geoHashBox.neighbors.bottomleft.corners.bottomleft);
- var searcharea = parseInt((xdistance/1000) * (ydistance/1000)*100)/100;
- if (xdistance>2000) {
- xdistance = parseInt(xdistance/10)/100;
- ydistance = parseInt(ydistance/10)/100;
- units = "km";
- } else {
- xdistance = parseInt(xdistance+0.5);
- ydistance = parseInt(ydistance+0.5);
- units = "m";
- }
-
- searchInfo.innerHTML = "w:" + xdistance + units + ", h:" + ydistance + units + " (" + searcharea + "km2)";
-}
-
-window.onload = function () {
- if (GBrowserIsCompatible()) {
- sizeMap();
- GEvent.addDomListener(document.getElementById('map'), "DOMMouseScroll", wheelZoom);
- GEvent.addDomListener(document.getElementById('map'), "mousewheel", wheelZoom);
- } else {
- alert("Sorry, your browser is lame!")
- }
-
-}
View
4 index.html
@@ -1,9 +1,7 @@
<html>
<head>
<title>geohash demonstrator</title>
- <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAS-9BXlmhAxzk5tMQ6009tBQ60YHOa08tQ3Rk7kk6p9CpE9bRLhRgOlUOLYUPHsGwp_XgmEwZWB1hnA&amp;hl=" type="text/javascript"/>
- <script src="./geohash.js" type="text/javascript"/>
- <script src="./labeledmarker.js" type="text/javascript"/>
+ <script src="./geohash-demo.js" type="text/javascript"/>
<style>
body {
font-family: 'Helvetica';
View
26 matrix.txt
@@ -1,17 +1,10 @@
+These matrices demonstrate the formation of the geohash base 32
+pattern in both vertical and horizontal orientations.
-02 03 08 09 06 07 12 13 10 11 00 01 14 15 04 05 18 19 24 25 22 23 28 29 26 27 16 17 30 31 20 21
-10 11 00 01 14 15 04 05 02 03 08 09 06 07 12 13 26 27 16 17 30 31 20 21 18 19 24 25 22 23 28 29
-
-01 04 03 06 05 16 07 18 09 12 11 14 13 24 15 26 17 20 19 22 21 00 23 02 25 28 27 30 29 08 31 10
-00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-
-00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-21 00 23 02 01 04 03 06 29 08 31 10 09 12 11 14 05 16 07 18 17 20 19 22 13 24 15 26 25 28 27 30
-
-
-odd neighbors
-00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-
+Even-length geohash strings are arranged in the 'even' pattern,
+while odd-length geohash strings take on the 'odd' pattern.
+This matrix was used to determine the neighbor relationship
+lookup tables.
Even Lengths
21 23 29 31 21 23 29 31 21 23 29 31
@@ -57,9 +50,4 @@ Odd Lengths
02 03 06 07 18 19 22 23 02 03 06 07 18 19 22 23 02 03 06 07 18 19 22 23
00 01 04 05 16 17 20 21 00 01 04 05 16 17 20 21 00 01 04 05 16 17 20 21
-DQCB -> DQF0
-c == 11
-f == 14
-
-b == 10
-0 == 0
+geohash-js code (c) 2008 David Troy; MIT License.

0 comments on commit 463cb69

Please sign in to comment.
Something went wrong with that request. Please try again.