-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mostly redone, using jquery plugins and mapbox.js
- Loading branch information
Showing
13 changed files
with
238 additions
and
5,279 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.