Skip to content

How to use OpenHeatMap within your own site

petewarden edited this page Oct 25, 2010 · 20 revisions

OpenHeatMap is packaged as a JQuery plugin designed to be easily be reused by web developers who want to display map data within their own sites. Here’s the code to embed and populate your own heatmap:

$(document).ready(function() {
  $(‘#openheatmap_container’).insertOpenHeatMap({
    width: 800,
    height: 600
  });
});

function onMapCreated() {
  var map = $.getOpenHeatMap();
  map.loadWaysFromFile(‘http://static.openheatmap.com/world_countries.osm’);
  map.loadValuesFromFile(‘hello_data.csv’);
}

That will create a map of the world inside the HTML element with the id ‘openheatmap_container’ inside your page, with the data inside the hello_data.csv file displayed on top of the map. Here’s what that CSV file could look like:

country_code,value
AFG,10
AUS,20
CAN,30
USA,40

You can see the full source to a minimal but complete example at http://github.com/petewarden/openheatmap/tree/master/examples/hello_world/ and try it running live at http://www.openheatmap.com/examples/hello_world/

The component depends on Jquery and you’ll need to also include the OpenHeatMap script from http://static.openheatmap.com/scripts/jquery.openheatmap.js

Cross-domain

Because the rendering code is held on the openheatmap.com server, you will need to grant cross-domain access if you want to allow it to read the CSV files from your own server. You can download mine from http://www.openheatmap.com/crossdomain.xml or here’s the contents of a crossdomain.xml file to enable that, you’ll need to save it in the root folder of your webserver:

<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-access-from domain="static.openheatmap.com" /> <allow-access-from domain="static.openheatmap.com.s3.amazonaws.com" />

As an alternative, you can copy the http://static.openheatmap.com/openheatmap.swf file over to your local server, and supply your local URL as the optional fourth argument to insertOpenHeatMapInto(). If you take that route you won’t get automatically get updates and bug fixes to the rendering component, and you may want to copy over the openheatmap.js file also to ensure everything you need is local.

Next steps

To make your map interactive and integrated with your own service, you’ll want to look at the Events list to see what behavior you can tie your own code into, and check out the Component API calls to see what customization options you have.