Skip to content

Commit

Permalink
Mostly redone, using jquery plugins and mapbox.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Jul 25, 2012
1 parent 2cab9bb commit b825430
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 5,279 deletions.
459 changes: 0 additions & 459 deletions ext/easey.handlers.js

This file was deleted.

167 changes: 0 additions & 167 deletions ext/easey.js

This file was deleted.

62 changes: 62 additions & 0 deletions ext/mapbox.jquery.geocoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
!function($) {

$.fn.geocode = geocode;

function geocode(e) {

e.preventDefault();
var $this = $(this),
map = $('#' + $this.parents('[data-map]').data('map')).data('map'),
query = encodeURIComponent($this.find('input[type=text]').val());

$this.addClass('loading');

reqwest({
url: 'http://open.mapquestapi.com/nominatim/v1/search?format=json&json_callback=callback&&limit=1&q=' + query,
type: 'jsonp',
jsonpCallback: 'callback',
jsonpCallbackName: 'callback',
success: success
});

function success(resp) {
resp = resp[0];
$this.removeClass('loading');

if (!resp) {
$this.find('#geocode-error').text('This address cannot be found.').fadeIn('fast');
console.log(resp);
return;
}

$this.find('#geocode-error').hide();

map.setExtent([
{ lat: resp.boundingbox[1], lon: resp.boundingbox[2] },
{ lat: resp.boundingbox[0], lon: resp.boundingbox[3] }
]);

if (!map.getLayer('geocode')) {
var layer = mapbox.markers.layer().named('geocode');
map.addLayer(layer);
layer.tilejson = function() { return {
attribution: 'Search by <a href="http://developer.mapquest.com/web/products/open">MapQuest Open</a>'
}};
}

map.getLayer('geocode').features([]).add_feature({
'type': 'Feature',
'geometry': { 'type': 'Point', 'coordinates': [resp.lon, resp.lat] },
'properties': {}
});

map.ui.refresh(); // Update attribution
}
}


$(function() {
$('[data-control="geocode"] form').submit(geocode);
});

}(window.jQuery);
91 changes: 91 additions & 0 deletions ext/mapbox.jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
!function($) {

// Either creates map or goes full auto
// If map already created, map returned
$.fn.mapbox = function(a, b, c) {

// Return map if already created
if (this.data('map')) return this.data('map');

// Load tilejsons and create map using mapbox.auto
if (typeof a === 'string' || (a instanceof Array && typeof a[0] === 'string')) {
mapbox.auto(this.attr('id'), a, $.proxy(function(m, tj) {
this.data('map', m);
if (b) b(m, tj);
}, this));
return this;

// Initialize a mapbox map in element
} else {
var map = mapbox.map(this.attr('id'), a, b, c);
return map;
}

};

// Expose functionality as jQuery plugin
$.fn.switchLayer = switchLayer;

function switchLayer(e) {
var $this = $(this),
$parent = $this.parents('[data-control="switcher"]'),
group = $this.data('group') || 0,
map = $('#' + $this.parents('[data-map]').data('map')).data('map'),
name = $this.attr('href').replace('#','');

if (!map.getLayer(name).enabled) {
// Disable all layers in same group
var layers = $parent.find('a');
for (var i = 0; i < layers.length; i++) {
var l = map.getLayer($(layers[i]).attr('href').replace('#',''));
if (l && group == $(layers[i]).data('group') && l.enabled) {
$(layers[i]).removeClass('active');
$(layers[i]).trigger('disabled');
l.disable();
}
}

map.enableLayer(name);
$this.addClass('active');
$this.trigger('enabled');

} else if ($this.data('toggle')) {
// Toggle layer off
map.disableLayer(name);
$this.removeClass('active');
map.draw();
}
map.ui.refresh();
return false;
}

$(function() {
$('body').on('click.switcher.data-api', '[data-control="switcher"] a', switchLayer);
});

$.fn.ease = easeMap;

function easeMap(e, force) {
var $this = $(this);

// Don't ease when toggling layer off
if ($this.data('toggle') && e.type !== 'enabled') return false;

var mapid = $this.data('map') || $this.parents('[data-map]').data('map'),
map = $('#' + mapid).data('map');

if (!map) return false;

var lat = $this.data('lat') || map.center().lat,
lon = $this.data('lon') || map.center().lon,
zoom = $this.data('zoom') || map.zoom();

map.ease.location({ lat: lat, lon: lon}).zoom(zoom).optimal();
return false;
}

$(function() {
$('body').on('click.ease.data-api enabled.ease', '[data-lat],[data-lon],[data-zoom]', easeMap);
});

}(window.jQuery);
Loading

0 comments on commit b825430

Please sign in to comment.