HarvardMaps API
on the CS50 Wiki
The HarvardMaps API allows you to request data programmatically from HarvardMaps in CSV, JSON, JSONP, or serialized PHP format. The API provides a RESTful interface, which means that you can query it using simple HTTP GET requests.
Contents |
Author
To report bugs or request features, contact:
David J. Malan '99
http://www.cs.harvard.edu/malan/
License
Use of this API is governed by a Creative Commons Attribution-Noncommerical 3.0 Unported license, which means that you may use its data for non-commercial purposes so long as you attribute your data to this API, as by including a link to HarvardMaps in your own work. To discuss commercial purposes, contact the author.
Methods
buildings
To query the HarvardMaps API for buildings, contact the server via URLs in this format:
http://maps.cs50.net/api/1.1/channels?param1=value1¶m2=value2&...
Supported parameters include:
| parameter | description |
|---|---|
| bid | Optional. Comma-separated list of buildings' IDs. If omitted, all buildings will be returned. |
| callback | Required iff output is jsonp. Callback function with which response will be padded. |
| output | Required. Format for output. Must be csv, json, jsonp, or php. |
If any parameters are malformed or any required parameters are omitted, an empty response will be returned along with, possibly, HTTP status code 400 Bad Request.
Note that buildings' IDs are not strictly numeric; some are alphanumeric.
Because the HarvardMaps API and the HarvardEnergy API rely on data from different sources on campus, this method's output may not be identical to the HarvardEnergy API's buildings method.
CSV
If you request buildings in CSV format (e.g., http://maps.cs50.net/api/1.1/buildings?bid=952,991&output=csv), the response will be a series of comma-delimited rows, per the below:
bid,name,lat,lng 991,"Mather House",42.368270,-71.115270 952,"Matthews Hall",42.374070,-71.118150
Note that the response's first row defines the CSV file's fields. Fields' order may change over time, so do not hardcode indices into your own code. Use that first row to determine fields' indices dynamically, per this article on parsing CSV.
Beware opening the CSV file in Excel, as some buildings' IDs have leading 0s that Excel likes to remove.
JSON
If you request channels in JSON format (e.g., http://maps.cs50.net/api/1.1/buildings?bid=952,991&output=json), the response wil be an array of objects, per the below:
[ { "bid":"991", "name":"Mather House", "lat":"42.368270", "lng":"-71.115270" }, { "bid":"952", "name":"Matthews Hall", "lat":"42.374070", "lng":"-71.118150" } ]
JSONP
If you request channels in JSONP format (e.g., http://maps.cs50.net/api/1.1/buildings?bid=952,991&output=jsonp&callback=parseResponse), the response will be a padded array of objects, per the below:
parseResponse([{"bid":"991","name":"Mather House","lat":"42.368270","lng":"-71.115270"},{"bid":"952","name":"Matthews Hall","lat":"42.374070","lng":"-71.118150"}])
PHP
If you request channels in serialized PHP format (e.g., http://maps.cs50.net/api/1.1/buildings?bid=952,991&output=php), the response will be a serialized array of associative arrays, per the below:
a:2:{i:0;a:4:{s:3:"bid";s:3:"991";s:4:"name";s:12:"Mather House";s:3:"lat";s:9:"42.368270";s:3:"lng";s:10:"-71.115270";}i:1;a:4:{s:3:"bid";s:3:"952";s:4:"name";s:13:"Matthews Hall";s:3:"lat";s:9:"42.374070";s:3:"lng";s:10:"-71.118150";}}
Once you unserialize that response, you'll have the below in memory:
Array ( [0] => Array ( [bid] => 991 [name] => Mather House [lat] => 42.368270 [lng] => -71.115270 ) [1] => Array ( [bid] => 952 [name] => Matthews Hall [lat] => 42.374070 [lng] => -71.118150 ) )
Examples
- Returns all buildings:
- Returns Mather House:
See Also
Related APIs
- HarvardCourses API
- HarvardEnergy API
- HarvardEvents API
- HarvardFood API
- HarvardNews API
- HarvardTweets API
- Shuttleboy API
External Links
Changelog
- 1.0
- 1.1
- Added support for JSONP