-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacmap.html
204 lines (197 loc) · 7.84 KB
/
pacmap.html
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
<html>
<head>
<title>PacMap</title>
<style type="text/css">
html, body, #basicMap {
width: 100%;
height: 100%;
margin: 0;
}
</style>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
var marker;var dots;
//var clon = -2.12;
//var clat = 53.47;
var clon = -2.237;
var clat = 53.484;
function init() {
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(new OpenLayers.LonLat(clon,clat) // Center of the map
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
), 16 // Zoom level
);
var lonLat = new OpenLayers.LonLat(-2.12,53.47)
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
marker = new OpenLayers.Marker(lonLat, new OpenLayers.Icon("pac.png"));
var markers = new OpenLayers.Layer.Markers( "Markers" );
dots = new OpenLayers.Layer.Markers( "Dots" );
map.addLayer(markers);
map.addLayer(dots);
markers.addMarker(marker);
redraw();
}
</script>
<script type="text/javascript">
var target_lat = clat;
var target_lon = clon;
var min_lat;
var max_lat;
var min_lon;
var max_lon;
var node;
var intervalID;
var first = true;
var doMove = false;
var lon = lat = nextlon = nextlat = startlon = startlat = 0;
function _callback(d) {
data = d;
for (var ref in data["nodes"]) {
node = ref;
if (data["nodes"][node]["join"].length > 0) {
break;
}
}
for (var ref in data["nodes"]) {
if (data["nodes"][ref]["join"].length > 0) {
var lonLat = new OpenLayers.LonLat(data["nodes"][ref]["lon"], data["nodes"][ref]["lat"])
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var dot = new OpenLayers.Marker(lonLat, new OpenLayers.Icon("dot.png"));
dots.addMarker(dot);
node = ref;
data["nodes"][ref]["dot"] = dot;
}
}
if (first) intervalID = setInterval(move,100);
}
function move() {
h = Math.sqrt((nextlon-startlon)*(nextlon-startlon)+(nextlat-startlat)*(nextlat-startlat));
lon += (nextlon-startlon)/h * 0.0001;
lat += (nextlat-startlat)/h * 0.0001;
if (first || (nextlon-startlon) * (nextlon-lon) < 0) {
if (!first) {
data["nodes"][nextnode]["dot"].display(false);
node = nextnode;
}
first = false;
var bestdist;
for (var i = 0; i < data["nodes"][node]["join"].length; i++) {
nextlon = parseFloat(data["nodes"][data["nodes"][node]["join"][i]]["lon"]);
nextlat = parseFloat(data["nodes"][data["nodes"][node]["join"][i]]["lat"]);
var dist = Math.sqrt((target_lat-nextlat)*(target_lat-nextlat) + (target_lon-nextlon)*(target_lon-nextlon));
if (i==0 || dist < bestdist) {
bestdist = dist;
nextnode = data["nodes"][node]["join"][i];
}
}
startlon = parseFloat(data["nodes"][node]["lon"]);
startlat = parseFloat(data["nodes"][node]["lat"]);
nextlon = parseFloat(data["nodes"][nextnode]["lon"]);
nextlat = parseFloat(data["nodes"][nextnode]["lat"]);
lon = startlon; lat = startlat;
}
var lonLat = new OpenLayers.LonLat(lon,lat)
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
marker.moveTo(map.getLayerPxFromViewPortPx(map.getPixelFromLonLat(lonLat)));
}
function redraw() {
var min_lat = clat-0.005;
var max_lat = clat+0.005;
var min_lon = clon-0.005;
var max_lon = clon+0.005;
bbox = min_lon+","+min_lat+","+max_lon+","+max_lat;
var script = document.createElement("script");
script.setAttribute("src", "http://xn--gce.com/pacmap/osm.py/data?bbox=" + bbox);
script.setAttribute("type", "text/javascript");
document.getElementsByTagName("head")[0].appendChild(script);
}
function handleMove(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
px = new OpenLayers.Pixel(parseInt(posx),parseInt(posy));
ll = map.getLonLatFromViewPortPx(px).transform(
map.getProjectionObject(),
new OpenLayers.Projection("EPSG:4326")
);
target_lat = ll.lat;
target_lon = ll.lon;
}
//document.onclick = handleClick;
document.onmousemove = handleMove;
function handleKeyDown(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
switch (evt.keyCode) {
case 37:
//doMove = true;
break;
case 38:
ll = map.getCenter().transform(
map.getProjectionObject(),
new OpenLayers.Projection("EPSG:4326")
);
clat = ll.lat;
clon = ll.lon;
clearInterval(intervalID);
first = true;
redraw();
break;
case 39:
break;
case 40:
ll = map.getCenter().transform(
map.getProjectionObject(),
new OpenLayers.Projection("EPSG:4326")
);
clat = ll.lat;
clon = ll.lon;
redraw();
break;
}
}
}
/*function handleKeyUp(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
switch (evt.keyCode) {
case 37:
case 38:
case 39:
case 40:
doMove = false;
break;
}
}
}*/
document.onkeydown = handleKeyDown;
//document.onkeyup = handleKeyUp;
</script>
</head>
<body onload="init();">
<div id="basicMap"></div>
</body>
</html>