leaflet-tests.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <meta name="description" content="Leaflet Tests">
  8. <meta name="author" content="Gwyneth Llewelyn">
  9. <title>Leaflet/OpenSimulator Maps Integration</title>
  10. <!-- Google Web Fonts -->
  11. <!-- <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Cantarell|Cardo"> -->
  12. <!-- Bootstrap -->
  13. <!--
  14. <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  15. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  16. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  17. <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  18. -->
  19. <!-- Bootstrap-Dialog -->
  20. <!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.35.4/css/bootstrap-dialog.min.css" integrity="sha256-wstTM1F5dOf7cgnlRHIW3bmoRAAGh6jL7tMIvqTuFZE=" crossorigin="anonymous" /> -->
  21. <!-- Call Leaflet.js to deal with maps -->
  22. <!-- Bumped to Leaflet 1.5.1 (gwyneth 20191116) -->
  23. <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
  24. integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
  25. crossorigin=""/>
  26. <script src="https://unpkg.com/[email protected]/dist/leaflet.js"
  27. integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
  28. crossorigin="">
  29. </script>
  30. <script src="leaflet-rastercoords.js"></script> <!-- direct from GitHub! (20191120) -->
  31. <!-- Our own CSS -->
  32. <!-- <link rel="stylesheet" href="../../lib/gobot.css" /> -->
  33. </head>
  34. <body>
  35. <h1>Leaflet/OpenSimulator Maps Integration</h1>
  36. <hr />
  37. <div id="map"></div>
  38. <script type="text/javascript">
  39. // Solution from opensim-dev
  40. var mapMinZoom = 1;
  41. var mapMaxZoom = 6;
  42. L.Projection.Direct = {
  43. project: function(latlng) {
  44. return new L.Point(latlng.lat * 256, latlng.lng * 256);
  45. },
  46. unproject: function(point) {
  47. return new L.LatLng(point.x / 256, point.y / 256);
  48. }
  49. };
  50. L.CRS.OpenSim = L.extend({}, L.CRS, {
  51. projection: L.Projection.Direct,
  52. transformation: new L.Transformation(1, 0, 1, 0),
  53. scale: function(zoom) {
  54. return 1; // OpenSim zoom
  55. }
  56. });
  57. var map = L.map('map', {
  58. minZoom: mapMinZoom,
  59. maxZoom: mapMaxZoom
  60. });
  61. L.tileLayer('http://opensim.betatechnologies.info:8002/map-{z}-{x}-{y}-objects.jpg', {
  62. minZoom: mapMinZoom,
  63. maxZoom: mapMaxZoom,
  64. continuousWorld: false,
  65. noWrap: true,
  66. // zoomReverse: true,
  67. // zoomOffset: 1,
  68. reuseTiles: true,
  69. tms: true,
  70. tileSize: 256,
  71. crs: L.CRS.OpenSim,
  72. attribution: 'opensim',
  73. id: 'opensim',
  74. }).addTo(map);
  75. // map.panTo([3650, 3650], 1);
  76. // map.setView([3650, 3650], 2);
  77. map.setView(map.project([3650, 3650]), mapMinZoom);
  78. /* Doing it the SL way */
  79. /*
  80. // === Constants ===
  81. const os_base_url = "https://opensim.betatechnologies.info/";
  82. const tile_url = "http://opensim.betatechnologies.info:8002";
  83. // Turn on the map debugger
  84. const map_debug = true;
  85. // The maximum width/height of the grid in regions:
  86. // 2^20 regions on a side = 1,048,576 ("This should be enough for anyone")
  87. // *NOTE: This must be a power of 2 and divisible by 2^(max zoom) = 256
  88. // map_grid_edge_size: 1048576,
  89. const map_grid_edge_size = 5000;
  90. var mapDiv = "map"; // change as needed!
  91. var MIN_ZOOM_LEVEL = 1;
  92. var MAX_ZOOM_LEVEL = 6;
  93. var SLTileLayer = L.TileLayer.extend({
  94. getTileUrl: function (coords) {
  95. var data = {
  96. r: L.Browser.retina ? '@2x' : '',
  97. s: this._getSubdomain(coords),
  98. z: this._getZoomForUrl()
  99. };
  100. var regionsPerTileEdge = Math.pow(2, data['z'] - 1);
  101. data['region_x'] = coords.x * regionsPerTileEdge;
  102. data['region_y'] = (Math.abs(coords.y) - 1) * regionsPerTileEdge;
  103. return L.Util.template(this._url, L.extend(data, this.options));
  104. }
  105. });
  106. var tiles = new SLTileLayer(tile_url + "/map-{z}-{region_x}-{region_y}-objects.jpg", {
  107. crs: L.CRS.Simple,
  108. minZoom: MIN_ZOOM_LEVEL,
  109. maxZoom: MAX_ZOOM_LEVEL,
  110. zoomOffset: 1,
  111. zoomReverse: true,
  112. bounds: [[0, 0], [map_grid_edge_size, map_grid_edge_size]],
  113. attribution: "<a href='" + os_base_url + "'>Beta Technologies OpenSimulator Grid</a>"
  114. });
  115. var map = L.map(mapDiv, {
  116. crs: L.CRS.Simple,
  117. minZoom: MIN_ZOOM_LEVEL,
  118. maxZoom: MAX_ZOOM_LEVEL,
  119. maxBounds: [[0, 0], [map_grid_edge_size, map_grid_edge_size]],
  120. layers: [tiles],
  121. center: [3650, 3650],
  122. zoom: MIN_ZOOM_LEVEL
  123. });
  124. map.on('click', function (event) {
  125. gotoSLURL(event.latlng.lng, event.latlng.lat, map);
  126. });
  127. map.panTo([3650, 3650]);
  128. */
  129. /*
  130. var mapMaxZoom = 2;
  131. var mapMinZoom = 1;
  132. var map = L.map('map', {
  133. maxZoom: mapMaxZoom,
  134. minZoom: mapMinZoom,
  135. crs: L.CRS.Simple
  136. });
  137. map.setView(map.unproject([3650, 3650], mapMinZoom), mapMinZoom);
  138. var mapBounds = new L.LatLngBounds(
  139. map.unproject([0, 2560000], mapMaxZoom),
  140. map.unproject([2560000, 0], mapMaxZoom));
  141. L.tileLayer('http://opensim.betatechnologies.info:8002/map-{z}-{x}-{y}-objects.jpg', {
  142. minZoom: mapMinZoom,
  143. maxZoom: mapMaxZoom,
  144. zoomReverse: true,
  145. zoomOffset: 1,
  146. continuousWorld: true,
  147. reuseTiles: true,
  148. noWrap: true,
  149. attribution: 'Beta Technologies OpenSim Grid',
  150. bounds: mapBounds,
  151. tms: true
  152. }).addTo(map);
  153. // map.panTo([3650, 3650]);
  154. */
  155. /*
  156. // Using rastercoords (20191120)
  157. // See: https://commenthol.github.io/leaflet-rastercoords/
  158. // for use with browserify / webpack
  159. // const mapSize = 2^15 * 256; // arbitrary size
  160. const mapSize = 2^15 * 256; // arbitrary size
  161. var img = [
  162. mapSize, // original width of image (here from `example/karta.jpg`)
  163. mapSize // original height of image
  164. ]
  165. // create the map
  166. var map = L.map('map');
  167. // assign map and image dimensions
  168. var rc = new L.RasterCoords(map, img);
  169. // set max zoom Level (might be `x` if gdal2tiles was called with `-z 0-x` option)
  170. map.setMaxZoom(rc.zoomLevel());
  171. // all coordinates need to be unprojected using the `unproject` method
  172. // set the view in the lower right edge of the image
  173. // map.setView(rc.unproject([img[0], img[1]]), 2);
  174. map.setView(rc.unproject([3650, 3650]), 2);
  175. // set markers on click events in the map
  176. map.on('click', function (event) {
  177. // any position in leaflet needs to be projected to obtain the image coordinates
  178. var coords = rc.project(event.latlng);
  179. var marker = L.marker(rc.unproject(coords))
  180. .addTo(map);
  181. marker.bindPopup('[' + Math.floor(coords.x) + ',' + Math.floor(coords.y) + ']')
  182. .openPopup();
  183. });
  184. L.tileLayer('http://opensim.betatechnologies.info:8002/map-{z}-{x}-{y}-objects.jpg', {
  185. noWrap: true
  186. }).addTo(map);
  187. */
  188. </script>
  189. <hr />
  190. </body>
  191. </html>