annotate map.js @ 272:a66d849006f2 gmap2

More work on player position updates.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 14 Apr 2014 02:36:45 +0300
parents 561640ed2204
children f8aa8534b951
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;
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
13 var pmapUpdateTime, 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;
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;
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
133 pmapUpdateTime = Date.now();
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
134 pmapUpdateDelay = 0;
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
135 pmapUpdatePID = setInterval("pmapUpdatePlayerPosition()", 500);
265
e908103cdcd8 Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents: 263
diff changeset
136 }
224
85ee47035acf Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
137 }
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
138
224
85ee47035acf Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents: 200
diff changeset
139 pmapInitializeNav();
161
074b6936ec4f "Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents: 157
diff changeset
140 pmapInitializeIcons();
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
141 pmapInitializeMarkers();
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
142 }
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
143
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
144
151
13e75e9e732a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 143
diff changeset
145 //
152
095754c6b332 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 151
diff changeset
146 // Listener for updating coordinates display
151
13e75e9e732a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 143
diff changeset
147 //
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
148 function pmapCoordinateListener(point)
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
149 {
228
2be6060df6cc Use global offsets.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
150 // Round the coordinates so that we hit the pointed square more accurately
172
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
151 var p = pmapLatLngToMapCoords(point.latLng, 7);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
152 var tx = Math.round((p.x + 1) / 2.0);
c62f0cf2bf16 It almost works \:D\
Matti Hamalainen <ccr@tnsp.org>
parents: 171
diff changeset
153 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
154
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
155 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
156 var cont = "Deep Sea";
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
157
157
fadfd0e0a312 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
158 for (i = 0; i < pmapContinents.length; i++)
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
159 {
157
fadfd0e0a312 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
160 var c = pmapContinents[i];
93
2324a519ebec Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
161 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
162 {
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
163 cont = c[0];
170
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
164 cx = tx - c[1];
fb98e644b80c Fix coordinates display.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
165 cy = ty - c[2];
91
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
166 break;
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 }
e4a37528f1a7 Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents: 76
diff changeset
169
230
c5a06121ed95 Adjust coordinates.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
170 tx += pmapWorld.ox - 1;
c5a06121ed95 Adjust coordinates.
Matti Hamalainen <ccr@tnsp.org>
parents: 228
diff changeset
171 ty += pmapWorld.oy - 1;
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
172
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
173 var str = "Cursor: "+
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
174 "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
175 "Continent: <span class=\"continent\">"+ cont +"</span> ";
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
176
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
177 if (cx >= 0 && cy >= 0)
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
178 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
179
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
180 document.getElementById("footercontent").innerHTML = str;
153
a7f43ddd1c5e Functionalize.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
181 }
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
182
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
183
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
184 function pmapMakeLink()
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 if (pmap)
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
187 {
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
188 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
189 var tx = Math.round(p.x) + pmapWorld.ox - 1;
228
2be6060df6cc Use global offsets.
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
190 var ty = Math.round(p.y) + pmapWorld.oy;
200
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
191
250
ee5492ccba8e Add configurable base URL.
Matti Hamalainen <ccr@tnsp.org>
parents: 235
diff changeset
192 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
193
f5aa704b1ddf Add "Make a link" feature.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
194 window.prompt("Copy to clipboard: Ctrl+C, Enter", str);
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 }
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
197
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
198
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
199 function pmapStopPlayerUpdate(msg)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
200 {
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
201 clearInterval(pmapUpdatePID);
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
202 alert("An error occured: "+ msg);
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
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 function pmapIncreaseUpdateDelay(v)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
207 {
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
208 pmapUpdateDelay += v;
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
209 if (pmapUpdateDelay > 5000)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
210 pmapUpdateDelay = 5000;
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
211 }
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
212
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
213
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
214 function pmapUpdatePlayerPosition()
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
215 {
270
561640ed2204 Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
216 if (Date.now() - pmapUpdateTime >= pmapUpdateDelay)
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
217 {
268
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
218 pmapUpdateTime = Date.now();
270
561640ed2204 Improve player position handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 269
diff changeset
219 XDownloadUrl("http://tnsp.org/gmapng/playerpos.php?name="+pmapPlrName+"&token="+pmapPlrToken,
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
220 function(data, responseCode)
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
221 {
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
222 // Check response for type
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
223 pmapUpdateFails = 0;
269
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
224 if (typeof(data) == "string")
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
225 {
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
226 // Check response
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
227 if (dmatches = data.match(/^Error: (.*)$/))
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
228 {
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
229 pmapStopPlayerUpdate(dmatches[1]);
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
230 return;
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
231 }
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
232 else
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
233 if (dmatches = data.match(/{[A-Za-z0-9\'\":, ]+}/))
269
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
234 {
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
235 var pos = JSON.parse(dmatches[0]);
269
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
236 if (typeof(pos) == "object" && (pos.x != pmapPlrPrevPos.x || pos.y != pmapPlrPrevPos.y))
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
237 {
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
238 pmapPlrMarker.setPosition(pmapMapCoordsToLatLng(new google.maps.Point(pos.x * 2 + 1, pos.y * 2 + 1), 7));
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
239 pmap.panTo(pmapMapCoordsToLatLng(new google.maps.Point(pos.x, pos.y), 6));
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
240 pmapPlrPrevPos = pos;
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
241 pmapUpdateDelay = 500;
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
242 }
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
243 else
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
244 // Position not changed, increase update delay
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
245 pmapIncreaseUpdateDelay(500);
269
ce747cd9ec59 Add error situation handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 268
diff changeset
246 }
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
247 else
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
248 // Not a position update, increase update delay
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
249 pmapIncreaseUpdateDelay(1000);
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
250 }
268
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
251 else
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
252 pmapUpdateDelay = 10000;
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
253 },
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
254 function()
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
255 {
98115d71bb64 Some adjustments to realtime player position update code.
Matti Hamalainen <ccr@tnsp.org>
parents: 266
diff changeset
256 pmapUpdateDelay = 10000;
272
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
257 if (pmapUpdateFails++ > 5)
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
258 {
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
259 pmapStopPlayerUpdate("Server failed to respond");
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
260 return;
a66d849006f2 More work on player position updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 270
diff changeset
261 }
266
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
262 });
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
263 }
b50a7ab76548 Implement client-side code for tracking player position.
Matti Hamalainen <ccr@tnsp.org>
parents: 265
diff changeset
264 }