Skip to content

Instantly share code, notes, and snippets.

@ajturner
Created February 7, 2012 18:41
Show Gist options
  • Select an option

  • Save ajturner/1761165 to your computer and use it in GitHub Desktop.

Select an option

Save ajturner/1761165 to your computer and use it in GitHub Desktop.
Adds a layer to a map and new features.
// Example of Adding a Structured layer to a map and then adding client side twitter messages
// This example includes categorical styling of sentiment. You can change the schema, the categories, etc.
var geoiq = new function() {
this.map = map;
this.twitter_layer = null;
this.createLayer = function () {
var self = this
var data = {};
var dataset = {};
dataset['title'] = "Twitter - Map "
dataset['description'] = "Twitter Stream from ..."
// Layer schema. Change to match your XML
var attributes = [
{ 'name' : 'username', 'type' : 'string' },
{ 'name' : 'text', 'type' : 'string' },
{ 'name' : 'happiness', 'type' : 'string' },
{ 'name' : 'sentiment', 'type' : 'decimal' },
{ 'name' : 'latitude', 'type' : 'latitude' },
{ 'name' : 'longitude', 'type' : 'longitude' },
{ 'name' : 'created', 'type' : 'datetime' }
];
dataset['attributes'] = attributes;
// This could also be left out to keep it private. But we're just creating an empty dataset
data['permissions'] = [{
"group_id" : "everyone",
"permissions" : {
"view" : 'false',
"download" : 'false',
"edit" : 'false'
}
}]
data['dataset'] = dataset;
var info_window_style = {
title : "$[sentiment] - $[text]",
subtitle : "$[username]",
tabs : [
{
title : "Description",
type : "text",
value : "$[text] \n\n $[username]"
},
{
title : "Details",
type : "text",
value : "Contributed: $[username]\nDate: $[created]\nLocation: $[longitude] x $[latitude]"
}
]
}
// Find better icons
var categorystyle = {
attribute:'happiness',
categories:{
"happy" : "http://www.lasirenas.com/images/happy.gif",
"neutral" : "http://educate.intel.com/NR/rdonlyres/029D7205-DD5C-4113-8C8B-9B123BC0A9CE/5969/neutral_face.gif",
"sad" : "http://www.geniisoft.com/showcase.nsf/sadface.gif"
}
};
// Creates a new empty layer on GeoIQ using the above schema
jq.ajax({
type: 'POST',
url: '/overlays.json',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(data),
processData : false,
complete: function(xhr) {
if (xhr.readyState == 4) {
if (xhr.status == 201) {
var response = JSON.parse(xhr.responseText)
self.twitter_layer = self.map.addLayer({source: 'finder:'+response.id, title: dataset['title'], categoryFilter: categorystyle,
infoWindowFilter: info_window_style, zoomToExtent: false, show_styles: false});
}
} else {
console.log("Error creating layer")
}
}
});
}
};
// If your variable isn't called 'map'
//geoiq.map = map_2492;
// Create the blank 'bucket' dataset in GeoIQ and add Layer to map
geoiq.createLayer(F1.Maker.current_map)
// Put your XML parsing code here in a setTimeout loop
var features = [{
username: "Bob",
text: "First Twitter",
sentiment: 0.8,
happiness: "happy",
created: '2011-01-01T12:40:00Z',
latitude: 45, longitude: -100, // have to
geometry: {type: "Point", coordinates: [-100, 45]}
}];
// Add these features within the browser to the map
geoiq.map.addFeatures(geoiq.twitter_layer, features, false);
// Get a list of twitter features that are on the map
console.log("Features", geoiq.map.getFeatures(geoiq.twitter_layer) )
geoiq.map.addFilter(geoiq.twitter_layer, {expression: "$[happiness] == 'happy'"})
geoiq.map.clearFilters(geoiq.twitter_layer)
geoiq.map.addFilter(geoiq.twitter_layer, {expression: "$[happiness] != 'happy'"})
geoiq.map.clearFilters(geoiq.twitter_layer)
geoiq.map.addFilter(geoiq.twitter_layer, {expression: "$[sentiment] > 0.5"})
geoiq.map.clearFilters(geoiq.twitter_layer)
geoiq.map.addFilter(geoiq.twitter_layer, {expression: "$[sentiment] < -0.5"})
geoiq.map.clearFilters(geoiq.twitter_layer)
geoiq.map.addHighlight(geoiq.twitter_layer, {expression: "$[sentiment] > -0.5"})
geoiq.map.clearHighlights(geoiq.twitter_layer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment