forked from davetroy/geohash-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geohash-demo.js
206 lines (175 loc) · 7.26 KB
/
geohash-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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.toString().substr(0,4)=='file')
var key = "ABQIAAAAS-9BXlmhAxzk5tMQ6009tBQ60YHOa08tQ3Rk7kk6p9CpE9bRLhRgOlUOLYUPHsGwp_XgmEwZWB1hnA";
else
var key = "ABQIAAAAS-9BXlmhAxzk5tMQ6009tBSuPyGFyYqpbBL0yyePbwJ9Yzj2TRSRG70K1wsky3JHARggI0ccbJ3Y0A";
GScript('http://maps.google.com/maps?file=api&v=2&key=' + key);
GScript('./geohash.js');
GScript('./labeledmarker.js');
var ZOOMLEVELS = { 3: 7, 4 : 10, 5 : 12, 6 : 15, 7 : 17, 8 : 18, 9 : 20 };
function getWindowDimensions () {
var myWidth = 0, myHeight = 0;
if(typeof(window.innerWidth) == 'number')
{
myWidth = window.innerWidth;
myHeight = window.innerHeight;
}
else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
{
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
}
else if(document.body && (document.body.clientWidth || document.body.clientHeight))
{
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return {'width' : myWidth, 'height' : myHeight};
}
function wheelZoom(a) { (a.detail || -a.wheelDelta) < 0 ? map.zoomIn() : map.zoomOut(); }
function sizeMap() {
var dims = getWindowDimensions();
var mapdiv = document.getElementById("map");
mapdiv.style.height = dims.height-120;
mapdiv.style.width = dims.width;
var headerdiv = document.getElementById("header");
headerdiv.style.width = dims.width-30;
var creditsdiv = document.getElementById("credits");
creditsdiv.style.left = dims.width-180;
map = new GMap2(mapdiv);
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 () {
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[1], this.box.longitude[0]);
this.corners.topright = new GLatLng(this.box.latitude[1], this.box.longitude[1]);
this.corners.bottomright = new GLatLng(this.box.latitude[0], this.box.longitude[1]);
this.corners.bottomleft = new GLatLng(this.box.latitude[0], this.box.longitude[0]);
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) {
if (gLatLng==null) {
setText('boxList', 'Location not found!');
setText('searchInfo', '');
return false;
}
var geohash = encodeGeoHash(gLatLng.lat(), gLatLng.lng());
document.getElementById("geoHash").value = geohash;
innerPlotGeohash(geohash, gLatLng.lat(), gLatLng.lng());
}
function plotByHash () {
var geohash = document.getElementById("geoHash").value;
var latlng = decodeGeoHash(geohash);
innerPlotGeohash(geohash, latlng["latitude"][2], latlng["longitude"][2]);
}
function mapTiles() {
var tiles = document.getElementById("tiles").value.split(',');
var geoHashBox = new GeoHashBox(tiles[0].replace(/^\s+|\s+$/g,''));
geoHashBox.centerMap();
// skip first one..
for (var i = 1; i < tiles.length; i++) {
new GeoHashBox(tiles[i].replace(/^\s+|\s+$/g,''));
}
}
function innerPlotGeohash(geohash, latitude, longitude) {
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+"'");
}
boxes.push("'"+geoHashBox.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 xtile = xdistance / 3;
var ytile = ydistance / 3;
var searcharea = parseInt((xdistance/1000) * (ydistance/1000)*100)/100;
if (xdistance>2000) {
xdistance = parseInt(xdistance/10)/100;
ydistance = parseInt(ydistance/10)/100;
xtile = parseInt(xtile/10)/100;
ytile = parseInt(ytile/10)/100;
units = "km";
} else {
xdistance = parseInt(xdistance+0.5);
ydistance = parseInt(ydistance+0.5);
xtile = parseInt(xtile+0.5);
ytile = parseInt(ytile+0.5);
units = "m";
}
var lat = parseInt(latitude*100000)/100000;
var lng = parseInt(longitude*100000)/100000;
searchInfo.innerHTML = lat + ", " + lng + " [w:" + xdistance + units + ", h:" + ydistance + units + "] (" + searcharea + "km2)";
searchInfo.innerHTML = searchInfo.innerHTML + " tile size: w:" + xtile + units + ", h:" + ytile + units;
// var myIcon = new GIcon({image : './anchor.png', shadow : './shadow.png'});
var myMarker = new LabeledMarker(new GLatLng(latitude, longitude), this.options );
map.addOverlay(myMarker);
}
function setText(s,t) {
sp = document.getElementById(s);
sp.innerHTML = t;
}
function cleanUp() {
map.clearOverlays();
setText('boxList','');
setText('searchInfo','');
}
window.onload = function () {
if (GBrowserIsCompatible()) {
window.onresize = sizeMap;
sizeMap();
GEvent.addDomListener(document.getElementById('map'), "DOMMouseScroll", wheelZoom);
GEvent.addDomListener(document.getElementById('map'), "mousewheel", wheelZoom);
} else {
alert("Sorry, your browser is lame!");
}
};