Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - The tiles of the map are displaying incorrectly when hosted on a web server

I've made an interactive map as a third party tool for a video game using the leaflet API. All is working fine on my computer, but when I push my project on my host, the map has some tiling errors.

Here is what it looks like on local

Here is what it looks like on my host

So i've stripped down my code to the bare minimum (no more markers, layerGroups...) and the bug persists.

Here is my code:

    <!doctype html>

<html>
<head>

  <title>PROJECT TITLE</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
   integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
   crossorigin=""/>
   <script src="https://unpkg.com/[email protected]/dist/leaflet.js"
   integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
   crossorigin=""></script>

</head>
<body style="margin: 0;">
  <div id="map" style="width: 100vw; height: 100vh; background: #000000;"></div>
  <script>
  var map = L.map('map', {
    center: [0, 0],
    zoom: 2
  });
  L.tileLayer('map/{z}/{x}/{y}.png', {
    attribution: 'SOMETHING SOMETHING',
    minZoom: 2,
    maxZoom: 5,
    tileSize: 256,
    noWrap: true
  }).addTo(map);
  </script>
</body>
</html>

Any help appreciated. Thanks !

question from:https://stackoverflow.com/questions/65895333/the-tiles-of-the-map-are-displaying-incorrectly-when-hosted-on-a-web-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Got rid of the issue by setting bounds on my L.tileLayer.

Here is the new code:

    <!doctype html>

<html>
<head>

  <title>PROJECT TITLE</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
   integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
   crossorigin=""/>
   <script src="https://unpkg.com/[email protected]/dist/leaflet.js"
   integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
   crossorigin=""></script>

</head>
<body style="margin: 0;">
  <div id="map" style="width: 100vw; height: 100vh; background: #000000;"></div>
  <script>
  var bounds = [[-50, 50],[50, -50]];
  var map = L.map('map', {
    center: [0, 0],
    zoom: 2
  });
  L.tileLayer('map/{z}/{x}/{y}.png', {
    bounds: bounds,
    attribution: 'SOMETHING SOMETHING',
    minZoom: 2,
    maxZoom: 5,
    tileSize: 256,
    noWrap: true
  }).addTo(map);
  </script>
</body>
</html>

To find the correct values for the boundaries, I created a rectangle to try coordinates:

  var bounds = [[-50, 50],[50, -50]];
  L.rectangle(bounds, {
  color: "#ff7800",
  weight: 1
}).addTo(map);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...