Mercurial > hg > batmud > gmap2
annotate map.js @ 265:e908103cdcd8 gmap2
Change how map initialization arguments are handled.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 13 Apr 2014 20:52:11 +0300 |
parents | 7e57e5577425 |
children | b50a7ab76548 |
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 |
250 | 3 var pmapBaseURL; |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
4 var pmap; |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
5 var pmapCanvas; |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
6 |
170 | 7 var pmapTileSize = 256; |
8 var pmapTileOrig = pmapTileSize / 2.0; | |
9 var pmapTilePxPerDeg = pmapTileSize / 360.0; | |
10 var pmapTilePxPerRad = pmapTileSize / (2.0 * Math.PI); | |
11 | |
12 | |
171 | 13 function pmapMinMax(vval, vmin, vmax) |
170 | 14 { |
171 | 15 return Math.min(Math.max(vval, vmin), vmax); |
16 } | |
17 | |
18 | |
172 | 19 function pmapLatLngToMapCoords(latLng, zoom) |
171 | 20 { |
21 msiny = pmapMinMax(Math.sin((latLng.lat() * Math.PI) / 180.0), -0.9999, 0.9999); | |
170 | 22 |
23 px = latLng.lng() * pmapTilePxPerDeg; | |
24 py = 0.5 * Math.log((1 + msiny) / (1 - msiny)) * - pmapTilePxPerRad; | |
171 | 25 |
172 | 26 pz = 1 << zoom; |
27 return new google.maps.Point(px * pz, py * pz); | |
28 } | |
29 | |
30 | |
31 function pmapMapCoordsToLatLng(point, zoom) | |
32 { | |
33 pz = 1 << zoom; | |
34 px = point.x / pz; | |
35 py = point.y / pz; | |
36 | |
37 lng = (px - pmapTileOrig) / pmapTilePxPerDeg; | |
38 latRadians = (py - pmapTileOrig) / - pmapTilePxPerRad; | |
39 | |
40 //lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2); | |
41 //lat = (2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2) / (Math.PI / 180); | |
42 | |
43 lat = (360 * Math.atan(Math.exp(latRadians))) / Math.PI - 90; | |
44 | |
45 return new google.maps.LatLng(lat, lng); | |
170 | 46 } |
47 | |
48 | |
166 | 49 function CanvasProjectionOverlay() {} |
50 CanvasProjectionOverlay.prototype = new google.maps.OverlayView(); | |
51 CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay; | |
52 CanvasProjectionOverlay.prototype.onAdd = function(){}; | |
53 CanvasProjectionOverlay.prototype.draw = function(){}; | |
54 CanvasProjectionOverlay.prototype.onRemove = function(){}; | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
55 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
56 |
262 | 57 function pmapInitializeMap(args) |
153 | 58 { |
171 | 59 // 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
|
60 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
|
61 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
62 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
|
63 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
64 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
|
65 }, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
66 |
171 | 67 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
|
68 maxZoom: 10, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
69 minZoom: 3, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
70 radius: 1738000, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
71 name: 'BatMap' |
153 | 72 }); |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
73 |
171 | 74 // 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
|
75 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
|
76 { |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
77 zoom: 3, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
78 center: new google.maps.LatLng(-15, 10), |
153 | 79 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
80 streetViewControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
81 draggableCursor: 'crosshair', |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
82 draggingCursor: 'pointer', |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
83 mapTypeControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
84 scaleControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
85 rotateControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
86 overviewMapControl: false, |
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
87 }); |
153 | 88 |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
89 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
|
90 pmap.setMapTypeId('BatMap'); |
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 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
|
93 pmapCanvas.setMap(pmap); |
153 | 94 |
170 | 95 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
|
96 |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
97 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
|
98 { |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
99 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
|
100 { |
265
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
101 google.maps.event.addDomListener(pmap, "foobar", function() |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
102 { |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
103 pmap.setZoom(args.zoom); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
104 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
|
105 var tgtMarker = pmapGetMarkerIndexByCoords(args.x, args.y); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
106 if (tgtMarker) |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
107 pmapMyClick(tgtMarker); |
e908103cdcd8
Change how map initialization arguments are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
108 }); |
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 } |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
111 } |
153 | 112 |
224
85ee47035acf
Open marker info window if specified coordinates hit it exactly.
Matti Hamalainen <ccr@tnsp.org>
parents:
200
diff
changeset
|
113 pmapInitializeNav(); |
161
074b6936ec4f
"Kind of works", but not really. Almost nothing works. And even that
Matti Hamalainen <ccr@tnsp.org>
parents:
157
diff
changeset
|
114 pmapInitializeIcons(); |
153 | 115 pmapInitializeMarkers(); |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
116 } |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
117 |
93 | 118 |
151 | 119 // |
152 | 120 // Listener for updating coordinates display |
151 | 121 // |
153 | 122 function pmapCoordinateListener(point) |
93 | 123 { |
228 | 124 // Round the coordinates so that we hit the pointed square more accurately |
172 | 125 var p = pmapLatLngToMapCoords(point.latLng, 7); |
126 var tx = Math.round((p.x + 1) / 2.0); | |
127 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
|
128 |
200 | 129 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
|
130 var cont = "Deep Sea"; |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
131 |
157 | 132 for (i = 0; i < pmapContinents.length; i++) |
93 | 133 { |
157 | 134 var c = pmapContinents[i]; |
93 | 135 if (c != null && tx > c[1] && tx < c[3] && ty > c[2] && ty < c[4]) |
136 { | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
137 cont = c[0]; |
170 | 138 cx = tx - c[1]; |
139 cy = ty - c[2]; | |
91
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
140 break; |
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 } |
e4a37528f1a7
Convert line ends via dos2unix.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
143 |
230 | 144 tx += pmapWorld.ox - 1; |
145 ty += pmapWorld.oy - 1; | |
200 | 146 |
147 var str = "Cursor: "+ | |
148 "Global X: <span class=\"coord\">"+ tx +"</span>, Y: <span class=\"coord\">"+ ty +"</span>, "+ | |
149 "Continent: <span class=\"continent\">"+ cont +"</span> "; | |
150 | |
151 if (cx >= 0 && cy >= 0) | |
152 str += "[ Local X: <span class=\"coord\">"+ cx +"</span>, Y: <span class=\"coord\">"+ cy +"</span> ]"; | |
153 | |
154 document.getElementById("footercontent").innerHTML = str; | |
153 | 155 } |
200 | 156 |
157 | |
158 function pmapMakeLink() | |
159 { | |
160 if (pmap) | |
161 { | |
162 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
|
163 var tx = Math.round(p.x) + pmapWorld.ox - 1; |
228 | 164 var ty = Math.round(p.y) + pmapWorld.oy; |
200 | 165 |
250 | 166 str = pmapBaseURL +"?x="+ tx +"&y="+ ty +"&zoom="+ pmap.getZoom(); |
200 | 167 |
168 window.prompt("Copy to clipboard: Ctrl+C, Enter", str); | |
169 } | |
170 } |