Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Last active September 4, 2023 11:52
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save donmccurdy/1d3998a37c73e9435b5503e5f37457be to your computer and use it in GitHub Desktop.
Save donmccurdy/1d3998a37c73e9435b5503e5f37457be to your computer and use it in GitHub Desktop.
Custom tiles in Google Maps
license: apache-2.0

An example showing how to use custom tiles with the Google Maps JS API.

▶️ Live demo

Instructions

  • (1) Sign into the Google Cloud Platform Console, or create a new account.
  • (2) Create a new project.
  • (3) In the console, open: Menu (☰) → API ManagerLibrary. Select the Google Maps JavaScript API and choose Enable.
  • (4) Go to the Credentials page in the sidebar. Select Create credentialsAPI key. Save this key for later. You may, optionally, add restrictions so that this new key may only be used on your domain(s).
  • (5) Add an element to the page, where the map should appear.
<div id="map"></div>
  • (6) Add the Maps API script to your page:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
  • (7) Create a new map, attached to the element.
var mapEl = document.querySelector('#map');
var map = new google.maps.Map(mapEl, {
  center: new google.maps.LatLng(39.8282, -98.5795),
  zoom: 5
});
  • (8) Replace the sample TILE_URL with your own, which should look (roughly) like: https://example.com/sample/{z}_{x}_{y}.jpg.

Create a new tile layer using this URL:

// Replace this with your URL.
var TILE_URL = 'https://example.com/sample/{z}_{x}_{y}.jpg';

// Name the layer anything you like.
var layerID = 'my_custom_layer';

// Create a new ImageMapType layer.
var layer = new google.maps.ImageMapType({
  name: layerID,
  getTileUrl: function(coord, zoom) {
    console.log(coord);
    var url = TILE_URL
      .replace('{x}', coord.x)
      .replace('{y}', coord.y)
      .replace('{z}', zoom);
    return url;
  },
  tileSize: new google.maps.Size(256, 256),
  minZoom: 1,
  maxZoom: 20
});
  • (9) Add the new tile layer to your map:
// Register the new layer, then activate it.
map.mapTypes.set(layerID, layer);
map.setMapTypeId(layerID);

A complete example is shown in index.html.

Further documentation at Google Maps JS API: Image Map Types.

License

Sample code by Google, under the Apache 2.0 License.

Watercolor map tiles by Stamen Design, under CC-BY-3.0.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
/* styles */
html, body {
margin: 0;
padding: 0;
}
#map {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var TILE_URL = 'http://c.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg';
var map;
var mapEl;
var layer;
var layerID = 'my-custom-layer';
window.initMap = function() {
// Select the element with id="map".
mapEl = document.querySelector('#map');
// Create a new map.
map = new google.maps.Map(mapEl, {
center: new google.maps.LatLng(39.8282, -98.5795),
zoom: 4
});
// Create a tile layer, configured to fetch tiles from TILE_URL.
layer = new google.maps.ImageMapType({
name: layerID,
getTileUrl: function(coord, zoom) {
console.log(coord);
var url = TILE_URL
.replace('{x}', coord.x)
.replace('{y}', coord.y)
.replace('{z}', zoom);
return url;
},
tileSize: new google.maps.Size(256, 256),
minZoom: 1,
maxZoom: 20
});
// Apply the new tile layer to the map.
map.mapTypes.set(layerID, layer);
map.setMapTypeId(layerID);
};
</script>
<!-- NOTE: The 'key' parameter should be replaced with your Google Maps API Key. -->
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=AIzaSyDaoLdV31Y5ls8ABBpuAQt9t8RzMDfOMiM"></script>
</body>
</html>
@renilbabu03
Copy link

Hi. I am stuck in this. How to make this url with zoom , x-cord, y-cord. I am confused .

@donmccurdy
Copy link
Author

@renilbabu03 I would recommend asking on Stack Overflow, including the code you've tried so far and what error you're seeing.

@renilbabu03
Copy link

I haven't implemented anything on server to fetch the url for tile. I don't know what to implement on server. I have searched a lot but didn't find. Google doc says to put static files in server. But how to generate the tiles?

@donmccurdy
Copy link
Author

That would depend on what sort of source data you have, and might be a better question for a GIS help site. Perhaps start with one of these?

They aren't about Google Maps API specifically, but the tile URL x/y/z coordinate system is the same.

@renilbabu03
Copy link

Thank you soo much for the help.

@renilbabu03
Copy link

My source file is a static image. What other source data are there.!?

@donmccurdy
Copy link
Author

If you have a single static PNG or JPEG image, and you know where it needs to be shown, then this example is probably what you want:

https://developers.google.com/maps/documentation/javascript/examples/overlay-simple

In that case, you don't need to create tiles with x/y/z coordinates at all.

@renilbabu03
Copy link

renilbabu03 commented Jul 1, 2019

Yes i saw that example but this only works for sqaure or rectangular image. My image is tilted and another image is circular

@donmccurdy
Copy link
Author

I don't have an easy solution for that – one of the GIS or web development help sites on Stack Exchange is probably a better place to ask. You'll need to provide as much detail as possible (ideally example images and at least of a bit of code you've tried) for others to help.

@jrodenhouse
Copy link

If you have a single static PNG or JPEG image, and you know where it needs to be shown, then this example is probably what you want:

https://developers.google.com/maps/documentation/javascript/examples/overlay-simple

In that case, you don't need to create tiles with x/y/z coordinates at all.

Thank you for sharing your work! I had a questions about finding the x/y/z url. I used maptiler > raster tiles for "not a georeferenced image" and uploaded my generated tile files to dropbox. The folders are organized by zoom level (0-6). I am getting stuck at how to find my x/y/z url. Thank you.

@donmccurdy
Copy link
Author

I'm not sure what Maptiler generates, this may be more of a question for that tool. If the first level folder is zoom, then your URLs would be something.com/<something>/{z}/{x}/{y}.png for example, where {z} comes before the other parameters separated by a /.

@lighteagle
Copy link

how to to implement this on geoserver ? because they use -y
https://{host}/geoserver/gwc/service/tms/1.0.0/{layername}@epsg%3A900913@png/{z}/{x}/{-y}.png

@chakravarthi-vl
Copy link

@lighteagle For Geoserver you can use the below URL
https://{host}/geoserver/gwc/service/tms/1.0.0/{layername}@epsg%3A900913@png/{z}/{x}/{y}.png?flipY=true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment