annotate map.js @ 308:c0bac5a78724 gmap2

Change setTimeout() calls to use function format.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 11 Sep 2017 14:36:16 +0300
parents 9ca251cc5d7c
children a00a1507f4c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
5 var pmapTileSize = 256;
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
6 var pmapTileOrig = pmapTileSize / 2.0;
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
7 var pmapTilePxPerDeg = pmapTileSize / 360.0;
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
8 var pmapTilePxPerRad = pmapTileSize / (2.0 * Math.PI);
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
9
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
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
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
16 function pmapMinMax(vval, vmin, vmax)
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
17 {
171
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
18 return Math.min(Math.max(vval, vmin), vmax);
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
19 }
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
20
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
21
172
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
22 function pmapLatLngToMapCoords(latLng, zoom)
171
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
23 {
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
24 msiny = pmapMinMax(Math.sin((latLng.lat() * Math.PI) / 180.0), -0.9999, 0.9999);
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
25
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
26 px = latLng.lng() * pmapTilePxPerDeg;
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
27 py = 0.5 * Math.log((1 + msiny) / (1 - msiny)) * - pmapTilePxPerRad;
171
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
28
172
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
29 pz = 1 << zoom;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
30 return new google.maps.Point(px * pz, py * pz);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
31 }
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
32
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
33
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
34 function pmapMapCoordsToLatLng(point, zoom)
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
35 {
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
36 pz = 1 << zoom;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
37 px = point.x / pz;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
38 py = point.y / pz;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
39
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
40 lng = (px - pmapTileOrig) / pmapTilePxPerDeg;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
41 latRadians = (py - pmapTileOrig) / - pmapTilePxPerRad;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
42
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
43 //lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
44 //lat = (2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2) / (Math.PI / 180);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
45
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
46 lat = (360 * Math.atan(Math.exp(latRadians))) / Math.PI - 90;
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
47
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
48 return new google.maps.LatLng(lat, lng);
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
49 }
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
50
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
51
166
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
52 function CanvasProjectionOverlay() {}
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
53 CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
54 CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
55 CanvasProjectionOverlay.prototype.onAdd = function(){};
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
56 CanvasProjectionOverlay.prototype.draw = function(){};
21cf84dcae73 Move code.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
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
254343316a90 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 250
diff changeset
60 function pmapInitializeMap(args)
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
61 {
171
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
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
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
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
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
75 });
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
76
171
b874aae3e75f Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
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
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
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
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
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
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
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
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
97
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
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;
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
103
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
104 // If args have x, y and zoom set, center to the coordinates
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
105 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
106 {
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
107 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
108 posSet = true;
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
109
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
110 google.maps.event.addDomListener(pmap, "foobar", function()
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
111 {
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
112 // First, set desired zoom level
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
113 pmap.setZoom(args.zoom);
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
114
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
115 // Convert and pan to given coordinates
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
116 pmap.panTo(pmapMapCoordsToLatLng(new google.maps.Point(args.x, args.y), 6));
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
117
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
118 // Then check if any marker exists at these coordinates
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
119 // and if one does, activate it.
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
120 var tgtMarker = pmapGetMarkerIndexByCoords(args.x, args.y);
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
121 if (tgtMarker)
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
122 pmapMyClick(tgtMarker);
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
123 });
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
124 }
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
125
270
561640ed2204 Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
126 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
127 {
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
128 // Create player position marker
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
129 pmapPlrMarker = new google.maps.Marker(
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
130 {
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
131 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
132 draggable: false,
274
43e8493891ed Player icon.
Matti Hamalainen <ccr@tnsp.org>
parents: 273
diff changeset
133 icon: "http://www.bat.org/pic/i?s="+ args.name +"&l=0",
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
134 map: pmap
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
135 });
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
136
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
137 if (!posSet)
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
138 pmap.setZoom(9);
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
139
270
561640ed2204 Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
140 pmapPlrToken = args.token;
561640ed2204 Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
141 pmapPlrName = args.name;
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
142 pmapUpdateDelay = 1000;
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
143 pmapScheduleNextUpdate();
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
144 }
224
85ee47035acf Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
145 }
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
146
224
85ee47035acf Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
147 pmapInitializeNav();
161
074b6936ec4f "Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents: 157
diff changeset
148 pmapInitializeIcons();
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
149 pmapInitializeMarkers();
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
150 }
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
151
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
152
151
13e75e9e732a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 143
diff changeset
153 //
152
095754c6b332 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 151
diff changeset
154 // Listener for updating coordinates display
151
13e75e9e732a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 143
diff changeset
155 //
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
156 function pmapCoordinateListener(point)
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
157 {
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
158 // Convert the coordinates by using "zoom" level 6+1=7
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
159 // in order to get 2x resolution for them, then
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
160 // divide the coords by 2 and use rounding so that we
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
161 // hit the pointed square more accurately.
172
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
162 var p = pmapLatLngToMapCoords(point.latLng, 7);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
163 var tx = Math.round((p.x + 1) / 2.0);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
164 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
165
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
166 // Check if we are on any continent
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
167 var cx = -1, cy = -1;
161
074b6936ec4f "Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents: 157
diff changeset
168 var cont = "Deep Sea";
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
169
157
fadfd0e0a312 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
170 for (i = 0; i < pmapContinents.length; i++)
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
171 {
157
fadfd0e0a312 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
172 var c = pmapContinents[i];
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
173 if (c != null && tx > c[1] && tx < c[3] && ty > c[2] && ty < c[4])
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
174 {
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
175 cont = c[0];
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
176 cx = tx - c[1];
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
177 cy = ty - c[2];
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
178 break;
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
179 }
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
180 }
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
181
301
95b818a7550b Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 292
diff changeset
182 // Adjust by world origin
230
c5a06121ed95 Adjust coordinates.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
183 tx += pmapWorld.ox - 1;
c5a06121ed95 Adjust coordinates.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
184 ty += pmapWorld.oy - 1;
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
185
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
186 var str = "Cursor: "+
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
187 "Global X: <span class=\"coord\">"+ tx +"</span>, Y: <span class=\"coord\">"+ ty +"</span>, "+
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
188 "Continent: <span class=\"continent\">"+ cont +"</span> ";
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
189
303
9ca251cc5d7c Coordinates local to a continent start always at origo of 1,1.
Matti Hamalainen <ccr@tnsp.org>
parents: 301
diff changeset
190 if (cx > 0 && cy > 0)
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
191 str += "[ Local X: <span class=\"coord\">"+ cx +"</span>, Y: <span class=\"coord\">"+ cy +"</span> ]";
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
192
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
193 document.getElementById("footercontent").innerHTML = str;
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
194 }
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
195
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
196
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
197 function pmapMakeLink()
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
198 {
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
199 if (pmap)
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
200 {
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
201 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
202 var tx = Math.round(p.x) + pmapWorld.ox - 1;
228
2be6060df6cc Use global offsets.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
203 var ty = Math.round(p.y) + pmapWorld.oy;
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
204
250
ee5492ccba8e Add configurable base URL.
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
205 str = pmapBaseURL +"?x="+ tx +"&y="+ ty +"&zoom="+ pmap.getZoom();
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
206
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
207 window.prompt("Copy to clipboard: Ctrl+C, Enter", str);
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
208 }
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
209 }
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
210
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
211
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
212 function pmapScheduleNextUpdate()
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
213 {
308
c0bac5a78724 Change setTimeout() calls to use function format.
Matti Hamalainen <ccr@tnsp.org>
parents: 303
diff changeset
214 setTimeout(pmapUpdatePlayerPosition, pmapUpdateDelay);
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
215 }
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
216
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
217
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
218 function pmapIncreaseUpdateDelay(v)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
219 {
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
220 pmapUpdateDelay += v;
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
221 if (pmapUpdateDelay > 5000)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
222 pmapUpdateDelay = 5000;
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
223 }
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
224
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
225
292
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
226 function pmapError(msg)
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
227 {
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
228 alert(msg);
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
229 }
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
230
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
231
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
232 function pmapUpdatePlayerPosition()
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
233 {
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
234 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
235 function(data, responseCode)
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
236 {
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
237 // Check response for type
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
238 pmapUpdateFails = 0;
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
239 if (typeof(data) == "string")
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
240 {
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
241 // Check response
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
242 if (dmatches = data.match(/^Error: (.*)$/))
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
243 {
292
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
244 pmapError("An error occured: "+ dmatches[1]);
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
245 return false;
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
246 }
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
247 else
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
248 if (dmatches = data.match(/{[A-Za-z0-9\'\":, ]+}/))
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
249 {
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
250 var pos = JSON.parse(dmatches[0]);
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
251 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
252 {
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
253 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
254 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
255 pmapPlrPrevPos = pos;
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
256 pmapUpdateDelay = 500;
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
257 }
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
258 else
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
259 // Position not changed, increase update delay
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
260 pmapIncreaseUpdateDelay(500);
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
261 }
268
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
262 else
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
263 // Not a position update, increase update delay
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
264 pmapIncreaseUpdateDelay(1000);
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
265 }
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
266 else
268
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
267 pmapUpdateDelay = 10000;
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
268
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
269 pmapScheduleNextUpdate();
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
270 },
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
271 function()
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
272 {
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
273 pmapUpdateDelay = 10000;
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
274 if (pmapUpdateFails++ > 5)
292
64f67ed89172 Use a wrapper function for errors.
Matti Hamalainen <ccr@tnsp.org>
parents: 274
diff changeset
275 pmapError("An error occured: Server failed to respond.");
273
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
276 else
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
277 pmapScheduleNextUpdate();
f8aa8534b951 More work .. fixed error handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents: 272
diff changeset
278 });
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
279 }