Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2018 17:12
Show Gist options
  • Save brool/f2b7eb0e1b85df5d4ebd6b583b52ce53 to your computer and use it in GitHub Desktop.
Save brool/f2b7eb0e1b85df5d4ebd6b583b52ce53 to your computer and use it in GitHub Desktop.
Example of custom map with marker data read from Google spreadsheets
<!-- see for explanation -->
<link rel="stylesheet" href=""
<script src=""
<div id="map" style="height: 100%; width: 100%"></div>
var map ='map', {
crs: L.CRS.Simple,
minZoom: -1
var bounds = [[0,0], [2304,2560]];
// this represents the "center" of the map, with +X +Y being to the lower right.
// adjust to your own particular coordinate system
var center = [1048, 1408];
var image = L.imageOverlay('map.png', bounds).addTo(map);
var popup = L.popup();
function onMapClick(e) {
var coordinates = (Math.round(e.latlng.lng) - center[0]) + "," + (center[1] - Math.round(
.setContent("Coordinates: " + coordinates)
map.on('click', onMapClick);
// Load up our spreadsheet
var url = '[SPREADSHEET ID]/values/[START COL ROW i.e. A2]%3A[END COL ROW]?dateTimeRenderOption=SERIAL_NUMBER&majorDimension=DIMENSION_UNSPECIFIED&valueRenderOption=FORMATTED_VALUE&key=[API KEY]';
$(document).ready(function () {
jQuery.getJSON(url, function (data) {
for (ix = 0; ix < data.values.length; ix++) {
// spreadsheet is: X, Y, marker type (not used), added by (person), description (text)
var x = parseInt(data.values[ix][0]) + center[0];
var y = -parseInt(data.values[ix][1]) + center[1];
var marker = data.values[ix][2];
var addedby = data.values[ix][3];
// description and added by should be sanitized
var description = data.values[ix][4];
if (addedby > "") {
description += "<br/><i>Added by: " + addedby + "</i>";
console.log(x, y, marker, addedby, description);
var marker = L.marker([y, x]).bindPopup(description).openPopup().addTo(map);
Copy link

brool commented Aug 29, 2018

Simple example from

Be sure to clean up addedby and description, or anyone with access to your spreadsheet will be able to inject arbitrary code.

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