Skip to content

Instantly share code, notes, and snippets.

@fumingshih
Created December 1, 2013 21:08
Show Gist options
  • Save fumingshih/7740805 to your computer and use it in GitHub Desktop.
Save fumingshih/7740805 to your computer and use it in GitHub Desktop.
GAS script that read the location data from spreadsheet and create a static map in the uiApp.
// test showing the map on website
function doGet() {
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('locations');
var sheet = SpreadsheetApp.openById('0AmcViZZV0aRjdEZ0amxsQ2NERmthRWx6aF9MVlgyUHc').getSheetByName('locations');
// Read out the data for my locations
var locations = sheet.getRange(2, 1, sheet.getLastRow() - 1, 3).getValues();
// Create a new StaticMap
var locationMap = Maps.newStaticMap();
// Create a new UI Application, which we use to display the map
var ui = UiApp.createApplication();
// Create a grid widget that use to display the location and address name using reverse geo-coding
// The grid will displaya location id, time, address name
var grid = ui.createGrid(locations.length + 1, 3);
grid.setWidget(0, 0, ui.createLabel('Id').setStyleAttribute('fontWeight', 'bold'));
grid.setWidget(0, 1, ui.createLabel('time').setStyleAttribute('fontWeight', 'bold'));
grid.setWidget(0, 2, ui.createLabel('address').setStyleAttribute('fontWeight', 'bold'));
// For each entry in locations, create a map marker
for (var i = 0; i < locations.length; i++) {
locationMap.setMarkerStyle(Maps.StaticMap.MarkerSize.MID,
Maps.StaticMap.Color.GREEN, 'T');
locationMap.addMarker(locations[i][0], locations[i][1]);
grid.setWidget(i + 1, 0, ui.createLabel((i + 1).toString()));
var datestring = Utilities.formatDate(new Date(locations[i][2]), 'EDT', "yyyy-MM-dd HH:mm:ss");
grid.setWidget(i + 1, 1, ui.createLabel(datestring));
var response = Maps.newGeocoder().reverseGeocode(locations[i][0],locations[i][1]);
var address = response.results[0];// try to get the first one
//var anchor = ui.createAnchor(address.formatted_address, "maps/#!q="+ encodeURIComponent(address.formatted_address));
var anchor = ui.createAnchor(address.formatted_address, "maps/?q="+ encodeURIComponent(address.formatted_address));
//grid.setWidget(i + 1, 2, ui.createLabel(address.formatted_address));
grid.setWidget(i + 1, 2, anchor);
}
var vpanel = ui.createVerticalPanel();
var scrollPanel = ui.createScrollPanel().setPixelSize(800, 800);
// Get the URL of the restaurant map and use that to create an image and add
// it to the panel. Next add the grid to the panel.
var imagePanel = ui.createVerticalPanel();
var mapUrl = locationMap.getMapUrl();
imagePanel.add(ui.createImage(mapUrl))
Logger.log("map url:" + mapUrl);
vpanel.add(imagePanel);
vpanel.add(grid);
scrollPanel.add(vpanel);
// Finally, add the panel widget to our UI instance, and set its height,
// width, and title.
ui.add(scrollPanel);
ui.setHeight(800);
ui.setWidth(800);
ui.setTitle('StayFit@mit Collects the Following Locations');
return ui;
}
// Script-as-app template.
function doGet() {
var sheet = SpreadsheetApp.openById('0AmcViZZV0aRjdEZ0amxsQ2NERmthRWx6aF9MVlgyUHc').getSheetByName('locations');
var locationMap = Maps.newStaticMap();
// Read out the data for my locations
var locations = sheet.getRange(2, 1, sheet.getLastRow() - 1, 3).getValues();
locationMap.setMarkerStyle(Maps.StaticMap.MarkerSize.MID,
Maps.StaticMap.Color.GREEN, 'T');
for (var i = 0; i < locations.length; i++) {
locationMap.addMarker(locations[i][0], locations[i][1]);
}
Logger.log(locationMap.getMapUrl());
var result = {
name: 'fuming',
url: locationMap.getMapUrl(),
age: 35,
};
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment