OpenLayers 2 is outdated. Go to the latest 3.x version:

Using Custom Tile Sources / Google-like Tile Layer Support

If you have tiles which are set up according to the 'Google' tile schema -- that is, based on x,y,z and starting in the upper left corner of the world -- you can load these tiles with the TMS layer with a slightly modified get_url function. (Note that in the past there was a 'LikeGoogle' layer in SVN -- this is the appropriate replacement for that code/functionality.)

First, define a getURL function that you want to use: it should accept a bounds as an argument, and will look something like this:

    function get_my_url (bounds) {
        var res =;
        var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
        var y = Math.round (( - / (res * this.tileSize.h));
        var z =;

        var path = z + "/" + x + "/" + y + "." + this.type; 
        var url = this.url;
        if (url instanceof Array) {
            url = this.selectUrl(path, url);
        return url + path;

Then, when creating your TMS layer, pass in options like:

  new OpenLayers.Layer.TMS("Name", "", { 'type':'png', 'getURL':get_my_url });

This will cause the getURL function to be overridden by your function, thus requesting your inverted google-like tiles instead of standard TMS tiles.

Note that the map configuration options for a Google-like layer should be:

   maxExtent: new OpenLayers.Bounds(-20037508.3427892,-20037508.3427892,20037508.3427892,20037508.3427892), 
   projection: "EPSG:900913",
   displayProjection: new OpenLayers.Projection("EPSG:4326")

To transform from WGS84 long/lat to mercator-projected meters as in the setCenter example:

  var lonLat = new OpenLayers.LonLat(-100, 40) ;
  map.setCenter(lonLat, 5);

This would center the map on WGS84 coordinates long -100 lat 40 at zoom level 5.

Note: Projection.js is available as "trunk" code until 2.6 becomes stable.

Using Tiles Without a Projection

If you're using OpenLayers to display, say, a document rather than a map and you have a set of square tiles, this tip from crschmidt on #openlayers helped. In this example, you will have 1 tile (a grid of 1 by 1) for the first zoom level, 4 tiles (2 by 2) for the second zoom level, 16 tiles (4 by 4) for the third, and 64 tiles (8 by 8) for the fourth, and 256 tiles (16 by 16) for the fifth. Each tile is 256 pixels by 256 pixels. That means your map, at the highest zoom, is 4096 pixels (256 multiplied by 16).

Use this in the configuration of your map layer for this example:

maxExtent: new OpenLayers.Bounds(0,0,4096,4096),
maxResolution:4096 / 256,
Last modified 8 months ago Last modified on May 20, 2016 1:18:37 AM