Created
July 4, 2014 14:35
-
-
Save VonD/0acc383d274ba07b863f to your computer and use it in GitHub Desktop.
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
/*! L.Mappy 5.0.3 2014-03-27 */ | |
(function(L) { | |
'use strict'; | |
var random_string = function(length) { | |
var str; | |
str = ''; | |
while (str.length < length) { | |
str += Math.random().toString(36)[2]; | |
} | |
return str; | |
}; | |
var object_to_uri = function(obj) { | |
var data, key, value; | |
data = []; | |
for (key in obj) { | |
value = obj[key]; | |
data.push(window.encodeURIComponent(key) + '=' + window.encodeURIComponent(value)); | |
} | |
return data.join('&'); | |
}; | |
L.Mappy = { | |
version: '5.0.3', | |
_domain: 'mappy.net', | |
_token: null, | |
_https: false, | |
setToken: function(token) { | |
this._token = token; | |
}, | |
_getToken: function() { | |
return this._token; | |
}, | |
enableHttps: function() { | |
this._https = true; | |
}, | |
disableHttps: function() { | |
this._https = false; | |
}, | |
_getHttps: function() { | |
return this._https; | |
}, | |
_getDomain: function() { | |
return this._domain; | |
}, | |
JSONP: function(options) { | |
var callback, done, head, params, script; | |
options = options ? options : {}; | |
params = { | |
data: options.data || {}, | |
error: options.error || L.Util.falseFn, | |
success: options.success || L.Util.falseFn, | |
url: options.url || '' | |
}; | |
if (params.url.length === 0) { | |
throw new Error('MissingUrl'); | |
} | |
done = false; | |
callback = params.data[options.callback_name || 'callback'] = 'jsonp_' + random_string(15); | |
window[callback] = function(data) { | |
params.success(data); | |
// return delete window[callback]; | |
window[callback] = null; | |
}; | |
script = window.document.createElement('script'); | |
script.src = params.url; | |
script.src += params.url.indexOf('?' === -1) ? '?' : '&'; | |
script.src += object_to_uri(params.data); | |
script.async = true; | |
script.onerror = function(evt) { | |
return params.error({ | |
url: script.src, | |
event: evt | |
}); | |
}; | |
script.onload = script.onreadystatechange = function() { | |
if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) { | |
done = true; | |
script.onload = script.onreadystatechange = null; | |
if (script && script.parentNode) { | |
return script.parentNode.removeChild(script); | |
} | |
} | |
}; | |
head = head || window.document.getElementsByTagName('head')[0]; | |
return head.appendChild(script); | |
} | |
}; | |
var Attribution = L.Control.Attribution.extend({ | |
options: { | |
'prefix': '© ' + | |
'<a href="http://corporate.mappy.com/conditions-dutilisation/copyright/" title="Mappy">Mappy</a> - ' + | |
'<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>' | |
}, | |
_layers: [], | |
onAdd: function(map) { | |
map.on('layeradd', function(evt) { | |
if (evt.layer instanceof TileLayer) { | |
this._layers.push(evt.layer); | |
evt.layer.on('attributionsrefresh', this._refreshAttributions, this); | |
} | |
}, this); | |
map.on('layerremove', function(evt) { | |
for (var i = this._layers.length - 1; i >= 0; i--) { | |
if (this._layers[i] === evt.layer) { | |
this._layers.splice(i, 1); | |
evt.layer.off('attributionsrefresh', this._refreshAttributions, this); | |
this._refreshAttributions(); | |
} | |
} | |
}, this); | |
return L.Control.Attribution.prototype.onAdd.call(this, map); | |
}, | |
/* Remove the option of removing the attributions */ | |
removeFrom: L.Util.falseFn, // 0.7 | |
remove: L.Util.falseFn, // 0.8 | |
clearAttributions: function() { | |
this._attributions = []; | |
this._update(); | |
}, | |
/** | |
* add on map attributions of all layers (overlay/viewmode) | |
**/ | |
_refreshAttributions: function() { | |
this.clearAttributions(); | |
var attribs = []; | |
for (var i = this._layers.length - 1; i >= 0; i--) { | |
attribs = attribs.concat(this._layers[i].getAttributions()); | |
} | |
for (var j = 0; j < attribs.length; j++) { | |
this.addAttribution(attribs[j]); | |
} | |
} | |
}); | |
var Layers = L.Control.Layers.extend({ | |
_onInputClick: function (evt) { | |
var inputs = this._form.getElementsByTagName('input'), | |
input, layer, hasLayer; | |
if (evt) { | |
var target = evt.target; | |
if (evt.target.type !== 'checkbox') { | |
target = evt.target.parentNode.getElementsByTagName('input')[0]; | |
} | |
for (var i = 0, len = inputs.length; i < len; i++) { | |
input = inputs[i]; | |
if (input.type == 'checkbox' && target.type == 'checkbox' && input !== target) { | |
input.checked = false; | |
} | |
} | |
} | |
this._handlingClick = true; | |
for (var j = 0, len2 = inputs.length; j < len2; j++) { | |
input = inputs[j]; | |
layer = this._layers[input.layerId].layer; | |
hasLayer = this._map.hasLayer(layer); | |
if (input.checked && !hasLayer) { | |
this._map.addLayer(layer); | |
} else if (!input.checked && hasLayer) { | |
this._map.removeLayer(layer); | |
} | |
} | |
this._handlingClick = false; | |
this._refocusOnMap(); | |
} | |
}); | |
var Logo = L.Control.extend({ | |
options: { | |
position: 'bottomleft', | |
dir: '../images/' | |
}, | |
onAdd: function(map) { | |
this._container = L.DomUtil.create('div', 'mappy-control-logo'); | |
var image = L.DomUtil.create('img', 'mappy-logo', this._container); | |
image.src = this.options.dir + (L.Browser.retina ? 'api-logo-2x.png' : 'api-logo.png'); | |
image.style.display = 'block'; | |
image.width = '50'; | |
image.height = '12'; | |
image.onerror = function() { | |
throw new Error('Logo is not accessible.'); | |
}; | |
return this._container; | |
}, | |
/* Remove the option of removing the logo */ | |
removeFrom: L.Util.falseFn, // 0.7 | |
remove: L.Util.falseFn // 0.8 | |
}); | |
var TileLayer = L.TileLayer.extend({ | |
options: { | |
minZoom: 0, | |
maxZoom: 12, | |
tileSize: 384, | |
subdomains: '1234', | |
tms: true | |
}, | |
tileUrl: 'http{h}://map{s}.{domain}/map/1.0/slab/{m}/{tileSize}/{z}/{x}/{y}', | |
descrUrl: 'http{h}://map1.{domain}/map/1.0/multi-descr/{m}/{tileSize}/{z}/{t}', | |
descrInfos: {}, | |
attributions: [], | |
layerItems: [], | |
initialize: function(name, zIndex, options) { | |
options = L.setOptions(this, L.extend({ | |
h: L.Mappy._getHttps() ? 's': '', | |
m: name, | |
zIndex: zIndex, | |
domain: L.Mappy._getDomain() | |
}, options)); | |
L.TileLayer.prototype.initialize.call(this, this.tileUrl, options); | |
}, | |
onAdd: function(map) { | |
L.TileLayer.prototype.onAdd.call(this, map); | |
this.on('load', this._onLoad); | |
}, | |
onRemove: function(map) { | |
L.TileLayer.prototype.onRemove.call(this, map); | |
this.off('load', this._onLoad); | |
}, | |
getAttributions: function() { | |
return this.attributions; | |
}, | |
_onLoad: function() { | |
var bounds = this._map.getPixelBounds(); | |
var min = bounds.min.clone(); | |
var max = bounds.max.clone(); | |
// Retrieve tile x & y values. | |
min = min.divideBy(this.options.tileSize)._floor(); | |
max = max.divideBy(this.options.tileSize)._floor(); | |
var tilesCoords = []; | |
// Correct them reflecting weird options | |
this._adjustTilePoint(min); | |
this._adjustTilePoint(max); | |
// We got it ! | |
for (var x = min.x; x <= max.x; x++) { | |
for (var y = max.y; y <= min.y; y++) { | |
tilesCoords.push(x + ',' + y); | |
} | |
} | |
if (tilesCoords.length > 0) { | |
this._requestDescr(tilesCoords); | |
} | |
}, | |
_refreshAttributions: function(tiles) { | |
this.attributions = []; | |
this.layerItems = []; | |
for (var i = 0; i < tiles.length; i++) { | |
var key = this.options.m + '/' + this._map.getZoom() + '/' + tiles[i].replace(',', '/'); | |
if (this.descrInfos[key]) { | |
for (var j = 0; j < this.descrInfos[key].copyrights.length; j++) { | |
this.attributions.push(this.descrInfos[key].copyrights[j].name); | |
} | |
this.layerItems = this.layerItems.concat(this.descrInfos[key].items); | |
} | |
} | |
this.fire('attributionsrefresh'); | |
}, | |
_requestDescr: function(tiles) { | |
// TODO cors | |
var zoomLevel = this._map.getZoom(); | |
// Define tiles to request | |
var reqTiles = []; | |
for (var i = 0; i < tiles.length; i++) { | |
var key = this.options.m + '/' + zoomLevel + '/' + tiles[i].replace(',', '/'); | |
if (!this.descrInfos[key]) { | |
reqTiles.push(tiles[i]); | |
} | |
} | |
if (reqTiles.length > 0) { | |
var requestUrl = L.Util.template(this.descrUrl, L.extend({ | |
z: zoomLevel, | |
t: reqTiles.join(';') | |
}, this.options)); | |
var self = this; | |
// Request multi-descr | |
L.Mappy.JSONP({ | |
url: requestUrl, | |
success: function(response) { | |
for (var i = 0; i < response.length; i++) { | |
self.descrInfos[self.options.m + '/' + response[i].sid] = response[i]; | |
} | |
self._refreshAttributions(tiles); | |
} | |
}); | |
} | |
else { | |
this._refreshAttributions(tiles); | |
} | |
} | |
}); | |
var Tooltip = L.Class.extend({ | |
options: { | |
width: 'auto', | |
minWidth: '', | |
maxWidth: '', | |
padding: '2px 4px', | |
showDelay: 500, | |
hideDelay: 500, | |
mouseOffset: L.point(0, 20), | |
fadeAnimation: true, | |
trackMouse: false | |
}, | |
initialize: function (options) { | |
L.setOptions(this, options); | |
this._createTip(); | |
}, | |
_createTip: function () { | |
this._map = this.options.map; | |
if (!this._map) { | |
throw new Error('No map configured for tooltip'); | |
} | |
this._container = L.DomUtil.create('div', 'leaflet-tooltip'); | |
this._container.style.position = 'absolute'; | |
this._container.style.width = this._isNumeric(this.options.width) ? this.options.width + 'px' : this.options.width; | |
this._container.style.minWidth = this._isNumeric(this.options.minWidth) ? this.options.minWidth + 'px' : this.options.minWidth; | |
this._container.style.maxWidth = this._isNumeric(this.options.maxWidth) ? this.options.maxWidth + 'px' : this.options.maxWidth; | |
this._container.style.padding = this._isNumeric(this.options.padding) ? this.options.padding + 'px' : this.options.padding; | |
if (this.options.html) { | |
this.setHtml(this.options.html); | |
} | |
if (this.options.target) { | |
this.setTarget(this.options.target); | |
} | |
this._map._tooltipContainer.appendChild(this._container); | |
}, | |
isVisible: function () { | |
return this._showing; | |
}, | |
setTarget: function (target) { | |
if (target._icon) { | |
target = target._icon; | |
} | |
if (target === this._target) { | |
return; | |
} | |
if (this._target) { | |
this._unbindTarget(this._target); | |
} | |
this._bindTarget(target); | |
this._target = target; | |
}, | |
_bindTarget: function (target) { | |
L.DomEvent | |
.on(target, 'mouseover', this._onTargetMouseover, this) | |
.on(target, 'mouseout', this._onTargetMouseout, this) | |
.on(target, 'mousemove', this._onTargetMousemove, this); | |
}, | |
_unbindTarget: function (target) { | |
L.DomEvent | |
.off(target, 'mouseover', this._onTargetMouseover, this) | |
.off(target, 'mouseout', this._onTargetMouseout, this) | |
.off(target, 'mousemove', this._onTargetMousemove, this); | |
}, | |
setHtml: function (html) { | |
if (typeof html === 'string') { | |
this._container.innerHTML = html; | |
} else { | |
while (this._container.hasChildNodes()) { | |
this._container.removeChild(this._container.firstChild); | |
} | |
this._container.appendChild(this._content); | |
} | |
this._sizeChanged = true; | |
}, | |
setPosition: function (point) { | |
var mapSize = this._map.getSize(), | |
container = this._container, | |
containerSize = this._getElementSize(this._container); | |
point = point.add(this.options.mouseOffset); | |
if (point.x + containerSize.x > mapSize.x) { | |
container.style.left = 'auto'; | |
container.style.right = (mapSize.x - point.x) + 'px'; | |
} else { | |
container.style.left = point.x + 'px'; | |
container.style.right = 'auto'; | |
} | |
if (point.y + containerSize.y > mapSize.y) { | |
container.style.top = 'auto'; | |
container.style.bottom = (mapSize.y - point.y + 2*(this.options.mouseOffset.y)) + 'px'; | |
} else { | |
container.style.top = point.y + 'px'; | |
container.style.bottom = 'auto'; | |
} | |
}, | |
remove: function () { | |
this._container.parentNode.removeChild(this._container); | |
delete this._container; | |
if (this._target) { | |
this._unbindTarget(this._target); | |
} | |
}, | |
show: function (point, html) { | |
if (Tooltip.activeTip && Tooltip.activeTip != this) { | |
Tooltip.activeTip._hide(); | |
} | |
Tooltip.activeTip = this; | |
if (html) { | |
this.setHtml(html); | |
} | |
this.setPosition(point); | |
if (this.options.showDelay) { | |
this._delay(this._show, this, this.options.hideDelay); | |
} else { | |
this._show(); | |
} | |
}, | |
_show: function () { | |
this._container.style.display = 'inline-block'; | |
// Necessary to force re-calculation of the opacity value so transition will run correctly | |
// if (window.getComputedStyle) { | |
// window.getComputedStyle(this._container).opacity; | |
// } | |
L.DomUtil.addClass(this._container, 'leaflet-tooltip-fade'); | |
this._showing = true; | |
}, | |
hide: function () { | |
if (this.options.hideDelay) { | |
this._delay(this._hide, this, this.options.hideDelay); | |
} else { | |
this._hide(); | |
} | |
}, | |
_hide: function () { | |
if (this._timeout) { | |
clearTimeout(this._timeout); | |
} | |
L.DomUtil.removeClass(this._container, 'leaflet-tooltip-fade'); | |
this._container.style.display = 'none'; | |
this._showing = false; | |
if (Tooltip.activeTip === this) { | |
delete Tooltip.activeTip; | |
} | |
}, | |
_delay: function (func, scope, delay) { | |
var me = this; | |
if (this._timeout) { | |
clearTimeout(this._timeout); | |
} | |
this._timeout = setTimeout(function () { | |
func.call(scope); | |
delete me._timeout; | |
}, delay); | |
}, | |
_isNumeric: function (val) { | |
return !isNaN(parseFloat(val)) && isFinite(val); | |
}, | |
_getElementSize: function (el) { | |
var size = this._size; | |
if (!size || this._sizeChanged) { | |
size = {}; | |
el.style.left = '-999999px'; | |
el.style.right = 'auto'; | |
el.style.display = 'inline-block'; | |
size.x = el.offsetWidth; | |
size.y = el.offsetHeight; | |
el.style.left = 'auto'; | |
el.style.display = 'none'; | |
this._sizeChanged = false; | |
} | |
return size; | |
}, | |
_onTargetMouseover: function (e) { | |
var point = this._map.mouseEventToContainerPoint(e); | |
this.show(point); | |
}, | |
_onTargetMousemove: function (e) { | |
L.DomEvent.stopPropagation(e); | |
if (this.options.trackMouse) { | |
var point = this._map.mouseEventToContainerPoint(e); | |
this.setPosition(point); | |
} | |
}, | |
_onTargetMouseout: function (e) { | |
this.hide(); | |
} | |
}); | |
L.Map.addInitHook(function () { | |
this._tooltipContainer = L.DomUtil.create('div', 'leaflet-tooltip-container', this._container); | |
}); | |
// Tooltip = function (options) { | |
// return new Tooltip(options); | |
// }; | |
(function () { | |
var originalOnAdd = L.Marker.prototype.onAdd, | |
originalOnRemove = L.Marker.prototype.onRemove, | |
originalSetIcon = L.Marker.prototype.setIcon; | |
L.Marker.include({ | |
getTooltip: function () { | |
return this._tooltip; | |
}, | |
onAdd: function (map) { | |
originalOnAdd.call(this, map); | |
if (this.options.tooltip) { | |
this._tooltip = Tooltip(L.extend(this.options.tooltip, {target: this, map: map})); | |
} | |
}, | |
onRemove: function (map) { | |
if (this._tooltip) { | |
this._tooltip.remove(); | |
} | |
originalOnRemove.call(this, map); | |
}, | |
setIcon: function (icon) { | |
originalSetIcon.call(this, icon); | |
if (this._tooltip) { | |
this._tooltip.setTarget(this._icon); | |
} | |
} | |
}); | |
})(); | |
/** | |
* Gall projection. | |
* Code adapted from proj4js and gall.js. DON'T ASK. | |
* | |
* @type {Object} | |
*/ | |
var projection = { | |
EARTH_RADIUS: 6378137, | |
DEGREE_TO_RADIAN: 0.017453292519943295, | |
RADIAN_TO_DEGREE: 57.29577951308232, | |
project: function (latlng) { | |
var x = projection.EARTH_RADIUS * latlng.lng * 0.70710678118654752440 * projection.DEGREE_TO_RADIAN; | |
var y = projection.EARTH_RADIUS * Math.tan(0.5 * latlng.lat * projection.DEGREE_TO_RADIAN) * 1.70710678118654752440; | |
return new L.Point(x, y); | |
}, | |
unproject: function (point) { // (Point, Boolean) -> LatLng | |
var lng = (1.41421356237309504880 * point.x / projection.EARTH_RADIUS) * projection.RADIAN_TO_DEGREE; | |
var lat = (2 * Math.atan(0.58578643762690495119 * point.y / projection.EARTH_RADIUS)) * projection.RADIAN_TO_DEGREE; | |
return new L.LatLng(lat, lng); | |
} | |
}; | |
/** | |
* Mappy CRS, using our transformation, our x3 zoom levels and weird shapped world | |
* | |
* @type {*} | |
*/ | |
var crs = L.extend({}, L.CRS, { | |
code: 'ESRI:54016', | |
projection: projection, | |
transformation: new L.Transformation(1, 14168658.027268294, -1, 17449155.130499773), | |
scale: function(zoom) { | |
var scales = [ | |
73795.09389202237, | |
24598.364630674125, | |
8199.454876891376, | |
2733.1516256304585, | |
911.0505418768195, | |
303.68351395893984, | |
101.22783798631328, | |
33.74261266210443, | |
11.247537554034809, | |
3.7491791846782694, | |
1.2497263948927564, | |
0.41657546496425213, | |
0.13885848832141737 | |
]; | |
return 1 / scales[zoom]; | |
}, | |
getSize: function(zoom) { | |
var size = 384 * Math.pow(3, zoom); | |
return new L.Point(size, size); | |
} | |
}); | |
L.Mappy.Map = L.Map.extend({ | |
/** | |
* Map layers (overlay/viewmode) - traffic/public_transport | |
* .viewmode (standard/photo/hybrid) | |
* .overlay (traffic/public_transport) | |
*/ | |
_tileLayers: {}, | |
_tooltip: null, | |
/** | |
* | |
* @param element | |
* @param options | |
*/ | |
initialize: function(element, options) { | |
options = options || {}; | |
options.crs = crs; | |
// Take zoomControl out of Leaflet map init to handle it our way | |
var zoomControlOptions = (options.zoomControl !== undefined) ? options.zoomControl : true; | |
options.zoomControl = false; | |
// Take attributionControl out of Leaflet map init to handle it our way | |
var attributionControlOptions = (options.attributionControl !== undefined) ? options.attributionControl : {}; | |
options.attributionControl = false; | |
this.baseLayers = { | |
'standard': new TileLayer('standard', 1, options.tileLayerOptions), | |
'hybrid': new TileLayer('hybrid', 1, options.tileLayerOptions), | |
'photo': new TileLayer('photo', 1, options.tileLayerOptions) | |
}; | |
this.overlays = { | |
'public_transport': new TileLayer('public_transport', 2, options.tileLayerOptions), | |
'traffic': new TileLayer('traffic', 2, options.tileLayerOptions) | |
}; | |
L.Map.prototype.initialize.call(this, element, options); | |
this.attributionControl = new Attribution(attributionControlOptions).addTo(this); | |
this.logoControl = new Logo(options.logoControl || {}).addTo(this); | |
if (options.layersControl === undefined || options.layersControl) { | |
this.layersControl = new Layers(this.baseLayers, this.overlays, options.layersControl || {}).addTo(this); | |
} | |
if (zoomControlOptions !== false) { | |
this.zoomControl = new L.Control.Zoom(zoomControlOptions || {}).addTo(this); | |
} | |
if (! options.viewmode || !this._baseLayers[options.viewmode]) { | |
this.addLayer(this.baseLayers.standard); | |
} else { | |
this.addLayer(this.baseLayers[options.viewmode]); | |
} | |
this._tooltip = new Tooltip({ | |
map: this, | |
showDelay: 0, | |
hideDelay: 0 | |
}); | |
// bind events | |
this.on('mousemove', this._handleMousemove); | |
// hide background items' tooltip on drag/zoom | |
this.on('move', this._tooltip.hide, this._tooltip); | |
this.on('zoomstart', this._tooltip.hide, this._tooltip); | |
}, | |
/** | |
* Sets the background map layer (viewmode). Available viewmodes : standard, photo & hybrid. | |
* | |
* @param {string} name | |
*/ | |
setViewmode: function(name) { | |
name = name || 'standard'; | |
return this._setTileLayer(this.baseLayers, name); | |
}, | |
/** | |
* Sets the overlay map layer. Available overlays : traffic & public_transport. | |
* If nothing is specified, remove the current overlay. | |
* | |
* @param name | |
*/ | |
setOverlay: function(name) { | |
return this._setTileLayer(this.overlays, name); | |
}, | |
/** | |
* Returns the active Tilelayer of specified type | |
* | |
* @param type | |
*/ | |
getTilelayer: function(type) { | |
var layers = type === "overlay" ? this.overlays : this.baseLayers; | |
for (var layerName in layers) { | |
if (this.hasLayer(layers[layerName])) { | |
return layers[layerName]; | |
} | |
} | |
return null; | |
}, | |
/** | |
* Sets a map layer (overlay/viewmode) on map | |
* | |
* @param layer type of layer to create (viewmode, overlay) | |
* @param name name of the viewmode | |
**/ | |
_setTileLayer: function(layers, name) { | |
for (var layerName in layers) { | |
if (this.hasLayer(layers[layerName])) { | |
this.removeLayer(layers[layerName]); | |
} | |
} | |
if (layers[name]) { | |
this.addLayer(layers[name]); | |
} | |
return this; | |
}, | |
/** | |
* handle actions depending on cursor position (bind on mousemove event) | |
* | |
* @param event "mousemove" leaflet event | |
**/ | |
_handleMousemove: function(event) { | |
var items = this.getTilelayer().layerItems; | |
var overlay = this.getTilelayer('overlay'); | |
if (overlay) { | |
items = items.concat(overlay.layerItems); | |
} | |
var i = 0, | |
found = false; | |
while (i < items.length && !found) { | |
var box = items[i].box; | |
if (box.minx < event.latlng.lng && event.latlng.lng < box.maxx && | |
box.miny < event.latlng.lat && event.latlng.lat < box.maxy) { | |
if (!this._tooltip.isVisible()) { | |
this._showItemTooltip(items[i]); | |
} | |
found = true; | |
} | |
i++; | |
} | |
if (!found) { | |
this._tooltip.hide(); | |
} | |
}, | |
_showItemTooltip: function(item) { | |
var transportLabels = { | |
'M' : 'Métro', | |
'S' : 'RER', | |
'T' : 'Train', | |
'TY' : 'Tramway' | |
}; | |
var lines = []; | |
var itemLine = item.properties.description.line; | |
itemLine = itemLine instanceof Array ? itemLine : [itemLine]; // Thx Lbx... | |
for (var j = 0; j < itemLine.length; j++) { | |
if (!lines[itemLine[j].type]) { | |
lines[itemLine[j].type] = []; | |
} | |
lines[itemLine[j].type].push(itemLine[j].num); | |
} | |
// var description = 'ligne' + (lines.length > 1 ? 's ' : ' ') + lines.join(' / '); | |
var description = ''; | |
for (var type in lines) { | |
description += '</p><p>' + (transportLabels[type] || type) + ' : ' + lines[type].join(', '); | |
} | |
var popupPosition = this.latLngToContainerPoint(L.latLng((item.box.maxy + item.box.miny)/2, (item.box.maxx + item.box.minx)/2)); | |
this._tooltip.show(popupPosition, '<div><p>'+item.properties.description.label + description + '</p></div>'); | |
} | |
}); | |
L.Mappy.RouteModes = { | |
PEDESTRIAN: 'ped', | |
BIKE: 'bik', | |
MOTORBIKE: 'mot', | |
CAR: 'midcar' | |
}; | |
L.Mappy.Services = { | |
_checkToken: function() { | |
if (!L.Mappy._getToken()) { | |
throw new Error('Token must be set before using Mappy Services (see the docs).'); | |
} | |
}, | |
_decodePolyline : function(encoded) | |
{ | |
var len = encoded.length, | |
tmp = [], | |
decoded = [], | |
index = 0, | |
lat = 0, | |
lng = 0, | |
decodeNextPoint = function() { | |
var b, | |
shift = 0, | |
result = 0; | |
do { | |
//get binary encodings | |
b = encoded.charCodeAt(index++) - 63; | |
//binary shift | |
result |= (b & 0x1f) << shift; | |
//move to next chunk | |
shift += 5; | |
} while (b >= 0x20); //see if another binary value | |
//if negative, flip bits & return | |
return (((result & 1) > 0) ? ~(result >> 1) : (result >> 1)); | |
}; | |
while (index < len) { | |
tmp.push(decodeNextPoint()); | |
} | |
for(var i = 0; i < tmp.length ; i +=2) | |
{ | |
lat += tmp[i] * 1e-5; | |
lng += tmp[i+1] * 1e-5; | |
decoded.push([lat, lng]); | |
} | |
return decoded; | |
}, | |
geocode: function(query, successCallback, failureCallback) { | |
this._checkToken(); | |
if (query instanceof L.LatLng) { | |
query = query.lat + ',' + query.lng; | |
} else if (query instanceof Array) { | |
query = query[0] + ',' + query[1]; | |
} | |
L.Mappy.JSONP({ | |
url: 'http' + (L.Mappy._getHttps() ? 's': '') + '://axe.' + L.Mappy._getDomain() + '/1v1/loc/get.aspx', | |
data: { | |
'opt.format': 'json', | |
'opt.namedPlaceSearch': 1, | |
'opt.interactive': 1, | |
'opt.language': 'FRE', | |
'opt.xmlOutput': '3v0', | |
'auth': L.Mappy._getToken(), | |
'fullAddress': query | |
}, | |
success: function(response) { | |
if (!response.kml.Document) { | |
return []; | |
} | |
var placemark = response.kml.Document.Placemark; | |
placemark = placemark instanceof Array ? placemark : [placemark]; | |
successCallback(placemark); | |
}, | |
error: failureCallback | |
}); | |
}, | |
route: function(steps) { | |
this._checkToken(); | |
var options = {}, successCallback, failureCallback, data, step, elt; | |
if (typeof arguments[1] === 'function') { | |
successCallback = arguments[1]; | |
failureCallback = arguments[2]; | |
} else { | |
for (var name in arguments[1]) { | |
options['opt.' + name] = arguments[1][name]; | |
} | |
successCallback = arguments[2]; | |
failureCallback = arguments[3]; | |
} | |
data = L.extend(options || {}, { | |
'start.x': '', | |
'start.y': '', | |
'via.x': '', | |
'via.y': '', | |
'end.x': '', | |
'end.y': '', | |
'opt.trace' : 1, | |
'opt.format': 'json', | |
'opt.rbver': 5, | |
'auth': L.Mappy._getToken() | |
}); | |
for (var i = 0; i < steps.length; i++) { | |
step = L.latLng(steps[i]); | |
if (i === 0) { | |
elt = 'start'; | |
} else if (i === (steps.length - 1)) { | |
elt = 'end'; | |
} else { | |
elt = 'via'; | |
if (data['via.x'] !== '') { | |
data['via.x'] += ','; | |
data['via.y'] += ','; | |
} | |
} | |
data[elt + '.x'] += step.lng; | |
data[elt + '.y'] += step.lat; | |
} | |
var that = this; | |
L.Mappy.JSONP({ | |
url: 'http' + (L.Mappy._getHttps() ? 's': '') + '://axe.' + L.Mappy._getDomain() + '/1v1/route/get.aspx', | |
data: data, | |
success: function(results) { | |
try { | |
if (!results.routes.route.actions) { | |
throw new Error('No route found !'); | |
} | |
var polyline = that._decodePolyline(results.routes.route['polyline-definition'].polyline); | |
results.routes.route['polyline-definition'].polyline = polyline; | |
successCallback(results); | |
} catch (error) { | |
failureCallback(error); | |
} | |
}, | |
error: failureCallback | |
}); | |
} | |
}; | |
}(window.L)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment