[GIS] OSM set up OpenLayers to connect to local TileServer

localhostopenlayersopenstreetmaptile-server

I just set up local tile server following these instructions
https://switch2osm.org/serving-tiles/building-a-tile-server-from-packages/ and I want to deploy OpenLayers on local too, such that it communicates with the local tile server.

So I got v3.5.0.zip from here: http://openlayers.org/download

I am looking where to modify the code in OpenLayers libraries to make them contact the local tile server. I am testing on example below. Can anyone help me to figure out how to make this set up?

I've tried to modify it by myself in a couple of places, but it didn't work

<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="http://127.0.0.1/osm/v3.5.0/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="http://127.0.0.1/osm/v3.5.0/build/ol.js" type="text/javascript"></script>
    <title>OpenLayers 3 example</title>
  </head>
  <body>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.MapQuest({layer: 'sat'})
          })
        ],
        view: new ol.View({
          center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
          zoom: 4
        })
      });
    </script>
  </body>
</html>

Best Answer

You just need to follow the official sample Localized OpenStreetMap.

You will just have to copy/paste the referenced sample in a new html file.

Then, if your tiles "root" is located at http://localhost/osm/, replace http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png with http://localhost/osm/{z}/{x}/{y}.png

Save and it should work by opening the file in your browser.

Related Question