Skip to content

Instantly share code, notes, and snippets.

Created March 25, 2012 15:07
Show Gist options
  • Save crofty/2197042 to your computer and use it in GitHub Desktop.
Save crofty/2197042 to your computer and use it in GitHub Desktop.
Leaflet plugin that enables the use of Google Map tiles -
* L.TileLayer is used for standard xyz-numbered tile layers.
L.Google = L.Class.extend({
includes: L.Mixin.Events,
options: {
minZoom: 0,
maxZoom: 18,
tileSize: 256,
subdomains: 'abc',
errorTileUrl: '',
attribution: '',
opacity: 1,
continuousWorld: false,
noWrap: false,
initialize: function(type, options) {
L.Util.setOptions(this, options);
this._type = google.maps.MapTypeId[type || 'SATELLITE'];
onAdd: function(map, insertAtTheBottom) {
this._map = map;
this._insertAtTheBottom = insertAtTheBottom;
// create a container div for tiles
// set up events
map.on('viewreset', this._resetCallback, this);
this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
map.on('move', this._update, this);
//map.on('moveend', this._update, this);
onRemove: function(map) {
//this._container = null;'viewreset', this._resetCallback, this);'move', this._update, this);
//'moveend', this._update, this);
getAttribution: function() {
return this.options.attribution;
setOpacity: function(opacity) {
this.options.opacity = opacity;
if (opacity < 1) {
L.DomUtil.setOpacity(this._container, opacity);
_initContainer: function() {
var tilePane = this._map._container
first = tilePane.firstChild;
if (!this._container) {
this._container = L.DomUtil.create('div', 'leaflet-google-layer leaflet-top leaflet-left'); = "_GMapContainer";
if (true) {
tilePane.insertBefore(this._container, first);
var size = this._map.getSize(); = size.x + 'px'; = size.y + 'px';
_initMapObject: function() {
this._google_center = new google.maps.LatLng(0, 0);
var map = new google.maps.Map(this._container, {
center: this._google_center,
zoom: 0,
mapTypeId: this._type,
disableDefaultUI: true,
keyboardShortcuts: false,
draggable: false,
disableDoubleClickZoom: true,
scrollwheel: false,
streetViewControl: false
var _this = this;
this._reposition = google.maps.event.addListenerOnce(map, "center_changed",
function() { _this.onReposition(); });
map.backgroundColor = '#ff0000';
this._google = map;
_resetCallback: function(e) {
_reset: function(clearOldContainer) {
_update: function() {
var bounds = this._map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
var google_bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(, sw.lng),
new google.maps.LatLng(, ne.lng)
var center = this._map.getCenter();
var _center = new google.maps.LatLng(, center.lng);
_resize: function() {
var size = this._map.getSize();
if ( == size.x && == size.y)
return; = size.x + 'px'; = size.y + 'px';
google.maps.event.trigger(this._google, "resize");
onReposition: function() {
//google.maps.event.trigger(this._google, "resize");
Copy link

This has been very useful for my work, thanks Crofty!
Its a small detail, but I think there is a trailing comma on line 16

Copy link

Great post!
I used it with Leaflet draw, but when I drawn new features on the OSM layer, I could not see them when swtiching to the Google Layer.. Maybe something related to the z-index? Any thoughts?

Copy link

Just for the record, I used this bit of code with a little more customization in a client site running on Statamic, the static file CMS. Statamic has location stuff built-in but uses Cloudmade for map tiles, and my client wanted Google Maps. So I plugged this into Statamic with a little work and now the CMS is using Google tiles instead of Cloudmade.

Copy link

righteous. thank you, sir.

btw, i had a similar issue to @chmartinez (my markers were being covered by the google tiles). it's easy to fix layer issues with css like so:

    z-index: 0;
    z-index: 100;

Copy link

snario commented Apr 21, 2014

I have an issue where I want to add a native Google control (for example the pegman), so in mapOptions I set the street view property to true and the pegman is visible but I can't click on him. Same thing with any other UI like zoom or panning; they are visible but not clickable. Does anyone know how to fix this? I have tried to order the z-index properties many times but no luck.


Here is a fiddle of what I mean:

Copy link

cballou commented May 23, 2014

I noticed that even if you try to add the Google Map to the bottom layer, it still applies it to the top: map.addLayer(googleLayer, true); This would appear to be a bug. The workaround is as @jyoungblood mentioned, with one caveat.

    z-index: 0 !important;
    z-index: 100;

Copy link

jbhatab commented Jun 29, 2014

Thought I'd post this because it was useful to me. Here's a minimal setup of a coffeescript file that initializes the map with the google layer.

$ ->
map ="map").setView([51.505, -0.09], 13)
googleLayer = new L.Google("ROADMAP")
map.addLayer googleLayer

Copy link

trusktr commented Apr 13, 2015


I think there is a trailing comma on line 16

Trailing commas are fine in JavaScript. In an object literal the trailing comma doesn't do anything, but in an Array it does (the array length is longer depending on how many trailing commas you have).

Copy link

trusktr commented Apr 13, 2015

@crofty Have you used this with overlays (polygons, geojson, etc)? If so, do the overlays move in sync with the map?

Copy link

Whether a trailing comma is valid JavaScript is irrelevant. Old IE doesn't like it. End of.

FWIW it isn't valid JSON for that matter.

Copy link

Try the below option for click option not visible in the map :

this._container = L.DomUtil.create('div', 'leaflet-google-layer leaflet-top leaflet-left');

change to -->

this._container = L.DomUtil.create('div', 'leaflet-google-layer);

Copy link

Hi all,

I'm creating a map and i'm trying to add a tile layer with Leaflet 1.0.0 Beta, this is my code:

var myCenter = new L.LatLng(50.5, 30.51);
var map = new L.Map('map', {center: myCenter, zoom: 15});
var positron = L.tileLayer('http://{s}{z}/{x}/{y}.png', {});

It works perfectly, but when i try to add a Google Layer with Leaflet Google library( doesn't work:

var map = new L.Map('map', {center: myCenter, zoom: 15});
var ggl = new L.Google();

after that i tried to change the version of Leaflet to 0.7.5 and worked perfectly. I need to use Leaflet 1.0.0 Beta because of some features so i can't just change the version on my project.

Check an example in:,console,output

Please could you help me with this?

Copy link

fbazbo commented Nov 17, 2015

@palaniyappan, the change to make the google commands clickable worked, but how would you bypass the map drag while dragging the street view icon?

Never mind, figured it out myself doing something like:

    google.maps.event.addListenerOnce(map, 'idle', function(){
        var pegman = $("div[aria-label='Street View Pegman Control']");
        if (pegman) {
            .mousedown(function() {
            .mouseup(function() {
            if (!this.getVisible()) {

Copy link

@snario do you fix your issue?

I have a similar problem. I set the street view property to true in MapOptions. The pegman is visible but I can't click on him. I try to reorder layers with z-index but doesn't work.

Thank you

Copy link

S767 commented Jul 4, 2016

Due new version on Leaflet you need to change
some extra lines to this plugin for working with out JS error
L.Google = L.Layer.extend within L.Google = L.Class.extend

and comment string
this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);

Copy link

@S767, thank you very much!

Copy link

What version does this code apply to? Not the current one.

Copy link

Unfortunately I can't get this to work with

Copy link

Very laggy.

Copy link

anybody get this work with leaflet 1.0.2 ?

Copy link

Goury commented Mar 16, 2017

Please fix the URL, correct one should be
Your returns 404
Also user HTTPS please.

Also this is not working anymore.

Copy link

I want use google direction

Copy link

Oakwv commented Jan 2, 2018

Does anyone have a working jsfiddle with pegman dragable? I have him visible but nothing happens when I click . Leafelt 1.2.0 GridLayer.GoogleMutant 0.6.4 thanks in advance.

Copy link

License Status

Note on the license status of this file.
As indicated on (the archive page) @crofty has started with which got imported into on 2019-04-18.

The difference to the initially imported file from @shramov shows that @crofty has only made a very small change
which very likely is not copyrightable, see the change:

-o Google-20120418shramov.js 

curl -O
diff -U0 Google-20120418shramov.js leaflet-google-2012crofty.js
--- Google-20120418shramov.js   2019-08-26 11:38:37.062456529 +0200
+++ leaflet-google-2012crofty.js        2019-08-26 11:00:35.545241651 +0200
@@ -4 +3,0 @@
@@ -81,2 +80,2 @@
-              = size.x;
-              = size.y;
+              = size.x + 'px';
+              = size.y + 'px';
@@ -139,2 +138,2 @@
-      = size.x;
-      = size.y;
+      = size.x + 'px';
+      = size.y + 'px';

Thus we can conclude that the 99% of the file the rights will be with @shramov.
He placed the files in the github repository under MIT on 2012-07-31 (see shramov/leaflet-plugins@39cd5da#diff-9879d6db96fd29134fc802214163b95a) , which included an improved Google.js file, which still is quite similar. If we assume that this clarification in July 2012 was meant for earlier versions of the file as well, then this file is most likely copyright @shramov under MIT.

Copy link

shramov commented Aug 30, 2019

Thx for pointing. Yes, that's based on my script. Later it was dropped from leaflet-plugins since there is better version: shramov/leaflet-plugins#250

Copy link

@shramov thanks for the clarification!

Note that the problem with the new "version" is that it is not properly licensed so far, see

Copy link

shramov commented Aug 30, 2019

Licence for this script is really minor issue compared to debatable status of whole plugin - is it legal to use Google maps this way or not :)

Copy link

bernhardreiter commented Aug 30, 2019

@shramov unfortunately those are two different questions, which are both important.

  • As we are using Free Software in a commercial setting, the current Leaflet.GridLayer.GoogleMutant will not pass any serious licensing clearing house (precisely there is no permission to use it, because the license of you and Bergot is violated).
  • When using the Google Maps API with a key, a number of people evaluated this as acceptable. But this is a matter between different parties: google and the organisation that bought the api key from them. Maybe this should or is discussed in more depth somewhere else?

Copy link

Is there a final agreement on whether using the google maps API tiles without showing/using the shell is legal?

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