How to add a Google Maps styled layer in Qgis 2

I have been struggling for days to make a very simple and light base-map of the UK showing town circle markers, town names sized according to their population, a couple of country borders and nothing more than a plain white background for lands above sea-level.

After using numerous data sources (Natural Earth, Ordnance Survey, Open Gazetteers, Open Street Map vectors, OSM-GB WMS layers - none of which worked well) I finally came across an acceptable solution...

...which is to make a Google Maps Styled and to tweak Qgis Openlayers Plugin in order to load such a customised layer.


Firstly, make your styled Google Map. You can find mine on Snazzymaps. It is very easy to make your own, just have a go on the Styled Maps Wizard.

Once you have finished your customisations, export the style Javascript Array and place it as the value of the stylez variable in this Html.


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>OpenLayers Google Streets Layer</title>
    <link rel="stylesheet" href="qgis.css" type="text/css">
    <link rel="stylesheet" href="google.css" type="text/css">
    <script src="http://maps.google.com/maps/api/js?v=3.3&amp;sensor=false"></script>
    <script src="OpenLayers.js"></script>
    <script src="OlOverviewMarker.js"></script>
    <script type="text/javascript">
        var map;
        var loadEnd;
        var oloMarker; // OpenLayer Overview Marker
        function init() {
            map = new OpenLayers.Map('map', {
                theme: null,
                controls: [],
                units: "m",
                maxResolution: 156543.0339,
                maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)
            });

            var stylez = [ { featureType: "all", elementType: "all", stylers: [ { visibility: "simplified" } ] } ];
            
            var styledMapType = new google.maps.StyledMapType(stylez, styledMapOptions);           
            
            var gmap = new OpenLayers.Layer.Google(
                "Google Custom",
                { type: 'styled' }
            );
            map.addLayer(gmap);
            
            gmap.mapObject.mapTypes.set('styled', styledMapType);
            gmap.mapObject.setMapTypeId('styled');

            loadEnd = false;
            map.events.register('movestart', map, function() {
                loadEnd = false;
            });
            google.maps.event.addListener(gmap.mapObject, "tilesloaded", function() {
                // wait for tiles to fade in completely
                setTimeout(function() {
                  loadEnd = true;
                },
                200);
            });

            map.setCenter(new OpenLayers.LonLat(0, 0), 2);
            oloMarker = new OlOverviewMarker(map, getPathUpper(document.URL) + '/x.png');
            
        }
    </script>
  </head>
  <body onload="init()">
    <div id="map"></div>
  </body>
</html>


Save this code as google_custom.html in this folder:
C:\Users\youruser\.qgis2\python\plugins\openlayers_plugin\html\

For the sophisticated users:
%userprofile%\.qgis2\python\plugins\openlayers_plugin\html\

After that, enter folder C:\Users\youruser\.qgis2\python\plugins\openlayers_plugin\
and open openlayers_plugin.py with a text editor.

Go around line 124, where you find the #Layer comment and add this line to the layers section, just below the Google satellite entry:

    self.olLayerTypeRegistry.add( OlLayerType(self, 'Google Custom', 'google_icon.png', 'google_custom.html', True) )

Save and close the file and restart Qgis. A new Google custom item appears in the Openlayers plugin Menu.

Enjoy!

1 comment:

  1. interesting, but it doesnt work on qgis 2.16.1

    cant find #Layer comment

    ReplyDelete