Mercurial > hg > batmud > gmap2
annotate map.js @ 273:f8aa8534b951 gmap2
More work .. fixed error handling somewhat.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 14 Apr 2014 03:01:00 +0300 |
parents | a66d849006f2 |
children | 43e8493891ed |
rev | line source |
---|---|
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
1 // $Id: map.js 2404 2009-12-24 20:01:32Z jeskko $ |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
2 |
270
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
3 var pmap, pmapBaseURL, pmapCanvas; |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
4 |
170 | 5 var pmapTileSize = 256; |
6 var pmapTileOrig = pmapTileSize / 2.0; | |
7 var pmapTilePxPerDeg = pmapTileSize / 360.0; | |
8 var pmapTilePxPerRad = pmapTileSize / (2.0 * Math.PI); | |
9 | |
10 | |
270
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
11 var pmapPlrPrevPos = {"x": 8192, "y": 8192}; |
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
12 var pmapPlrMarker, pmapPlrToken, pmapPlrName; |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
13 var pmapUpdateDelay, pmapUpdateFails = 0; |
270
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
14 |
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
15 |
171 | 16 function pmapMinMax(vval, vmin, vmax) |
170 | 17 { |
171 | 18 return Math.min(Math.max(vval, vmin), vmax); |
19 } | |
20 | |
21 | |
172 | 22 function pmapLatLngToMapCoords(latLng, zoom) |
171 | 23 { |
24 msiny = pmapMinMax(Math.sin((latLng.lat() * Math.PI) / 180.0), -0.9999, 0.9999); | |
170 | 25 |
26 px = latLng.lng() * pmapTilePxPerDeg; | |
27 py = 0.5 * Math.log((1 + msiny) / (1 - msiny)) * - pmapTilePxPerRad; | |
171 | 28 |
172 | 29 pz = 1 << zoom; |
30 return new google.maps.Point(px * pz, py * pz); | |
31 } | |
32 | |
33 | |
34 function pmapMapCoordsToLatLng(point, zoom) | |
35 { | |
36 pz = 1 << zoom; | |
37 px = point.x / pz; | |
38 py = point.y / pz; | |
39 | |
40 lng = (px - pmapTileOrig) / pmapTilePxPerDeg; | |
41 latRadians = (py - pmapTileOrig) / - pmapTilePxPerRad; | |
42 | |
43 //lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2); | |
44 //lat = (2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2) / (Math.PI / 180); | |
45 | |
46 lat = (360 * Math.atan(Math.exp(latRadians))) / Math.PI - 90; | |
47 | |
48 return new google.maps.LatLng(lat, lng); | |
170 | 49 } |
50 | |
51 | |
166 | 52 function CanvasProjectionOverlay() {} |
53 CanvasProjectionOverlay.prototype = new google.maps.OverlayView(); | |
54 CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay; | |
55 CanvasProjectionOverlay.prototype.onAdd = function(){}; | |
56 CanvasProjectionOverlay.prototype.draw = function(){}; | |
57 CanvasProjectionOverlay.prototype.onRemove = function(){}; | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
58 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
59 |
262 | 60 function pmapInitializeMap(args) |
153 | 61 { |
171 | 62 // Initialize custom imagemap with our tiles |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
63 var pmapBatMap = new google.maps.ImageMapType( |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
64 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
65 getTileUrl: function(coord, zoom) |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
66 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
67 return "tiles/"+ zoom +"/"+ coord.y +"/"+ coord.x +".png"; |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
68 }, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
69 |
171 | 70 tileSize: new google.maps.Size(pmapTileSize, pmapTileSize), |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
71 maxZoom: 10, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
72 minZoom: 3, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
73 radius: 1738000, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
74 name: 'BatMap' |
153 | 75 }); |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
76 |
171 | 77 // Create map controller object |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
78 pmap = new google.maps.Map(document.getElementById("map"), |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
79 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
80 zoom: 3, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
81 center: new google.maps.LatLng(-15, 10), |
153 | 82 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
83 streetViewControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
84 draggableCursor: 'crosshair', |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
85 draggingCursor: 'pointer', |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
86 mapTypeControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
87 scaleControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
88 rotateControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
89 overviewMapControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
90 }); |
153 | 91 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
92 pmap.mapTypes.set('BatMap', pmapBatMap); |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
93 pmap.setMapTypeId('BatMap'); |
153 | 94 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
95 pmapCanvas = new CanvasProjectionOverlay(); |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
96 pmapCanvas.setMap(pmap); |
153 | 97 |
170 | 98 google.maps.event.addListener(pmap, "mousemove", pmapCoordinateListener); |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
99 |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
100 if (typeof(args) == "object") |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
101 { |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
102 var posSet = false; |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
103 if ("zoom" in args && "x" in args && "y" in args) |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
104 { |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
105 pmapPlrPrevPos = {"x": args.x, "y": args.y}; |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
106 posSet = true; |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
107 |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
108 google.maps.event.addDomListener(pmap, "foobar", function() |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
109 { |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
110 pmap.setZoom(args.zoom); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
111 pmap.panTo(pmapMapCoordsToLatLng(new google.maps.Point(args.x, args.y), 6)); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
112 var tgtMarker = pmapGetMarkerIndexByCoords(args.x, args.y); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
113 if (tgtMarker) |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
114 pmapMyClick(tgtMarker); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
115 }); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
116 } |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
117 |
270
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
118 if ("token" in args && args.token != "" && "name" in args && args.name != "") |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
119 { |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
120 // Create player position marker |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
121 pmapPlrMarker = new google.maps.Marker( |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
122 { |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
123 position: pmapMapCoordsToLatLng(new google.maps.Point(0, 0)), |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
124 draggable: false, |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
125 map: pmap |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
126 }); |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
127 |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
128 if (!posSet) |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
129 pmap.setZoom(9); |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
130 |
270
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
131 pmapPlrToken = args.token; |
561640ed2204
Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
132 pmapPlrName = args.name; |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
133 pmapUpdateDelay = 1000; |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
134 pmapScheduleNextUpdate(); |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
135 } |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
136 } |
153 | 137 |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
138 pmapInitializeNav(); |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
139 pmapInitializeIcons(); |
153 | 140 pmapInitializeMarkers(); |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
141 } |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
142 |
93 | 143 |
151 | 144 // |
152 | 145 // Listener for updating coordinates display |
151 | 146 // |
153 | 147 function pmapCoordinateListener(point) |
93 | 148 { |
228 | 149 // Round the coordinates so that we hit the pointed square more accurately |
172 | 150 var p = pmapLatLngToMapCoords(point.latLng, 7); |
151 var tx = Math.round((p.x + 1) / 2.0); | |
152 var ty = Math.round((p.y + 1) / 2.0); | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
153 |
200 | 154 var cx, cy; |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
155 var cont = "Deep Sea"; |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
156 |
157 | 157 for (i = 0; i < pmapContinents.length; i++) |
93 | 158 { |
157 | 159 var c = pmapContinents[i]; |
93 | 160 if (c != null && tx > c[1] && tx < c[3] && ty > c[2] && ty < c[4]) |
161 { | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
162 cont = c[0]; |
170 | 163 cx = tx - c[1]; |
164 cy = ty - c[2]; | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
165 break; |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
166 } |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
167 } |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
168 |
230 | 169 tx += pmapWorld.ox - 1; |
170 ty += pmapWorld.oy - 1; | |
200 | 171 |
172 var str = "Cursor: "+ | |
173 "Global X: <span class=\"coord\">"+ tx +"</span>, Y: <span class=\"coord\">"+ ty +"</span>, "+ | |
174 "Continent: <span class=\"continent\">"+ cont +"</span> "; | |
175 | |
176 if (cx >= 0 && cy >= 0) | |
177 str += "[ Local X: <span class=\"coord\">"+ cx +"</span>, Y: <span class=\"coord\">"+ cy +"</span> ]"; | |
178 | |
179 document.getElementById("footercontent").innerHTML = str; | |
153 | 180 } |
200 | 181 |
182 | |
183 function pmapMakeLink() | |
184 { | |
185 if (pmap) | |
186 { | |
187 var p = pmapLatLngToMapCoords(pmap.getCenter(), 6); | |
235
0e6fa3f5684d
Adjust generated link coordinates so that they match what we desire.
Matti Hamalainen <ccr@tnsp.org>
parents:
230
diff
changeset
|
188 var tx = Math.round(p.x) + pmapWorld.ox - 1; |
228 | 189 var ty = Math.round(p.y) + pmapWorld.oy; |
200 | 190 |
250 | 191 str = pmapBaseURL +"?x="+ tx +"&y="+ ty +"&zoom="+ pmap.getZoom(); |
200 | 192 |
193 window.prompt("Copy to clipboard: Ctrl+C, Enter", str); | |
194 } | |
195 } | |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
196 |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
197 |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
198 function pmapScheduleNextUpdate() |
272
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
199 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
200 setTimeout("pmapUpdatePlayerPosition()", pmapUpdateDelay); |
272
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
201 } |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
202 |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
203 |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
204 function pmapIncreaseUpdateDelay(v) |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
205 { |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
206 pmapUpdateDelay += v; |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
207 if (pmapUpdateDelay > 5000) |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
208 pmapUpdateDelay = 5000; |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
209 } |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
210 |
a66d849006f2
More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
211 |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
212 function pmapUpdatePlayerPosition() |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
213 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
214 XDownloadUrl("http://tnsp.org/gmapng/playerpos.php?name="+pmapPlrName+"&token="+pmapPlrToken, |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
215 function(data, responseCode) |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
216 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
217 // Check response for type |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
218 pmapUpdateFails = 0; |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
219 if (typeof(data) == "string") |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
220 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
221 // Check response |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
222 if (dmatches = data.match(/^Error: (.*)$/)) |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
223 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
224 alert("An error occured: "+ dmatches[1]); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
225 return false; |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
226 } |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
227 else |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
228 if (dmatches = data.match(/{[A-Za-z0-9\'\":, ]+}/)) |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
229 { |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
230 var pos = JSON.parse(dmatches[0]); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
231 if (typeof(pos) == "object" && (pos.x != pmapPlrPrevPos.x || pos.y != pmapPlrPrevPos.y)) |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
232 { |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
233 pmapPlrMarker.setPosition(pmapMapCoordsToLatLng(new google.maps.Point(pos.x * 2 + 1, pos.y * 2 + 1), 7)); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
234 pmap.panTo(pmapMapCoordsToLatLng(new google.maps.Point(pos.x, pos.y), 6)); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
235 pmapPlrPrevPos = pos; |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
236 pmapUpdateDelay = 500; |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
237 } |
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
238 else |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
239 // Position not changed, increase update delay |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
240 pmapIncreaseUpdateDelay(500); |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
241 } |
268
98115d71bb64
Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents:
266
diff
changeset
|
242 else |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
243 // Not a position update, increase update delay |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
244 pmapIncreaseUpdateDelay(1000); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
245 } |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
246 else |
268
98115d71bb64
Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents:
266
diff
changeset
|
247 pmapUpdateDelay = 10000; |
273
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
248 |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
249 pmapScheduleNextUpdate(); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
250 }, |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
251 function() |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
252 { |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
253 pmapUpdateDelay = 10000; |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
254 if (pmapUpdateFails++ > 5) |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
255 alert("An error occured: Server failed to respond."); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
256 else |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
257 pmapScheduleNextUpdate(); |
f8aa8534b951
More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
258 }); |
266
b50a7ab76548
Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
259 } |