leaflet-google.js
A Leaflet plugin that allows easy integration with the Google Maps API
For a working example (without API Key) see demo
Initally based on the work of James “Crofty” Croft
How to use
- include CSS & JavaScript
<head> ... <style> html, body, #map { height: 100%; width: 100%; padding: 0; margin: 0; } </style> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script> <script src="https://cdn.jsdelivr.net/gh/Raruto/leaflet-google@master/leaflet-google.js"></script> <script src="https://maps.googleapis.com/maps/api/js?key=<INSERT_HERE_API_KEY>"></script> ... </head> - choose a div container used for the slippy map
<body> ... <div id="map"></div> ... </body> - create your first simple “leaflet-google” slippy map
<script> { const map = new L.Map('map', { center: [41.4583, 12.7059], zoom: 5, markerZoomAnimation: false, zoomControl: false }); const zoomControl = new (L.ZoomControl || L.Control.Zoom)({ position: 'topright' }); const ggl = new L.Google('ROADMAP'); // Possible types: SATELLITE, ROADMAP, HYBRID, TERRAIN const url = 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', attr = 'Map data: © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: © <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)', otm = new L.TileLayer(url, { attribution: attr, /*subdomains:"1234"*/ }); const baseLayers = { "Google Map": ggl, "Leaflet Map": otm, }; const layersControl = new (L.LayersControl || L.Control.Layers)(baseLayers, null, { collapsed:false }); layersControl.addTo(map); zoomControl.addTo(map); map.addLayer(ggl); } </script>
NB to be able to use the “pegman” (a.k.a. “Street View Control”) you MUST use a valid Google Maps API Key.
